Skip to content

runtime: reuse map string key instead of allocating #55930

@dsnet

Description

@dsnet

Consider the following benchmark:

func Benchmark(b *testing.B) {
	m := map[string]int{}
	k := []byte("hello, world")

	b.ReportAllocs()
	for i := 0; i < b.N; i++ {
		m[string(k)] = 1
	}
}

This currently prints:

Benchmark    	37319388	        29.83 ns/op	      16 B/op	       1 allocs/op

I expect this to not allocate since it is reusing the same map entry every time.
The very first mapassign will need to allocate the string on heap, but subsequent operations should not need to.
This is related to #45045, which made this optimization harder.

\cc @randall77 @cuonglm

Metadata

Metadata

Labels

NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Performancecompiler/runtimeIssues related to the Go compiler and/or runtime.

Type

No type

Projects

Status

Todo

Relationships

None yet

Development

No branches or pull requests

Issue actions