7
7
8
8
"github.com/alphadose/haxmap"
9
9
"github.com/cornelk/hashmap"
10
+ "github.com/puzpuzpuz/xsync/v2"
10
11
)
11
12
12
13
const (
@@ -30,14 +31,22 @@ func setupGoSyncMap() *sync.Map {
30
31
return m
31
32
}
32
33
33
- func setupCornelkMap (b * testing. B ) * hashmap.Map [uintptr , uintptr ] {
34
+ func setupCornelkMap () * hashmap.Map [uintptr , uintptr ] {
34
35
m := hashmap.NewSized [uintptr , uintptr ](mapSize )
35
36
for i := uintptr (0 ); i < epochs ; i ++ {
36
37
m .Set (i , i )
37
38
}
38
39
return m
39
40
}
40
41
42
+ func setupXsyncMap () * xsync.MapOf [uintptr , uintptr ] {
43
+ m := xsync .NewIntegerMapOf [uintptr , uintptr ]()
44
+ for i := uintptr (0 ); i < epochs ; i ++ {
45
+ m .Store (i , i )
46
+ }
47
+ return m
48
+ }
49
+
41
50
func BenchmarkHaxMapReadsOnly (b * testing.B ) {
42
51
m := setupHaxMap ()
43
52
b .ResetTimer ()
@@ -119,7 +128,7 @@ func BenchmarkGoSyncMapReadsWithWrites(b *testing.B) {
119
128
}
120
129
121
130
func BenchmarkCornelkMapReadsOnly (b * testing.B ) {
122
- m := setupCornelkMap (b )
131
+ m := setupCornelkMap ()
123
132
b .ResetTimer ()
124
133
b .RunParallel (func (pb * testing.PB ) {
125
134
for pb .Next () {
@@ -134,7 +143,7 @@ func BenchmarkCornelkMapReadsOnly(b *testing.B) {
134
143
}
135
144
136
145
func BenchmarkCornelkMapReadsWithWrites (b * testing.B ) {
137
- m := setupCornelkMap (b )
146
+ m := setupCornelkMap ()
138
147
var writer uintptr
139
148
b .ResetTimer ()
140
149
b .RunParallel (func (pb * testing.PB ) {
@@ -157,3 +166,43 @@ func BenchmarkCornelkMapReadsWithWrites(b *testing.B) {
157
166
}
158
167
})
159
168
}
169
+
170
+ func BenchmarkXsyncMapReadsOnly (b * testing.B ) {
171
+ m := setupXsyncMap ()
172
+ b .ResetTimer ()
173
+ b .RunParallel (func (pb * testing.PB ) {
174
+ for pb .Next () {
175
+ for i := uintptr (0 ); i < epochs ; i ++ {
176
+ j , _ := m .Load (i )
177
+ if j != i {
178
+ b .Fail ()
179
+ }
180
+ }
181
+ }
182
+ })
183
+ }
184
+
185
+ func BenchmarkXsyncMapReadsWithWrites (b * testing.B ) {
186
+ m := setupXsyncMap ()
187
+ var writer uintptr
188
+ b .ResetTimer ()
189
+ b .RunParallel (func (pb * testing.PB ) {
190
+ // use 1 thread as writer
191
+ if atomic .CompareAndSwapUintptr (& writer , 0 , 1 ) {
192
+ for pb .Next () {
193
+ for i := uintptr (0 ); i < epochs ; i ++ {
194
+ m .Store (i , i )
195
+ }
196
+ }
197
+ } else {
198
+ for pb .Next () {
199
+ for i := uintptr (0 ); i < epochs ; i ++ {
200
+ j , _ := m .Load (i )
201
+ if j != i {
202
+ b .Fail ()
203
+ }
204
+ }
205
+ }
206
+ }
207
+ })
208
+ }
0 commit comments