-
Notifications
You must be signed in to change notification settings - Fork 17.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd/compile: escape analysis on interface calls #36964
Comments
There's lots of issues about resolving interfaces at compile time, so we don't have to allocate their receiver or args: #8618 #18822 #23676 #33160 It's an interesting idea to resolve this at run time instead. Generate code for both cases and decide which allocation path to use. It can get tricky:
Then we can only allocate At the time we allocate
Until we can inspect Sometimes interfaces are wrappers. For example, |
More ideas to explore:
Ofcause it is quite tricky and 2 and 3 require internal sub interface pointers in bufio.Writer to stay consistent and do not change to anything but nil. Probably it requires a huge logic in the compiler :/ |
Here's another data point. In this program, we have no allocation for the closure or the potentially-escaped variable:
However, in this similar program that uses an interface value, we have an allocation for both the interface and
Issue #8618 tracks the issue about allocating storage for the interface. I think this issue is the right one for tracking escape analysis of the value inside the interface. |
What version of Go are you using (
go version
)?1.13.1
Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?on any OS / env
What did you do?
type myCtx struct { data int }
type interface if { Call(ctx *myCtx) }
ctx := myCtx{}
i.Call(&ctx) // i == interface of type 'if'
What did you expect to see?
ctx allocated on stack (as Call() method was empty in my case).
What did you see instead?
ctx allocated on heap as escape analysis doesn't work through interface types. Compiler simply doesn't know the future of the pointer.
Suggestions
As interfaces are widely used in the language it seems quite important to be able to perform such optimisations.
What if interface run-time type info had escape flag for the method. That would allow to generate code for both escaping interface calls and non-escaping ones.
The text was updated successfully, but these errors were encountered: