mirror of
https://github.com/golang/go
synced 2024-11-19 18:34:43 -07:00
c1e267cc73
Also add a benchmark that shows off the new behavior. The existing benchmarks reuse the same slice, and thus don't ever have to clear memory. Running the Append|Grow benchmarks in runtime: name old time/op new time/op delta AppendSliceLarge/1024Bytes-12 265ns ± 1% 265ns ± 3% ~ (p=0.524 n=17+20) AppendSliceLarge/4096Bytes-12 807ns ± 3% 772ns ± 1% -4.38% (p=0.000 n=20+20) AppendSliceLarge/16384Bytes-12 3.20µs ± 4% 2.82µs ± 4% -11.93% (p=0.000 n=19+20) AppendSliceLarge/65536Bytes-12 13.0µs ± 4% 11.0µs ± 3% -15.22% (p=0.000 n=20+20) AppendSliceLarge/262144Bytes-12 62.7µs ± 1% 51.6µs ± 1% -17.67% (p=0.000 n=19+20) AppendSliceLarge/1048576Bytes-12 337µs ± 3% 289µs ± 3% -14.36% (p=0.000 n=20+20) GrowSliceBytes-12 31.2ns ± 4% 31.4ns ±11% ~ (p=0.308 n=19+18) GrowSliceInts-12 53.4ns ±14% 45.0ns ± 6% -15.74% (p=0.000 n=20+19) GrowSlicePtr-12 87.0ns ± 3% 83.3ns ± 3% -4.26% (p=0.000 n=18+17) GrowSliceStruct24Bytes-12 88.9ns ± 5% 77.8ns ± 2% -12.45% (p=0.000 n=20+19) Append-12 17.2ns ± 1% 17.3ns ± 2% ~ (p=0.464 n=18+17) AppendGrowByte-12 2.28ms ± 1% 1.92ms ± 2% -15.65% (p=0.000 n=20+18) AppendGrowString-12 255ms ± 3% 253ms ± 4% ~ (p=0.065 n=19+19) AppendSlice/1Bytes-12 3.13ns ± 0% 3.11ns ± 1% -0.65% (p=0.000 n=17+18) AppendSlice/4Bytes-12 3.02ns ± 2% 3.11ns ± 1% +3.27% (p=0.000 n=18+17) AppendSlice/7Bytes-12 4.14ns ± 3% 4.13ns ± 2% ~ (p=0.380 n=19+18) AppendSlice/8Bytes-12 3.74ns ± 3% 3.68ns ± 1% -1.76% (p=0.000 n=19+18) AppendSlice/15Bytes-12 4.03ns ± 2% 4.04ns ± 2% ~ (p=0.261 n=19+20) AppendSlice/16Bytes-12 4.03ns ± 2% 4.03ns ± 0% ~ (p=0.062 n=18+17) AppendSlice/32Bytes-12 3.23ns ± 4% 3.43ns ± 1% +6.10% (p=0.000 n=17+18) AppendStr/1Bytes-12 3.51ns ± 1% 3.52ns ± 1% ~ (p=0.321 n=18+19) AppendStr/4Bytes-12 3.46ns ± 1% 3.46ns ± 1% ~ (p=0.977 n=18+20) AppendStr/8Bytes-12 3.18ns ± 1% 3.19ns ± 1% ~ (p=0.650 n=16+17) AppendStr/16Bytes-12 6.08ns ±27% 5.52ns ± 3% -9.16% (p=0.002 n=18+19) AppendStr/32Bytes-12 3.71ns ± 1% 3.53ns ± 1% -4.73% (p=0.000 n=20+19) AppendSpecialCase-12 17.7ns ± 1% 17.8ns ± 3% +0.86% (p=0.045 n=17+18) AppendInPlace/NoGrow/Byte-12 375ns ± 1% 376ns ± 1% +0.35% (p=0.021 n=20+18) AppendInPlace/NoGrow/1Ptr-12 1.01µs ± 1% 1.10µs ± 1% +9.28% (p=0.000 n=18+20) AppendInPlace/NoGrow/2Ptr-12 1.85µs ± 2% 1.71µs ± 1% -7.51% (p=0.000 n=19+18) AppendInPlace/NoGrow/3Ptr-12 2.57µs ± 2% 2.44µs ± 1% -5.08% (p=0.000 n=19+19) AppendInPlace/NoGrow/4Ptr-12 3.52µs ± 2% 3.35µs ± 2% -4.70% (p=0.000 n=20+19) AppendInPlace/Grow/Byte-12 212ns ± 1% 217ns ± 8% +2.57% (p=0.000 n=20+20) AppendInPlace/Grow/1Ptr-12 214ns ± 2% 217ns ± 3% +1.23% (p=0.001 n=18+19) AppendInPlace/Grow/2Ptr-12 298ns ± 2% 300ns ± 2% +0.55% (p=0.038 n=19+20) AppendInPlace/Grow/3Ptr-12 367ns ± 2% 366ns ± 2% ~ (p=0.452 n=20+18) AppendInPlace/Grow/4Ptr-12 416ns ± 2% 411ns ± 2% -1.18% (p=0.000 n=20+19) StackGrowth-12 43.4ns ± 1% 43.4ns ± 0% ~ (p=1.000 n=16+16) StackGrowthDeep-12 11.4µs ± 4% 10.3µs ± 4% -9.65% (p=0.000 n=20+19) Change-Id: I69a8afbd942c787c591d95b9d9439bd6db4d1e49 Reviewed-on: https://go-review.googlesource.com/30192 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
329 lines
6.7 KiB
Go
329 lines
6.7 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 (
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
const N = 20
|
|
|
|
func BenchmarkMakeSlice(b *testing.B) {
|
|
var x []byte
|
|
for i := 0; i < b.N; i++ {
|
|
x = make([]byte, 32)
|
|
_ = x
|
|
}
|
|
}
|
|
|
|
func BenchmarkGrowSliceBytes(b *testing.B) {
|
|
b.StopTimer()
|
|
var x = make([]byte, 9)
|
|
b.StartTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
_ = append([]byte(nil), x...)
|
|
}
|
|
}
|
|
|
|
func BenchmarkGrowSliceInts(b *testing.B) {
|
|
b.StopTimer()
|
|
var x = make([]int, 9)
|
|
b.StartTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
_ = append([]int(nil), x...)
|
|
}
|
|
}
|
|
|
|
func BenchmarkGrowSlicePtr(b *testing.B) {
|
|
b.StopTimer()
|
|
var x = make([]*byte, 9)
|
|
b.StartTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
_ = append([]*byte(nil), x...)
|
|
}
|
|
}
|
|
|
|
type struct24 struct{ a, b, c int64 }
|
|
|
|
func BenchmarkGrowSliceStruct24Bytes(b *testing.B) {
|
|
b.StopTimer()
|
|
var x = make([]struct24, 9)
|
|
b.StartTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
_ = append([]struct24(nil), x...)
|
|
}
|
|
}
|
|
|
|
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 BenchmarkAppendGrowByte(b *testing.B) {
|
|
for i := 0; i < b.N; i++ {
|
|
var x []byte
|
|
for j := 0; j < 1<<20; j++ {
|
|
x = append(x, byte(j))
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkAppendGrowString(b *testing.B) {
|
|
var s string
|
|
for i := 0; i < b.N; i++ {
|
|
var x []string
|
|
for j := 0; j < 1<<20; j++ {
|
|
x = append(x, s)
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkAppendSlice(b *testing.B) {
|
|
for _, length := range []int{1, 4, 7, 8, 15, 16, 32} {
|
|
b.Run(fmt.Sprint(length, "Bytes"), func(b *testing.B) {
|
|
x := make([]byte, 0, N)
|
|
y := make([]byte, length)
|
|
for i := 0; i < b.N; i++ {
|
|
x = x[0:0]
|
|
x = append(x, y...)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
var (
|
|
blackhole []byte
|
|
)
|
|
|
|
func BenchmarkAppendSliceLarge(b *testing.B) {
|
|
for _, length := range []int{1 << 10, 4 << 10, 16 << 10, 64 << 10, 256 << 10, 1024 << 10} {
|
|
y := make([]byte, length)
|
|
b.Run(fmt.Sprint(length, "Bytes"), func(b *testing.B) {
|
|
for i := 0; i < b.N; i++ {
|
|
blackhole = nil
|
|
blackhole = append(blackhole, y...)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func BenchmarkAppendStr(b *testing.B) {
|
|
for _, str := range []string{
|
|
"1",
|
|
"1234",
|
|
"12345678",
|
|
"1234567890123456",
|
|
"12345678901234567890123456789012",
|
|
} {
|
|
b.Run(fmt.Sprint(len(str), "Bytes"), func(b *testing.B) {
|
|
x := make([]byte, 0, N)
|
|
for i := 0; i < b.N; i++ {
|
|
x = x[0:0]
|
|
x = append(x, str...)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
func BenchmarkCopy(b *testing.B) {
|
|
for _, l := range []int{1, 2, 4, 8, 12, 16, 32, 128, 1024} {
|
|
buf := make([]byte, 4096)
|
|
b.Run(fmt.Sprint(l, "Byte"), func(b *testing.B) {
|
|
s := make([]byte, l)
|
|
var n int
|
|
for i := 0; i < b.N; i++ {
|
|
n = copy(buf, s)
|
|
}
|
|
b.SetBytes(int64(n))
|
|
})
|
|
b.Run(fmt.Sprint(l, "String"), func(b *testing.B) {
|
|
s := string(make([]byte, l))
|
|
var n int
|
|
for i := 0; i < b.N; i++ {
|
|
n = copy(buf, s)
|
|
}
|
|
b.SetBytes(int64(n))
|
|
})
|
|
}
|
|
}
|
|
|
|
var (
|
|
sByte []byte
|
|
s1Ptr []uintptr
|
|
s2Ptr [][2]uintptr
|
|
s3Ptr [][3]uintptr
|
|
s4Ptr [][4]uintptr
|
|
)
|
|
|
|
// BenchmarkAppendInPlace tests the performance of append
|
|
// when the result is being written back to the same slice.
|
|
// In order for the in-place optimization to occur,
|
|
// the slice must be referred to by address;
|
|
// using a global is an easy way to trigger that.
|
|
// We test the "grow" and "no grow" paths separately,
|
|
// but not the "normal" (occasionally grow) path,
|
|
// because it is a blend of the other two.
|
|
// We use small numbers and small sizes in an attempt
|
|
// to avoid benchmarking memory allocation and copying.
|
|
// We use scalars instead of pointers in an attempt
|
|
// to avoid benchmarking the write barriers.
|
|
// We benchmark four common sizes (byte, pointer, string/interface, slice),
|
|
// and one larger size.
|
|
func BenchmarkAppendInPlace(b *testing.B) {
|
|
b.Run("NoGrow", func(b *testing.B) {
|
|
const C = 128
|
|
|
|
b.Run("Byte", func(b *testing.B) {
|
|
for i := 0; i < b.N; i++ {
|
|
sByte = make([]byte, C)
|
|
for j := 0; j < C; j++ {
|
|
sByte = append(sByte, 0x77)
|
|
}
|
|
}
|
|
})
|
|
|
|
b.Run("1Ptr", func(b *testing.B) {
|
|
for i := 0; i < b.N; i++ {
|
|
s1Ptr = make([]uintptr, C)
|
|
for j := 0; j < C; j++ {
|
|
s1Ptr = append(s1Ptr, 0x77)
|
|
}
|
|
}
|
|
})
|
|
|
|
b.Run("2Ptr", func(b *testing.B) {
|
|
for i := 0; i < b.N; i++ {
|
|
s2Ptr = make([][2]uintptr, C)
|
|
for j := 0; j < C; j++ {
|
|
s2Ptr = append(s2Ptr, [2]uintptr{0x77, 0x88})
|
|
}
|
|
}
|
|
})
|
|
|
|
b.Run("3Ptr", func(b *testing.B) {
|
|
for i := 0; i < b.N; i++ {
|
|
s3Ptr = make([][3]uintptr, C)
|
|
for j := 0; j < C; j++ {
|
|
s3Ptr = append(s3Ptr, [3]uintptr{0x77, 0x88, 0x99})
|
|
}
|
|
}
|
|
})
|
|
|
|
b.Run("4Ptr", func(b *testing.B) {
|
|
for i := 0; i < b.N; i++ {
|
|
s4Ptr = make([][4]uintptr, C)
|
|
for j := 0; j < C; j++ {
|
|
s4Ptr = append(s4Ptr, [4]uintptr{0x77, 0x88, 0x99, 0xAA})
|
|
}
|
|
}
|
|
})
|
|
|
|
})
|
|
|
|
b.Run("Grow", func(b *testing.B) {
|
|
const C = 5
|
|
|
|
b.Run("Byte", func(b *testing.B) {
|
|
for i := 0; i < b.N; i++ {
|
|
sByte = make([]byte, 0)
|
|
for j := 0; j < C; j++ {
|
|
sByte = append(sByte, 0x77)
|
|
sByte = sByte[:cap(sByte)]
|
|
}
|
|
}
|
|
})
|
|
|
|
b.Run("1Ptr", func(b *testing.B) {
|
|
for i := 0; i < b.N; i++ {
|
|
s1Ptr = make([]uintptr, 0)
|
|
for j := 0; j < C; j++ {
|
|
s1Ptr = append(s1Ptr, 0x77)
|
|
s1Ptr = s1Ptr[:cap(s1Ptr)]
|
|
}
|
|
}
|
|
})
|
|
|
|
b.Run("2Ptr", func(b *testing.B) {
|
|
for i := 0; i < b.N; i++ {
|
|
s2Ptr = make([][2]uintptr, 0)
|
|
for j := 0; j < C; j++ {
|
|
s2Ptr = append(s2Ptr, [2]uintptr{0x77, 0x88})
|
|
s2Ptr = s2Ptr[:cap(s2Ptr)]
|
|
}
|
|
}
|
|
})
|
|
|
|
b.Run("3Ptr", func(b *testing.B) {
|
|
for i := 0; i < b.N; i++ {
|
|
s3Ptr = make([][3]uintptr, 0)
|
|
for j := 0; j < C; j++ {
|
|
s3Ptr = append(s3Ptr, [3]uintptr{0x77, 0x88, 0x99})
|
|
s3Ptr = s3Ptr[:cap(s3Ptr)]
|
|
}
|
|
}
|
|
})
|
|
|
|
b.Run("4Ptr", func(b *testing.B) {
|
|
for i := 0; i < b.N; i++ {
|
|
s4Ptr = make([][4]uintptr, 0)
|
|
for j := 0; j < C; j++ {
|
|
s4Ptr = append(s4Ptr, [4]uintptr{0x77, 0x88, 0x99, 0xAA})
|
|
s4Ptr = s4Ptr[:cap(s4Ptr)]
|
|
}
|
|
}
|
|
})
|
|
|
|
})
|
|
}
|