Go runtime — the 24-byte size class and an 8-byte allocation win

While reading Dave Cheney’s post, I noticed a benchmark that highlights an eight-byte allocation improvement.

1
2
3
4
5
6
7
8
9
func BenchmarkSortStrings(b *testing.B) {
s := []string{"heart", "lungs", "brain", "kidneys", "pancreas"}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
var ss sort.StringSlice = s
var si sort.Interface = ss
sort.Sort(si)
}
}

The benchmark sorts a []string and reports allocations.

These lines:

1
2
3
var ss sort.StringSlice = s
var si sort.Interface = ss
sort.Sort(si)

are equivalent to:

1
sort.Strings(s)

Both paths convert []string into something implementing sort.Interface.

If you know how iface works, the data word holds the value — one machine word (8 bytes on 64-bit). A slice is 24 bytes: backing-array pointer, length, and capacity. It does not fit in the data word, so the runtime stores a pointer to the slice. Creating that pointer can force the slice to escape to the heap, because the compiler cannot prove the original ss outlives si.

That is where the allocation shows up.

Benchmark on Go 1.16:

1
2
3
4
5
6
7
$ go test sort_test.go -bench . -benchmem 
goos: darwin
goarch: amd64
cpu: Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz
BenchmarkInts-4 18846020 90.09 ns/op 24 B/op 1 allocs/op
PASS
ok command-line-arguments 2.663s

Twenty-four bytes per op — where is the “eight bytes saved”?

Compared to before Go 1.16, for example Go 1.15:

1
2
3
4
5
6
$ go1.15 test sort_test.go -bench . -benchmem 
goos: darwin
goarch: amd64
BenchmarkSortStrings-4 8617881 172 ns/op 32 B/op 1 allocs/op
PASS
ok command-line-arguments 1.649s

The runtime gained a 24-byte size class so three-word objects are not rounded up to 32 bytes. Commit: runtime: add 24 byte allocation size class.

Questions? Leave a comment below or email: newbvirgil@gmail.com