1
2
3
4
5 package test
6
7 import "testing"
8
9
10 type moveLoadBenchHandle[T any] struct {
11 value *T
12 }
13
14 func (h moveLoadBenchHandle[T]) Value() T {
15 return *h.value
16 }
17
18 type moveLoadBenchBig struct {
19 typ int8
20 index int64
21 str string
22 pkgID string
23 dummyField [1024]byte
24 }
25
26 type moveLoadBenchS struct {
27 h moveLoadBenchHandle[moveLoadBenchBig]
28 }
29
30 var moveLoadBenchSink int8
31
32 func moveLoadBenchTypViaValue(s moveLoadBenchS) int8 {
33 return s.h.Value().typ
34 }
35
36 func moveLoadBenchTypViaPtr(s moveLoadBenchS) int8 {
37 return (*s.h.value).typ
38 }
39
40 func benchmarkMoveLoad(b *testing.B, f func(moveLoadBenchS) int8) {
41 backing := make([]moveLoadBenchBig, 1<<10)
42 ss := make([]moveLoadBenchS, len(backing))
43 for i := range backing {
44 backing[i].typ = int8(i)
45 ss[i] = moveLoadBenchS{h: moveLoadBenchHandle[moveLoadBenchBig]{&backing[i]}}
46 }
47
48 b.ResetTimer()
49 var x int8
50 for i := 0; i < b.N; i++ {
51 x += f(ss[i&(len(ss)-1)])
52 }
53 moveLoadBenchSink = x
54 }
55
56 func BenchmarkMoveLoadTypViaValue(b *testing.B) {
57 benchmarkMoveLoad(b, moveLoadBenchTypViaValue)
58 }
59
60 func BenchmarkMoveLoadTypViaPtr(b *testing.B) {
61 benchmarkMoveLoad(b, moveLoadBenchTypViaPtr)
62 }
63
View as plain text