-
Notifications
You must be signed in to change notification settings - Fork 17.6k
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: function argument value escapes no matter the run-time condition #42781
Comments
Would you mind elaborating more details? Both your programs above will be heap allocated the |
I update the issue with the generated asm, and highlighting the change in the allocation position: one is always, while the other is conditional. |
Yes, the escape analysis is per-variable and is not flow-sensitive. Making it flow sensitive would require identifying when a variable can safely be split--basically, automatically inserting the |
CC @dr2chase |
This is one case where if we do escape analysis in SSA it probably can be done fairly easily. (We've been thinking about it for a while. But it needs a lot of work.) |
I'd prefer you didn't call it "wrong". |
I found this issue (or at least a related issue) when looking at a real-world scenario that might be quite common. It's common to have tracing functions that only need to record occasionally. When tracing isn't Here's an example, derived from some real-world code: https://play.golang.org/p/z4O9gg8UOkp
When I run the above in a benchmark, I see this:
That is, the argument slice is always being allocated, even when tracing is not enabled. Currently the only way of working around this issue a bit tedious: put an explicit |
Actually, I've just realised that's not the case. We can work around it by adding an explicit allocation:
It's not totally ideal (if |
What version of Go are you using (
go version
)?What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
While benchmarking a time histogram implementation, I found an unexpected allocation in a
IncrementOrStore()
method implementation, happening directly in the prolog code the compiler generated, where the return value is allocated regardless of whether it's needed or not.I could reproduce the issue with the following dummy function:
The compiled function of the previous example will allocate the int value in its function prolog rather than in the true-condition path:
The workaround I use for now is to rather return the value from the condition block scope:
func foo(cond bool, v int) *int { if cond { + v := v // new declaration in this scope to avoid using function scope return &v } return nil }
Resulting in:
The text was updated successfully, but these errors were encountered: