forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
go_bench_test.go
419 lines (396 loc) · 8.51 KB
/
go_bench_test.go
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package gno
import (
"fmt"
"reflect"
"testing"
)
type BenchValue interface {
Int32() int32
}
type Int32 int32
type Int32a int32
type Int32b int32
type Int32c int32
type Int32d int32
type Int32e int32
type Int32f int32
type Int32g int32
type Int32h int32
type Int32i int32
type Int32j int32
type Int32k int32
func (i Int32) Int32() int32 { return int32(i) }
func (i Int32a) Int32() int32 { return int32(i) }
func (i Int32b) Int32() int32 { return int32(i) }
func (i Int32c) Int32() int32 { return int32(i) }
func (i Int32d) Int32() int32 { return int32(i) }
func (i Int32e) Int32() int32 { return int32(i) }
func (i Int32f) Int32() int32 { return int32(i) }
func (i Int32g) Int32() int32 { return int32(i) }
func (i Int32h) Int32() int32 { return int32(i) }
func (i Int32i) Int32() int32 { return int32(i) }
func (i Int32j) Int32() int32 { return int32(i) }
func (i Int32k) Int32() int32 { return int32(i) }
func BenchmarkMapSet(b *testing.B) {
m := make(map[int32]int32)
for i := 0; i < b.N; i++ {
m[int32(i%20)] = int32(i)
}
}
func BenchmarkMapCreateSet(b *testing.B) {
for i := 0; i < b.N; i++ {
m := make(map[int32]int32)
m[int32(i%20)] = int32(i)
}
}
func BenchmarkMapCreateSetString(b *testing.B) {
for i := 0; i < b.N; i++ {
m := make(map[string]int32)
m["5"] += 1
}
}
// shows that it might be kinda worth it to not use maps but slices for struct
// fields, but for small structs.
func BenchmarkSliceIterate10(b *testing.B) {
fs := []TestField{}
for i := 0; i < 10; i++ {
fs = append(fs, TestField{fmt.Sprintf("%v", i%10), int32(i)})
}
for i := 0; i < b.N; i++ {
i10 := i % 10
for j := 0; j < 10; j++ {
if fs[i10].Name == "5" {
fs[i10].Value += 1
}
}
}
fmt.Println(fs)
}
type SomeStruct struct {
Field1 int32
Field2 int32
}
func (s SomeStruct) it() int32 {
return s.Field1 + s.Field2
}
// seems to inline.
func BenchmarkStructStack(b *testing.B) {
x := int32(0)
for i := 0; i < b.N; i++ {
s := SomeStruct{Field1: int32(i) % 20, Field2: 1}
x = s.it()
}
fmt.Println(x)
}
// this doesn't work
func BenchmarkStructGC(b *testing.B) {
x := int32(0)
gen := func(i int) *SomeStruct { return &SomeStruct{Field1: int32(i) % 20} }
for i := 0; i < b.N; i++ {
s := gen(i)
x = s.Field1
}
fmt.Println(x)
}
type TestField struct {
Name string
Value int32
}
func BenchmarkTypeAssertionMethodCall(b *testing.B) {
// This uses no interface.
b.Run("Int32().Int32() (no interface)", func(b *testing.B) {
var v Int32 = Int32(1)
x := int32(0)
for i := 0; i < b.N; i++ {
x += v.Int32()
}
})
// This calls a method on the interface.
// It's surprising that this is slower than switch concrete assert method
// by an order of magnitude when the alternative enables inlining.
// Perhaps go could do better by first grouping each interface into a
// single giant switch statement.
b.Run("BenchValue().Int32() (interface method)", func(b *testing.B) {
var v BenchValue = Int32(1)
x := int32(0)
for i := 0; i < b.N; i++ {
x += v.Int32()
}
})
// This type-asserts to a concrete type and calls its method.
b.Run("v.(Int32).Int32() (concrete assert method)", func(b *testing.B) {
var v interface{} = Int32(1)
x := int32(0)
for i := 0; i < b.N; i++ {
x += v.(Int32).Int32()
}
})
// This switch-type-asserts to a concrete type and calls its method.
// This actually ends up being the best choice, and is even faster than
// calling a method on an interface.
b.Run("case v.(Int32).Int32() (type switch concrete assert method)", func(b *testing.B) {
var v interface{} = Int32(1)
x := int32(0)
for i := 0; i < b.N; i++ {
switch v := v.(type) {
case Int32:
x += v.Int32()
case Int32a:
x += v.Int32() + 1
case Int32b:
x += v.Int32() + 2
case Int32c:
x += v.Int32() + 3
case Int32d:
x += v.Int32() + 4
case Int32e:
x += v.Int32() + 5
case Int32f:
x += v.Int32() + 6
case Int32g:
x += v.Int32() + 7
case Int32h:
x += v.Int32() + 8
case Int32i:
x += v.Int32() + 9
case Int32j:
x += v.Int32() + 10
case Int32k:
x += v.Int32() + 11
default:
panic("should not happen")
}
}
})
// This appears to run fast, not sure what optimization is happening,
// but maybe the initial interface setting is fine as the itable
// info is known statically.
b.Run("MyStruct{Value:Int32(i)} (struct interface field init)", func(b *testing.B) {
type MyStruct struct {
Value BenchValue
}
x := int32(0)
for i := 0; i < b.N; i++ {
s := MyStruct{Value: Int32(i)}
x += s.Value.(Int32).Int32()
}
})
// This type-asserts to an interface type and calls its method.
// v.(BenchValue) is super slow, see https://billglover.me/2018/09/17/how-expensive-is-a-go-function-call/
// or use `go tool compile -S test.go` for more info.
b.Run("v.(BenchValue).Int32() (interface assert method)", func(b *testing.B) {
var v interface{} = Int32(1)
x := int32(0)
for i := 0; i < b.N; i++ {
x += v.(BenchValue).Int32()
}
})
}
// there is a choice between type-switching on a slice of interfaces, or to
// iterate over a slice of super-structs.
func BenchmarkTypeSwitchOrCreate(b *testing.B) {
type Object interface {
}
type StructA struct {
Inner Object
A int
B int
}
type StructB struct {
C int
D int
}
x := make([]Object, 1000)
y := make([]StructA, 1000)
for i := 0; i < 1000; i++ {
x[i] = StructA{StructB{0, 0}, 0, 0}
y[i] = StructA{StructB{0, 0}, 0, 0}
}
c := 0
b.Run("type-switch", func(b *testing.B) {
for j := 0; j < b.N; j++ {
for i := 0; i < 1000; i++ {
switch xi := x[i].(type) {
case StructA:
switch xi.Inner.(type) {
case StructA:
panic("shouldn't happen")
case StructB:
c++
}
case StructB:
panic("shouldn't happen")
}
}
}
fmt.Println(c)
})
b.Run("super-struct", func(b *testing.B) {
for j := 0; j < b.N; j++ {
for i := 0; i < 1000; i++ {
switch y[i].Inner.(type) {
case StructA:
panic("shouldn't happen")
case StructB:
c++
}
}
}
fmt.Println(c)
})
}
func BenchmarkReflectValueOf(b *testing.B) {
var things = []interface{}{
int(0),
string(""),
struct{}{},
}
var rv reflect.Value
for _, thing := range things {
b.Run(reflect.TypeOf(thing).String(), func(b *testing.B) {
for i := 0; i < b.N; i++ {
rv = reflect.ValueOf(thing)
}
})
}
fmt.Println(rv)
}
func BenchmarkReflectAddInt64(b *testing.B) {
var rv reflect.Value = reflect.ValueOf(int64(1))
var x int64
for i := 0; i < b.N; i++ {
x += rv.Int()
}
fmt.Println(x)
}
func BenchmarkNativeAddInt64(b *testing.B) {
var x int64
for i := 0; i < b.N; i++ {
x += 1
}
fmt.Println(x)
}
func BenchmarkReflectTypeOf(b *testing.B) {
var x int64
var rt reflect.Type
for i := 0; i < b.N; i++ {
rt = reflect.TypeOf(x)
}
fmt.Println(x, rt)
}
func BenchmarkInterfaceEquality(b *testing.B) {
ctr := 0
var x interface{}
var y interface{}
for i := 0; i < b.N; i++ {
if x == y {
ctr++
}
}
fmt.Println(ctr)
}
func BenchmarkPointerEquality(b *testing.B) {
ctr := 0
a := 1
c := 2
x := &a
y := &c
for i := 0; i < b.N; i++ {
if x == y {
ctr++
}
}
fmt.Println(ctr)
}
func BenchmarkPointerDerefEquality(b *testing.B) {
ctr := 0
a := 1
c := 2
x := &a
y := &c
for i := 0; i < b.N; i++ {
if *x == *y {
ctr++
}
}
fmt.Println(ctr)
}
func BenchmarkArrayEquality(b *testing.B) {
b.Run("ArrayEquality[1]", func(b *testing.B) {
ctr := 0
x := [1]byte{0x00}
y := [1]byte{0x00}
for i := 0; i < b.N; i++ {
if x == y {
ctr++
}
}
fmt.Println(ctr)
})
b.Run("ArrayEquality[8]", func(b *testing.B) {
ctr := 0
x := [8]byte{}
y := [8]byte{}
for i := 0; i < b.N; i++ {
if x == y {
ctr++
}
}
fmt.Println(ctr)
})
b.Run("ArrayEquality[16]", func(b *testing.B) {
ctr := 0
x := [16]byte{}
y := [16]byte{}
for i := 0; i < b.N; i++ {
if x == y {
ctr++
}
}
fmt.Println(ctr)
})
b.Run("ArrayEquality[20]", func(b *testing.B) {
ctr := 0
x := [20]byte{}
y := [20]byte{}
for i := 0; i < b.N; i++ {
if x == y {
ctr++
}
}
fmt.Println(ctr)
})
b.Run("ArrayEquality[32]", func(b *testing.B) {
ctr := 0
x := [32]byte{}
y := [32]byte{}
for i := 0; i < b.N; i++ {
if x == y {
ctr++
}
}
fmt.Println(ctr)
})
b.Run("ArrayEquality[64]", func(b *testing.B) {
ctr := 0
x := [64]byte{}
y := [64]byte{}
for i := 0; i < b.N; i++ {
if x == y {
ctr++
}
}
fmt.Println(ctr)
})
b.Run("ArrayEquality[256]", func(b *testing.B) {
ctr := 0
x := [256]byte{}
y := [256]byte{}
for i := 0; i < b.N; i++ {
if x == y {
ctr++
}
}
fmt.Println(ctr)
})
}