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).
Consider the following snippet:
This prints:
On every iteration, it is calling
runtime.convT2Estring. It should be able to figure out that theruntime.eface._typeis 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:
Thus, nothing else should be referencing the underlying value (unless unsafe was used).