-
Notifications
You must be signed in to change notification settings - Fork 0
/
intmap_test.go
208 lines (180 loc) · 4.89 KB
/
intmap_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
package goatlang
import (
"math/rand"
"reflect"
"testing"
)
// go test -bench=. -benchtime=100000000x -run Map
// go test -count 1 -run Map
var testIntMapData []int
const (
testIntIndexMask = 0xffff
testIntValueMask = 0xff
)
func init() {
for i := 0; i < 65536; i++ {
testIntMapData = append(testIntMapData, rand.Int()&testIntValueMask)
}
}
func TestIntMap(t *testing.T) {
im := newIntMap(256)
for i := 0; i < 16; i++ {
im.Set(1234, Int(42))
value, ok := im.Get(1234)
assert(t, "exists ok", ok, true)
assert(t, "exists value", value.Int(), 42)
im.Delete(1234)
value, ok = im.Get(1234)
assert(t, "deleted ok", ok, false)
assert(t, "deleted value", value.Int(), 0)
}
}
func TestIntMap_Assign(t *testing.T) {
im := newIntMap(256)
im.Set(1234, Int(32))
im.Assign(1234, Int(42))
im.Assign(4321, Int(52))
value, ok := im.Get(1234)
assert(t, "exists ok", ok, true)
assert(t, "exists value", value.Int(), 42)
_, ok = im.Get(4321)
assert(t, "not exist ok", ok, false)
collisionKey := 1234 + 512 // double map size
im.Assign(collisionKey, Int(62))
value, ok = im.Get(1234)
assert(t, "exists ok", ok, true)
assert(t, "exists value", value.Int(), 42)
_, ok = im.Get(collisionKey)
assert(t, "not exist ok", ok, false)
}
func TestIntMap_Copy(t *testing.T) {
m := newIntMap(0)
m.Set(1, Int(42))
c := m.Copy()
if !reflect.DeepEqual(c, m) {
t.Fatalf("copy is not equal")
}
m.Set(1, Int(43))
v, _ := c.Get(1)
if v.Int() != 42 {
t.Fatalf("copy is not distinct")
}
}
func TestIntMap_Mix(t *testing.T) {
im := newIntMap(0)
for n := 0; n < 65536; n += 4 {
im.Set(testIntMapData[n&testIntIndexMask], Int(testIntMapData[(n+1)&testIntIndexMask]))
_, _ = im.Get(testIntMapData[(n+2)&testIntIndexMask])
im.Delete(testIntMapData[(n+3)&testIntIndexMask])
}
gm := map[int]Value{}
for n := 0; n < 65536; n += 4 {
gm[testIntMapData[n&testIntIndexMask]] = Int(testIntMapData[(n+1)&testIntIndexMask])
_ = gm[testIntMapData[(n+2)&testIntIndexMask]]
delete(gm, testIntMapData[(n+3)&testIntIndexMask])
}
assert(t, "len", im.Len(), len(gm))
for k := 0; k <= testIntValueMask; k++ {
iv, iok := im.Get(k)
gv, gok := gm[k]
assert(t, "ok", iok, gok)
assert(t, "value", iv.Int(), gv.Int())
}
}
func BenchmarkIntMap_Set(b *testing.B) {
m := newIntMap(0)
for n := 0; n < b.N; n++ {
m.Set(testIntMapData[n&testIntIndexMask], Int(testIntMapData[(n+1)&testIntIndexMask]))
}
}
func BenchmarkIntMap_Get(b *testing.B) {
m := newIntMap(0)
for n := 0; n < b.N; n++ {
m.Set(testIntMapData[n&testIntIndexMask], Int(testIntMapData[(n+1)&testIntIndexMask]))
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
m.Get(testIntMapData[n&testIntIndexMask])
}
}
func BenchmarkIntMap_Mix(b *testing.B) {
m := newIntMap(0)
for n := 0; n < b.N; n += 4 {
m.Set(testIntMapData[n&testIntIndexMask], Int(testIntMapData[(n+1)&testIntIndexMask]))
m.Get(testIntMapData[(n+2)&testIntIndexMask])
m.Delete(testIntMapData[(n+3)&testIntIndexMask])
}
}
func BenchmarkIntMap_Delete(b *testing.B) {
m := newIntMap(0)
for n := 0; n < b.N; n++ {
m.Set(testIntMapData[n&testIntIndexMask], Int(testIntMapData[(n+1)&testIntIndexMask]))
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
m.Delete(testIntMapData[n&testIntIndexMask])
}
}
func BenchmarkGoMap_Set(b *testing.B) {
m := map[int]Value{}
for n := 0; n < b.N; n++ {
m[testIntMapData[n&testIntIndexMask]] = Int(testIntMapData[(n+1)&testIntIndexMask])
}
}
func BenchmarkGoMap_Get(b *testing.B) {
m := map[int]Value{}
for n := 0; n < b.N; n++ {
m[testIntMapData[n&testIntIndexMask]] = Int(testIntMapData[(n+1)&testIntIndexMask])
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
_ = m[testIntMapData[n&testIntIndexMask]]
}
}
func BenchmarkGoMap_Mix(b *testing.B) {
m := map[int]Value{}
for n := 0; n < b.N; n += 4 {
m[testIntMapData[n&testIntIndexMask]] = Int(testIntMapData[(n+1)&testIntIndexMask])
_ = m[testIntMapData[(n+2)&testIntIndexMask]]
delete(m, testIntMapData[(n+3)&testIntIndexMask])
}
}
func BenchmarkGoMap_Delete(b *testing.B) {
m := map[int]Value{}
for n := 0; n < b.N; n++ {
m[testIntMapData[n&testIntIndexMask]] = Int(testIntMapData[(n+1)&testIntIndexMask])
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
delete(m, testIntMapData[n&testIntIndexMask])
}
}
// func intMapHash(k int) int {
// https://en.wikipedia.org/wiki/Xorshift
// 32-bit
// k ^= k << 13
// k ^= k >> 17
// k ^= k << 5
// 64-bit
// k ^= k << 13
// k ^= k >> 7
// k ^= k << 17
// 16-bit http://www.retroprogramming.com/2017/07/xorshift-pseudorandom-numbers-in-z80.html
// k ^= k << 7
// k ^= k >> 9
// k ^= k << 8
// https://mostlymangling.blogspot.com/2019/12/stronger-better-morer-moremur-better.html
// k ^= k >> 27
// k *= 0x3C79AC492BA7B653
// k ^= k >> 33
// k *= 0x1C69B3F74AC4AE35
// k ^= k >> 27
// https://nullprogram.com/blog/2018/07/31/
// https://github.com/skeeto/hash-prospector
// k ^= k >> 16
// k *= 0x21f0aaad
// k ^= k >> 15
// k *= 0xd35a2d97
// k ^= k >> 16
// return k
// }