New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/compile: reuse allocated value in interface #23677

Open
dsnet opened this Issue Feb 3, 2018 · 1 comment

Comments

Projects
None yet
4 participants
@dsnet
Member

dsnet commented Feb 3, 2018

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).

@dsnet dsnet added the Performance label Feb 3, 2018

@cznic

This comment has been minimized.

Contributor

cznic commented Feb 3, 2018

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

I think var sink2 interface{} = sink does reference the underlying value of sink.

@ianlancetaylor ianlancetaylor added this to the Go1.11 milestone Mar 28, 2018

@bradfitz bradfitz modified the milestones: Go1.11, Unplanned May 18, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment