1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
// 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
}
}
type (
struct24 struct{ a, b, c int64 }
struct32 struct{ a, b, c, d int64 }
struct40 struct{ a, b, c, d, e int64 }
)
func BenchmarkGrowSlice(b *testing.B) {
b.Run("Byte", func(b *testing.B) {
x := make([]byte, 9)
for i := 0; i < b.N; i++ {
_ = append([]byte(nil), x...)
}
})
b.Run("Int", func(b *testing.B) {
x := make([]int, 9)
for i := 0; i < b.N; i++ {
_ = append([]int(nil), x...)
}
})
b.Run("Ptr", func(b *testing.B) {
x := make([]*byte, 9)
for i := 0; i < b.N; i++ {
_ = append([]*byte(nil), x...)
}
})
b.Run("Struct", func(b *testing.B) {
b.Run("24", func(b *testing.B) {
x := make([]struct24, 9)
for i := 0; i < b.N; i++ {
_ = append([]struct24(nil), x...)
}
})
b.Run("32", func(b *testing.B) {
x := make([]struct32, 9)
for i := 0; i < b.N; i++ {
_ = append([]struct32(nil), x...)
}
})
b.Run("40", func(b *testing.B) {
x := make([]struct40, 9)
for i := 0; i < b.N; i++ {
_ = append([]struct40(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)]
}
}
})
})
}
|