]> Cypherpunks repositories - gostls13.git/commit
cmd/compile/internal/walk: convert composite literals to interfaces without allocating
authorthepudds <thepudds1460@gmail.com>
Fri, 14 Feb 2025 04:49:50 +0000 (23:49 -0500)
committerDavid Chase <drchase@google.com>
Wed, 21 May 2025 19:23:26 +0000 (12:23 -0700)
commitf4de2ecffb9c107e6058c1ebb30c68de1157f5c6
tree85625c56665e010090451c811ccc4d7dbfd5cbf5
parentce46c9db867fb54a9c1f39b73ac8c2f339ca0587
cmd/compile/internal/walk: convert composite literals to interfaces without allocating

Today, this interface conversion causes the struct literal
to be heap allocated:

    var sink any

    func example1() {
        sink = S{1, 1}
    }

For basic literals like integers that are directly used in
an interface conversion that would otherwise allocate, the compiler
is able to use read-only global storage (see #18704).

This CL extends that to struct and array literals as well by creating
read-only global storage that is able to represent for example S{1, 1},
and then using a pointer to that storage in the interface
when the interface conversion happens.

A more challenging example is:

    func example2() {
        v := S{1, 1}
        sink = v
    }

In this case, the struct literal is not directly part of the
interface conversion, but is instead assigned to a local variable.

To still avoid heap allocation in cases like this, in walk we
construct a cache that maps from expressions used in interface
conversions to earlier expressions that can be used to represent the
same value (via ir.ReassignOracle.StaticValue). This is somewhat
analogous to how we avoided heap allocation for basic literals in
CL 649077 earlier in our stack, though here we also need to do a
little more work to create the read-only global.

CL 649076 (also earlier in our stack) added most of the tests
along with debug diagnostics in convert.go to make it easier
to test this change.

See the writeup in #71359 for details.

Fixes #71359
Fixes #71323
Updates #62653
Updates #53465
Updates #8618

Change-Id: I8924f0c69ff738ea33439bd6af7b4066af493b90
Reviewed-on: https://go-review.googlesource.com/c/go/+/649555
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Keith Randall <khr@google.com>
src/cmd/compile/internal/walk/order.go
src/cmd/compile/internal/walk/walk.go
src/fmt/fmt_test.go
test/codegen/zerosize.go
test/escape_iface_data.go
test/live.go
test/live_regabi.go