mirror of
https://github.com/golang/go
synced 2024-10-04 20:21:22 -06:00
8ce8adbe7a
Fixes #4963. Sets the append crossover to 0 on intel platforms. Results for linux/amd64 Core i5 SNB benchmark old ns/op new ns/op delta BenchmarkAppend 102 104 +1.96% BenchmarkAppend1Byte 10 11 +0.92% BenchmarkAppend4Bytes 15 11 -28.10% BenchmarkAppend7Bytes 17 12 -32.58% BenchmarkAppend8Bytes 18 12 -36.17% BenchmarkAppend15Bytes 24 11 -55.02% BenchmarkAppend16Bytes 25 11 -56.03% BenchmarkAppend32Bytes 11 12 +4.31% BenchmarkAppendStr1Byte 8 9 +13.99% BenchmarkAppendStr4Bytes 11 9 -17.52% BenchmarkAppendStr8Bytes 14 9 -35.70% BenchmarkAppendStr16Bytes 21 9 -55.19% BenchmarkAppendStr32Bytes 10 10 -5.66% BenchmarkAppendSpecialCase 49 52 +7.96% Results for linux/386 Atom(TM) CPU 330 @ 1.60GHz benchmark old ns/op new ns/op delta BenchmarkAppend 219 218 -0.46% BenchmarkAppend1Byte 75 72 -3.44% BenchmarkAppend4Bytes 92 73 -19.87% BenchmarkAppend7Bytes 108 74 -31.20% BenchmarkAppend8Bytes 116 74 -35.95% BenchmarkAppend15Bytes 162 77 -52.22% BenchmarkAppend16Bytes 169 77 -54.20% BenchmarkAppend32Bytes 88 86 -2.38% BenchmarkAppendStr1Byte 57 59 +3.32% BenchmarkAppendStr4Bytes 72 59 -17.40% BenchmarkAppendStr8Bytes 92 60 -34.70% BenchmarkAppendStr16Bytes 141 63 -54.89% BenchmarkAppendStr32Bytes 75 73 -2.64% BenchmarkAppendSpecialCase 270 270 +0.00% R=golang-dev, r CC=golang-dev https://golang.org/cl/12440044
132 lines
2.4 KiB
Go
132 lines
2.4 KiB
Go
// Copyright 2011 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
package runtime_test
|
|
|
|
import "testing"
|
|
|
|
const N = 20
|
|
|
|
func BenchmarkAppend(b *testing.B) {
|
|
b.StopTimer()
|
|
x := make([]int, 0, N)
|
|
b.StartTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
x = x[0:0]
|
|
for j := 0; j < N; j++ {
|
|
x = append(x, j)
|
|
}
|
|
}
|
|
}
|
|
|
|
func benchmarkAppendBytes(b *testing.B, length int) {
|
|
b.StopTimer()
|
|
x := make([]byte, 0, N)
|
|
y := make([]byte, length)
|
|
b.StartTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
x = x[0:0]
|
|
x = append(x, y...)
|
|
}
|
|
}
|
|
|
|
func BenchmarkAppend1Byte(b *testing.B) {
|
|
benchmarkAppendBytes(b, 1)
|
|
}
|
|
|
|
func BenchmarkAppend4Bytes(b *testing.B) {
|
|
benchmarkAppendBytes(b, 4)
|
|
}
|
|
|
|
func BenchmarkAppend7Bytes(b *testing.B) {
|
|
benchmarkAppendBytes(b, 7)
|
|
}
|
|
|
|
func BenchmarkAppend8Bytes(b *testing.B) {
|
|
benchmarkAppendBytes(b, 8)
|
|
}
|
|
|
|
func BenchmarkAppend15Bytes(b *testing.B) {
|
|
benchmarkAppendBytes(b, 15)
|
|
}
|
|
|
|
func BenchmarkAppend16Bytes(b *testing.B) {
|
|
benchmarkAppendBytes(b, 16)
|
|
}
|
|
|
|
func BenchmarkAppend32Bytes(b *testing.B) {
|
|
benchmarkAppendBytes(b, 32)
|
|
}
|
|
|
|
func benchmarkAppendStr(b *testing.B, str string) {
|
|
b.StopTimer()
|
|
x := make([]byte, 0, N)
|
|
b.StartTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
x = x[0:0]
|
|
x = append(x, str...)
|
|
}
|
|
}
|
|
|
|
func BenchmarkAppendStr1Byte(b *testing.B) {
|
|
benchmarkAppendStr(b, "1")
|
|
}
|
|
|
|
func BenchmarkAppendStr4Bytes(b *testing.B) {
|
|
benchmarkAppendStr(b, "1234")
|
|
}
|
|
|
|
func BenchmarkAppendStr8Bytes(b *testing.B) {
|
|
benchmarkAppendStr(b, "12345678")
|
|
}
|
|
|
|
func BenchmarkAppendStr16Bytes(b *testing.B) {
|
|
benchmarkAppendStr(b, "1234567890123456")
|
|
}
|
|
|
|
func BenchmarkAppendStr32Bytes(b *testing.B) {
|
|
benchmarkAppendStr(b, "12345678901234567890123456789012")
|
|
}
|
|
|
|
func BenchmarkAppendSpecialCase(b *testing.B) {
|
|
b.StopTimer()
|
|
x := make([]int, 0, N)
|
|
b.StartTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
x = x[0:0]
|
|
for j := 0; j < N; j++ {
|
|
if len(x) < cap(x) {
|
|
x = x[:len(x)+1]
|
|
x[len(x)-1] = j
|
|
} else {
|
|
x = append(x, j)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
var x []int
|
|
|
|
func f() int {
|
|
x[:1][0] = 3
|
|
return 2
|
|
}
|
|
|
|
func TestSideEffectOrder(t *testing.T) {
|
|
x = make([]int, 0, 10)
|
|
x = append(x, 1, f())
|
|
if x[0] != 1 || x[1] != 2 {
|
|
t.Error("append failed: ", x[0], x[1])
|
|
}
|
|
}
|
|
|
|
func TestAppendOverlap(t *testing.T) {
|
|
x := []byte("1234")
|
|
x = append(x[1:], x...) // p > q in runtime·appendslice.
|
|
got := string(x)
|
|
want := "2341234"
|
|
if got != want {
|
|
t.Errorf("overlap failed: got %q want %q", got, want)
|
|
}
|
|
}
|