cmd/compile/internal/gc: generalize self-assignment handling in escape analysis #27772
Labels
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Performance
Milestone
Right now, there are at least 2 quite adhoc patterns that are recognized by
escape analysis as safe. It skips detailed analysis when it sees
self-assignment statement that does not introduce new pointers that need tracking.
Initially, only some simple self-slicing assignments like this were handled:
Later, some more patterns were added, so these are recognized, too:
The problem with them is that they are very fragile and can't match expressions
that are different from the simplest cases, but still represents self-assignments:
It is possible to generalize all self-assignment patterns with a concept of "base object".
What we see above is a matching of object field assignment to the object itself.
The base object is that object, the one that contains referenced field.
For the simplest cases, base object is just 1 syntactical level "above":
For cases where expression effectively returns the same object, we can skip several levels:
Given
base
function, we can express self-assignment as (pseudo Go):This covers all patterns above, plus a few more.
As a bonus, it also solves trivial
*p = *p
case (see #5919):More interesting examples of self-assignments that were not recognized until generalization:
This solution (if it is correct or can be made correct):
Collect new escape analysis results info:
And now with unpatched
go tool compile
:Now it is possible to do some comparisons.
New non-escaping values/parameters:
Since
ring.Ring.Len
receiver no longer escapes, we can try to verify and measure it with benchmarks:Old is unpatched escape analysis with leaking
r
. New is non-leakingr
:Here is
ring.Ring.Len
method implementation, for the reference:https://golang.org/src/container/ring/ring.go?s=2869:2893#L111
For additional examples, see proposed test suite extension:
The text was updated successfully, but these errors were encountered: