Open
Description
Memory allocation using make([]int, K)
is surprisingly slow compared to append(nil, ...)
, even though append
does strictly more work, such as copying.
$ cat a_test.go
package main
import "testing"
const K = 1e6
var escape []int
func BenchmarkMake(b *testing.B) {
for i := 0; i < b.N; i++ {
escape = make([]int, K)
}
}
var empty [K]int
func BenchmarkAppend(b *testing.B) {
for i := 0; i < b.N; i++ {
escape = append([]int(nil), empty[:]...)
}
}
$ go version
go version devel +6317adeed7 Tue Jan 2 13:39:20 2018 +0000 linux/amd64
$ go test -bench=. a_test.go
BenchmarkAppend-12 1000 1208800 ns/op
BenchmarkMake-12 1000 1473106 ns/op
While reporting this issue, I initially used an older runtime from December 18 in which the effect was much stronger: 10x-20x slowdown. But that seems to have been fixed.
Curiously, this issue is the exact opposite of the problem reported in #14718 (now closed).