You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
cmd/compile: possibly unnecessary allocation for interface data #20380
package main
typeFooerinterface {
Foo()
}
// HandleFooer is intended to show that passing interface will not rasie an allocation.funcHandleFooer(fFooer) {
// Passing interface Fooer<*Concrete> will not raise an allocation // even if we do some work on f, such as f.(*Concrete).Foo().
}
typeConcretebytefunc (cConcrete) Foo() {}
And if we run these benchmarks (with -gcflags="-l"):
package main
import"testing"funcBenchmarkCallFooer(b*testing.B) {
fori:=0; i<b.N; i++ {
varcConcrete=42// Moved to heap.Fooer(&c).Foo()
}
}
funcBenchmarkPassFooer(b*testing.B) {
fori:=0; i<b.N; i++ {
varcConcrete=42// Not moved to heap.HandleFooer(Fooer(&c))
}
}
./iface_test.go:17: HandleFooer f does not escape
./iface_test.go:22: Fooer(&c) escapes to heap
./iface_test.go:22: from Fooer(&c).Foo() (receiver in indirect call) at ./iface_test.go:22
./iface_test.go:22: &c escapes to heap
./iface_test.go:22: from Fooer(&c) (interface-converted) at ./iface_test.go:22
./iface_test.go:22: from Fooer(&c).Foo() (receiver in indirect call) at ./iface_test.go:22
./iface_test.go:21: moved to heap: c
Expected behavior: &c stays on stack in both cases.
This is a limitation of our current escape analysis. It totally gives up on interface method invocations. We could fix this problem by propagating known concrete types of interface values during escape analysis & doing the method devirtualization.
This limitation has been around for a while.
See #19361
This is basically a simpler form of #14018, I'm going to close as a dup.
Activity
bradfitz commentedon May 16, 2017
/cc @dr2chase
randall77 commentedon May 16, 2017
This is a limitation of our current escape analysis. It totally gives up on interface method invocations. We could fix this problem by propagating known concrete types of interface values during escape analysis & doing the method devirtualization.
This limitation has been around for a while.
See #19361
This is basically a simpler form of #14018, I'm going to close as a dup.