-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_test.go
82 lines (70 loc) · 1.54 KB
/
map_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
package xatomic
import (
"testing"
"unsafe"
"github.com/stretchr/testify/require"
)
type hmap struct{}
//go:linkname hmap_incrnoverflow runtime.(*hmap).incrnoverflow
func hmap_incrnoverflow(h *hmap)
func TestMapIsPointer(t *testing.T) {
m := map[int]int{
1: 1,
2: 2,
}
t.Run("Store&Load", func(t *testing.T) {
var storage map[int]int
StoreMap(&storage, m)
require.Equal(t, m, storage)
v := LoadMap(&storage)
require.Equal(t, m, v)
})
// The test above should be good enough, but just in case some additional tests
// go below:
t.Run("call_internal_method", func(t *testing.T) {
// Rechecking the assumption that `map` is actually a pointer
// (to the map header structure).
//
// Assuming that it may segfault or something, if something is wrong:
hdr := (*hmap)(unref((unsafe.Pointer)(&m)))
hmap_incrnoverflow(hdr)
})
}
var v map[int]int
var r map[int]int
func Benchmark(b *testing.B) {
m := map[int]int{
1: 1,
2: 2,
}
b.Run("noop", func(b *testing.B) {
for i := 0; i < b.N; i++ {
}
})
b.Run("Map", func(b *testing.B) {
b.Run("Store", func(b *testing.B) {
b.Run("atomic", func(b *testing.B) {
for i := 0; i < b.N; i++ {
StoreMap(&v, m)
}
})
b.Run("unatomic", func(b *testing.B) {
for i := 0; i < b.N; i++ {
v = m
}
})
})
b.Run("Load", func(b *testing.B) {
b.Run("atomic", func(b *testing.B) {
for i := 0; i < b.N; i++ {
r = LoadMap(&v)
}
})
b.Run("unatomic", func(b *testing.B) {
for i := 0; i < b.N; i++ {
r = v
}
})
})
})
}