aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/slice_test.go
diff options
context:
space:
mode:
authorMartin Möhrmann <moehrmann@google.com>2018-04-26 18:30:11 +0200
committerMartin Möhrmann <moehrmann@google.com>2018-05-06 04:28:23 +0000
commita8a60ac2a7bec701de6b502889e1dc740761e183 (patch)
treec53e32a00f0fc81f45099b0eadca4cfd6846ee24 /src/runtime/slice_test.go
parent87412a143051d63f4ce68900f4668b2a3fb5c4f2 (diff)
downloadgo-a8a60ac2a7bec701de6b502889e1dc740761e183.tar.xz
cmd/compile: optimize append(x, make([]T, y)...) slice extension
Changes the compiler to recognize the slice extension pattern append(x, make([]T, y)...) and replace it with growslice and an optional memclr to avoid an allocation for make([]T, y). Memclr is not called in case growslice already allocated a new cleared backing array when T contains pointers. amd64: name old time/op new time/op delta ExtendSlice/IntSlice 103ns ± 4% 57ns ± 4% -44.55% (p=0.000 n=18+18) ExtendSlice/PointerSlice 155ns ± 3% 77ns ± 3% -49.93% (p=0.000 n=20+20) ExtendSlice/NoGrow 50.2ns ± 3% 5.2ns ± 2% -89.67% (p=0.000 n=18+18) name old alloc/op new alloc/op delta ExtendSlice/IntSlice 64.0B ± 0% 32.0B ± 0% -50.00% (p=0.000 n=20+20) ExtendSlice/PointerSlice 64.0B ± 0% 32.0B ± 0% -50.00% (p=0.000 n=20+20) ExtendSlice/NoGrow 32.0B ± 0% 0.0B -100.00% (p=0.000 n=20+20) name old allocs/op new allocs/op delta ExtendSlice/IntSlice 2.00 ± 0% 1.00 ± 0% -50.00% (p=0.000 n=20+20) ExtendSlice/PointerSlice 2.00 ± 0% 1.00 ± 0% -50.00% (p=0.000 n=20+20) ExtendSlice/NoGrow 1.00 ± 0% 0.00 -100.00% (p=0.000 n=20+20) Fixes #21266 Change-Id: Idc3077665f63cbe89762b590c5967a864fd1c07f Reviewed-on: https://go-review.googlesource.com/109517 Run-TryBot: Martin Möhrmann <moehrmann@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
Diffstat (limited to 'src/runtime/slice_test.go')
-rw-r--r--src/runtime/slice_test.go30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/runtime/slice_test.go b/src/runtime/slice_test.go
index 46db071ebe..c2dfb7afd1 100644
--- a/src/runtime/slice_test.go
+++ b/src/runtime/slice_test.go
@@ -72,6 +72,36 @@ func BenchmarkGrowSlice(b *testing.B) {
})
}
+var (
+ SinkIntSlice []int
+ SinkIntPointerSlice []*int
+)
+
+func BenchmarkExtendSlice(b *testing.B) {
+ var length = 4 // Use a variable to prevent stack allocation of slices.
+ b.Run("IntSlice", func(b *testing.B) {
+ s := make([]int, 0, length)
+ for i := 0; i < b.N; i++ {
+ s = append(s[:0:length/2], make([]int, length)...)
+ }
+ SinkIntSlice = s
+ })
+ b.Run("PointerSlice", func(b *testing.B) {
+ s := make([]*int, 0, length)
+ for i := 0; i < b.N; i++ {
+ s = append(s[:0:length/2], make([]*int, length)...)
+ }
+ SinkIntPointerSlice = s
+ })
+ b.Run("NoGrow", func(b *testing.B) {
+ s := make([]int, 0, length)
+ for i := 0; i < b.N; i++ {
+ s = append(s[:0:length], make([]int, length)...)
+ }
+ SinkIntSlice = s
+ })
+}
+
func BenchmarkAppend(b *testing.B) {
b.StopTimer()
x := make([]int, 0, N)