Skip to content

cmd/compile: reuse allocated value in interface #23677

@dsnet

Description

@dsnet

Consider the following snippet:

var sink interface{}

func Benchmark(b *testing.B) {
	b.ReportAllocs()
	for i := 0; i < b.N; i++ {
		var s = "hello"
		sink = s
	}
}

This prints:

Benchmark-8   	30000000	        40.4 ns/op	      16 B/op	       1 allocs/op

On every iteration, it is calling runtime.convT2Estring. It should be able to figure out that the runtime.eface._type is identical and also a non-pointer kind. In that situation, it should be able to just reuse that previously allocated value.

This should be safe, since Go does not allow you to take the address of a interface value:

&(sink.(string)) // cannot take the address of sink.(string)

Thus, nothing else should be referencing the underlying value (unless unsafe was used).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions