-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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: argument escapes when it should not #18001
Comments
For this case, it certainly doesn't escape. But what if some child function of func f() {
var t int
g(&t)
// f() can return before goroutine inside g() finishes.
}
//go:noinline
func g(a *int) {
go fn(b *int) {
// some thing here that takes enough time for f() to return first
_ = b
}(a)
} How can we determine at compile time that all the functions in call-graph rooted at |
Passing to other (or new) goroutines makes it escape. |
Yes, but how do you determine that at compile time? I am not sure if we can do this statically though. Consider following case: Whether the var escapes or not can not be determined until run time. type doer interface{
func do(i *int)
}
type syncDoer struct {}
func (syncDoer) do(i *int) {
// accesses i synchronously
}
type asyncDoer struct {}
func (asyncDoer) do(i *int) {
// accesses i asynchronously
go func(b *int) {
// time consuming work
_ = b
}(i)
}
func f(s doer) {
var t int
g(s, &t)
// f() can return before goroutine inside g() finishes.
}
//go:noinline
func g(s doer, a *int) {
s.do(a)
}
func haha() {
if ( ...some run-time condition... ) {
f(syncDoer{})
} else {
f(asyncDoer{})
}
} |
Probably too late for 1.8 unless the fix is trivial. |
@Dhananjay92, passing object to function pointer (including interface) should also make it escape. |
This is all due to the fact that the parameter to g doesn't have a name. This was fixed in CL 38600. On tip, everything's good:
|
What version of Go are you using (
go version
)?tip (0bae74e)
Compile the following code:
I think all of the three functions,
f
,h
,h2
, should not allocate on heap. But it actually does for all three. For example,f
:Escape analysis debug output shows
@dr2chase
The text was updated successfully, but these errors were encountered: