-
Notifications
You must be signed in to change notification settings - Fork 1
/
map_test.go
1466 lines (1330 loc) · 36.4 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
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package swisstable
import (
"flag"
"fmt"
"math"
"math/bits"
"math/rand"
"sort"
"testing"
"github.com/google/go-cmp/cmp"
)
var longTestFlag = flag.Bool("long", false, "run long benchmarks")
var coldMemTestFlag = flag.Float64("coldmem", 512, "memory in MB to use for cold memory tests. should be substantially larger than L3 cache.")
// TODO: 1000 is probably reasonable
var repFlag = flag.Int("rep", 200, "number of repetitions for some tests that are randomized")
func TestMap_Get(t *testing.T) {
tests := []struct {
name string
keys []Key
}{
{"one key", []Key{1}},
{"small, with one grow", list(0, 20, 1)},
{"small, with multiple grows", list(0, 111, 1)}, // from fuzzing
}
for _, tt := range tests {
t.Run(fmt.Sprintf(tt.name), func(t *testing.T) {
m := New(10)
m.hashFunc = identityHash
for _, k := range tt.keys {
m.Set(Key(k), Value(k))
}
gotLen := m.Len()
if gotLen != len(tt.keys) {
t.Errorf("Map.Len() = %d, want %d", gotLen, len(tt.keys))
}
for _, k := range tt.keys {
gotV, gotOk := m.Get(k)
if gotV != Value(k) || !gotOk {
t.Errorf("Map.Get(%v) = %v, %v. want = %v, true", k, gotV, gotOk, k)
}
}
notPresent := Key(1e12)
gotV, gotOk := m.Get(notPresent)
if gotV != 0 || gotOk {
t.Errorf("Map.Get(notPresent) = %v, %v. want = 0, false", gotV, gotOk)
}
})
}
}
func TestMap_Range(t *testing.T) {
tests := []struct {
name string
elems map[Key]Value
}{
{
"three elements",
map[Key]Value{
1: 2,
8: 8,
1e6: 1e10,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := New(256) // TODO: confirm this is probably 512 underlying table length?
for key, value := range tt.elems {
m.Set(key, value)
gotV, gotOk := m.Get(key)
if !gotOk {
t.Errorf("Map.Get() gotOk = %v, want true", gotOk)
}
if gotV != value {
t.Errorf("Map.Get() gotV = %v, want %v", gotV, value)
}
}
got := make(map[Key]Value)
m.Range(func(key Key, value Value) bool {
// validate we don't see the same key twice
_, ok := got[key]
if ok {
dumpFixedTables(m)
t.Errorf("Map.Range() key %v seen before", key)
}
got[key] = value
return true
})
// validate our returned key/values match what we put in
if diff := cmp.Diff(tt.elems, got); diff != "" {
t.Errorf("Map.Range() result mismatch (-want +got):\n%s", diff)
}
gotLen := m.Len()
if gotLen != len(tt.elems) {
t.Errorf("Map.Len() gotV = %v, want %v", gotLen, len(tt.elems))
}
})
}
}
func TestMap_Delete(t *testing.T) {
tests := []struct {
name string
capacity int
disableResizing bool
insert int
deleteFront int
deleteBack int
}{
{
name: "small, delete one",
disableResizing: false,
capacity: 256,
insert: 2,
deleteFront: 1,
deleteBack: 0,
},
{
name: "small, delete one after resizing",
disableResizing: false,
capacity: 10,
insert: 20, // this forces a resize
deleteFront: 0,
deleteBack: 0,
},
{
name: "delete ten after resizing",
disableResizing: false,
capacity: 256,
insert: 510, // this forces a resize
deleteFront: 0,
deleteBack: 10,
},
{
name: "delete ten force fill",
disableResizing: true,
capacity: 256,
insert: 510, // this is close to full
deleteFront: 0,
deleteBack: 10,
},
{
name: "delete all after resizing",
disableResizing: false,
capacity: 256,
insert: 511, // this forces a resize
deleteFront: 256,
deleteBack: 256,
},
{
name: "delete all force fill",
disableResizing: true,
capacity: 256,
insert: 511, // this is a force fill, leaving one empty slot
deleteFront: 256,
deleteBack: 256,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := New(tt.capacity)
want := make(map[Key]Value)
for i := 0; i < tt.insert; i++ {
m.Set(Key(i), Value(i))
want[Key(i)] = Value(i)
}
// // Delete a non-existent key
m.Delete(-1)
delete(want, -1)
// Delete requested keys
for i := 0; i < tt.deleteFront; i++ {
m.Delete(Key(i))
delete(want, Key(i))
}
for i := tt.insert - tt.deleteBack; i < tt.insert; i++ {
m.Delete(Key(i))
delete(want, Key(i))
}
got := make(map[Key]Value)
m.Range(func(key Key, value Value) bool {
// validate we don't see the same key twice
_, ok := got[key]
if ok {
t.Errorf("Map.Range() key %v seen twice", key)
}
got[key] = value
return true
})
if diff := cmp.Diff(want, got); diff != "" {
t.Logf("slots: %v", m.current.slots)
t.Errorf("Map.Range() result mismatch (-want +got):\n%s", diff)
}
gotLen := m.Len()
if gotLen != len(want) {
t.Errorf("Map.Len() gotV = %v, want %v", gotLen, len(want))
}
})
}
}
// TODO: force example of an *allowed* repeat key from a range, such as:
// https://go.dev/play/p/y8kvkPoNCv_H
// We can't quite create that same pattern with the current TestMap_RangeAddDelete,
// including the bulk add doesn't happen after all the preceding add/deletes.
func TestMap_RangeAddDelete(t *testing.T) {
tests := []struct {
name string
repeatAllowed bool // allow repeated key, such as if add X, del X, then add X while iterating
capacity int
start []Key
del []Key
add []Key
addBulk []Key // can be set up to trigger resize in middle of loop if desired
addBulk2 []Key
bulkIndex int // loop index in Map range to do the addBulk
}{
{
name: "small",
repeatAllowed: true, // this pattern could in theory trigger repeat key
capacity: 16,
start: []Key{1, 2, 3, 4},
del: []Key{3, 4},
add: []Key{5, 6, 4, 7},
addBulk: nil,
addBulk2: nil,
bulkIndex: 0,
},
{
name: "small with one grow",
repeatAllowed: false,
capacity: 8, // will be table len of 16
start: []Key{1, 2, 3, 4},
del: nil,
add: nil,
addBulk: list(5, 15, 1),
addBulk2: nil,
bulkIndex: 0,
},
{
name: "small with two grows",
repeatAllowed: false,
capacity: 8, // will be table len of 16
start: []Key{1, 2, 3, 4},
del: nil,
add: nil,
addBulk: list(5, 30, 1),
addBulk2: nil,
bulkIndex: 0,
},
{
name: "small, start iter mid-grow then grow",
repeatAllowed: false,
capacity: 8, // will be table len of 16
start: list(0, 53, 1),
del: nil,
add: nil,
addBulk: list(64, 128, 1),
addBulk2: nil,
bulkIndex: 0,
},
{
name: "medium",
repeatAllowed: true, // this pattern could in theory trigger repeat key
capacity: 650,
start: list(0, 500, 1),
del: list(10, 400, 1),
add: list(500, 650, 1),
addBulk: list(10, 400, 1),
addBulk2: []Key{},
bulkIndex: 400,
},
{
name: "medium, start iter mid-grow then grow",
repeatAllowed: false,
capacity: 8, // will be table len of 16
start: list(0, 417, 1), // trigger growth at 416
del: nil,
add: list(512, 950, 1),
addBulk: nil,
addBulk2: nil,
bulkIndex: 415,
},
{
name: "medium, start iter mid-grow, overlapping writes during iter", // from fuzzing
repeatAllowed: false,
capacity: 48,
start: list(48, 102, 1), // 54 elems, grow starts at 52
del: nil,
add: nil,
addBulk: list(11, 119, 1), // 108 elems, some overlapping
addBulk2: nil,
bulkIndex: 0,
},
{
name: "medium, two bulks adds",
repeatAllowed: true, // this pattern could in theory trigger repeat key
capacity: 650,
start: list(0, 300, 1),
del: list(0, 299, 1),
add: nil,
addBulk: list(1000, 1300, 1),
addBulk2: list(0, 300, 1),
bulkIndex: 256 + 8,
},
{
name: "medium, no del",
repeatAllowed: false,
capacity: 650,
start: list(0, 500, 1),
del: nil,
add: list(500, 650, 1),
addBulk: list(10, 400, 1),
addBulk2: nil,
bulkIndex: 400,
},
{
name: "medium, no add overlaps del",
repeatAllowed: false,
capacity: 650,
start: list(0, 500, 1),
del: list(10, 400, 1), // no add overlaps with what we delete
add: list(500, 650, 1),
addBulk: list(500, 800, 1),
addBulk2: nil,
bulkIndex: 400,
},
}
for _, tt := range tests {
tt := tt
for _, startCap := range []int{tt.capacity, 10, 20, 40, 52, 53, 54, 100, 1000} {
t.Run(fmt.Sprintf("%s, start cap %d", tt.name, startCap), func(t *testing.T) {
t.Parallel()
for rep := 0; rep < *repFlag; rep++ {
// Create the Map under test.
m := New(startCap)
m.seed = uintptr(rep)
// TODO:
// m.hashFunc = identityHash
// m.hashFunc = zeroHash
// // TODO: TEMP. get into subtest name
switch rep {
case 0:
m.hashFunc = identityHash
case 1:
// do this second (worse perf, even further from reality than identityHash)
m.hashFunc = zeroHash
default:
m.hashFunc = hashUint64 // real hash
}
for _, key := range tt.start {
m.Set(key, Value(key))
}
// Create some sets to dynamically track validity of keys that appear in a range
allowed := newKeySet(tt.start) // tracks start + added - deleted; these keys allowed but not required
mustSee := newKeySet(tt.start) // tracks start - deleted; these are keys we are required to see at some point
seen := newKeySet(nil) // use to verify no dups, and at end, used to verify mustSee
// Also dynamically track if key X is added, deleted, and then re-added during iteration,
// which means it is legal per Go spec to be seen again in the iteration.
// Example with stdlib map repeating keys during iter: https://go.dev/play/p/RN-v8rmQmeE
deleted := newKeySet(nil)
addedAfterDeleted := newKeySet(nil)
// during loop, verify no duplicate keys and we only see allowed keys.
// after loop, verify that we saw everything that we were required to see.
i := 0
m.Range(func(key Key, value Value) bool {
if seen.contains(key) {
if !tt.repeatAllowed {
t.Fatalf("Map.Range() key %v seen twice, unexpected for this test", key)
}
// Even though this pattern is generally allowed to have repeats,
// verify this specific key has been added, then deleted, then added,
// which means it is legal to see it later in the iteration after
// being re-added.
if !addedAfterDeleted.contains(key) {
t.Fatalf("Map.Range() key %v seen twice and was not re-added after being deleted", key)
}
}
seen.add(key)
if !allowed.contains(key) {
t.Fatalf("Map.Range() key %v seen but not allowed (e.g., might have been deleted, or never added)", key)
}
// Delete one key, if requested
if i < len(tt.del) {
k := tt.del[i]
m.Delete(k)
allowed.remove(k)
mustSee.remove(k) // We are no longer required to see this... It's ok if we saw it earlier
deleted.add(k)
if addedAfterDeleted.contains(k) {
addedAfterDeleted.remove(k)
}
}
set := func(k Key, v Value) {
m.Set(k, v) // TODO: not checking values. maybe different test?
allowed.add(k)
if deleted.contains(k) {
addedAfterDeleted.add(k)
deleted.remove(k)
}
}
// Add one key, if requested
if i < len(tt.add) {
set(tt.add[i], Value(i+1e6))
}
// Bulk add keys, if requested
if i == tt.bulkIndex {
for _, k := range tt.addBulk {
set(k, Value(i+1e9))
}
for _, k := range tt.addBulk2 {
set(k, Value(i+1e12))
}
}
i++
return true
})
for _, key := range mustSee.elems() {
if !seen.contains(key) {
dumpFixedTables(m)
t.Fatalf("Map.Range() expected key %v not seen. table size: %d grows: %d",
key, m.elemCount, m.resizeGenerations)
}
}
if !tt.repeatAllowed && addedAfterDeleted.len() > 0 {
// TODO: is this still working? Verfied once
// repeatAllowed could be inferred in theory,
// but keep it as extra sanity check and to be more explicit on expectations
t.Fatal("repeatAllowed incorrectly set to false")
}
}
})
}
}
}
// TestMap_IterGrowAndDelete is modeled after TestIterGrowAndDelete
// from runtime/map_test.go.
func TestMap_IterGrowAndDelete(t *testing.T) {
m := New(16) // will resize
for i := 0; i < 100; i++ {
m.Set(Key(i), Value(i))
}
growflag := true
m.Range(func(key Key, value Value) bool {
if growflag {
// grow the table
for i := 100; i < 1000; i++ {
m.Set(Key(i), Value(i))
}
// delete all odd keys
for i := 1; i < 1000; i += 2 {
m.Delete(Key(i))
}
growflag = false
} else {
if key&1 == 1 {
t.Errorf("odd value returned %d", key)
}
}
return true
})
}
func TestMap_StoredKeys(t *testing.T) {
// TODO: probably make helper?
list := func(start, end Key) []Key {
var res []Key
for i := start; i < end; i++ {
res = append(res, i)
}
return res
}
storedKeys := func(m *Map) []Key {
// reach into the implementation to return
// keys in stored order
if m.old != nil {
panic("unexpectedly growing")
}
var keys []Key
for i := range m.current.control {
if isStored(m.current.control[i]) {
keys = append(keys, m.current.slots[i].Key)
}
}
return keys
}
tests := []struct {
name string
capacity int
start []Key
del []Key
add []Key
want []Key
}{
{
name: "delete key, add different key, 1 group",
capacity: 8, // ends up with 16 slots
start: []Key{0, 1, 2, 3},
del: []Key{2},
add: []Key{42}, // the slot that had 2 is replaced with 42
want: []Key{0, 1, 42, 3},
},
{
name: "delete key 1st group, add different key, 2 groups",
capacity: 16, // ends up with 32 slots
start: list(0, 20), // [0, 20)
del: []Key{2},
add: []Key{42}, // the DELETED slot that had 2 is replaced with 42
want: append([]Key{0, 1, 42}, list(3, 20)...),
},
{
name: "delete key 1st group, set key present in 2nd group",
capacity: 16, // ends up with 32 slots
start: list(0, 20), // [0, 20)
del: []Key{2},
add: []Key{19}, // should end up with single 19, still in the second group
want: append([]Key{0, 1}, list(3, 20)...),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create the Map under test.
m := New(tt.capacity)
// Reach into the implementation to force a terrible hash func,
// which lets us more predictably place elems.
hashToZero := func(k Key, seed uintptr) uint64 {
// could do something like: return uint64(k << m.current.h2Shift)
return 0
}
m.hashFunc = hashToZero
// Apply our operations
for _, key := range tt.start {
m.Set(key, Value(key))
}
for _, key := range tt.del {
m.Delete(key)
}
for _, key := range tt.add {
m.Set(key, Value(key))
}
got := storedKeys(m)
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Logf("got: %v", got)
t.Errorf("stored keys mismatch (-want +got):\n%s", diff)
}
})
}
}
func TestMap_ForceFill(t *testing.T) {
tests := []struct {
elem KV
}{
{KV{Key: 1, Value: 2}},
{KV{Key: 8, Value: 8}},
{KV{Key: 1e6, Value: 1e10}},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("get key %d", tt.elem.Key), func(t *testing.T) {
size := 10_000
m := New(size)
m.disableResizing = true
// TODO: this is true for sparsehash, but not our swisstable,
// which sizes the underlying table slices to roundPow2(1/0.8) times the requested capacity.
// TODO: also, might no longer be true for sparestable, either.
// TODO: reach in to disable growth?
// We reach into the implementation to see what full means.
underlyingTableLen := len(m.current.slots)
t.Logf("setting %d elements in table with underlying size %d", underlyingTableLen-1, underlyingTableLen)
// Force the underlying table to fill up the map so that it only has one empty slot left,
// without any resizing. This helps verify our triangular numbers are correct and
// we cycle properly. We also do this in a loop (so we set the same values repeatedly)
// in order to slightly stress things a bit more.
for i := 0; i < 100; i++ {
for j := 1000; j < 1000+underlyingTableLen-1; j++ {
m.Set(Key(j), Value(j))
}
}
// Confirm it is nearly 100% full, with only room for one more
gotLen := m.Len()
if gotLen != underlyingTableLen-1 {
t.Errorf("Map.Len gotLen = %v, want %v", gotLen, underlyingTableLen-1)
}
missingKey := Key(1e12)
gotV, gotOk := m.Get(missingKey)
if gotOk {
t.Errorf("Map.Get(missingKey) gotOk = %v, want false", gotOk)
}
if gotV != 0 {
t.Errorf("Map.Get(missingKey) gotV = %v, want %v", gotV, 0)
}
// Set one more value, which should make our table 100% full,
// and confirm we can get it back.
m.Set(tt.elem.Key, tt.elem.Value)
gotV, gotOk = m.Get(tt.elem.Key)
if !gotOk {
t.Errorf("Map.Get(%d) gotOk = %v, want true", tt.elem.Key, gotOk)
}
if gotV != tt.elem.Value {
t.Errorf("Map.Get(%d) gotV = %v, want %v", tt.elem.Key, gotV, tt.elem.Value)
}
// Confirm it is 100% full according to the public API.
gotLen = m.Len()
if gotLen != underlyingTableLen {
t.Errorf("Map.Len gotLen = %v, want %v", gotLen, underlyingTableLen)
}
// Reach in to the impl and to confirm that it is indeed seem to be 100% full
for i := 0; i < len(m.current.control); i++ {
if m.current.control[i] == emptySentinel {
t.Fatalf("control byte %d is empty", i)
}
}
for i := 0; i < len(m.current.slots); i++ {
if m.current.slots[i].Key == 0 || m.current.slots[i].Value == 0 {
// We set everything to non-zero values above.
t.Fatalf("element at index %d has key or value that is still 0: key = %d value = %d",
i, m.current.slots[i].Key, m.current.slots[i].Value)
}
}
})
}
}
func Test_StatusByte(t *testing.T) {
// probably/hopefully overkill
b := byte(0)
if isEvacuated(b) || isChainEvacuated(b) || curHasDisplaced(b) {
t.Errorf("statusByte unexpectedly set")
}
got := setEvacuated(b)
if !isEvacuated(got) {
t.Errorf("isEvacuated() = false, got = %v", got)
}
if isChainEvacuated(got) {
t.Errorf("isChainEvacuated() = true")
}
if curHasDisplaced(got) {
t.Errorf("curHasDisplaced() = true")
}
got = setChainEvacuated(b)
if isEvacuated(got) {
t.Errorf("isEvacuated() = true")
}
if !isChainEvacuated(got) {
t.Errorf("isChainEvacuated() = false, got = %v", got)
}
if curHasDisplaced(got) {
t.Errorf("curHasDisplaced() = true")
}
got = setCurHasDisplaced(b)
if isEvacuated(got) {
t.Errorf("isEvacuated() = true")
}
if isChainEvacuated(got) {
t.Errorf("isChainEvacuated() = true")
}
if !curHasDisplaced(got) {
t.Errorf("curHasDisplaced() = false, got = %v", got)
}
}
func BenchmarkMatchByte(b *testing.B) {
buffer := make([]byte, 16)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = MatchByte(42, buffer)
}
}
func BenchmarkFillGrow_Swiss(b *testing.B) {
bms := almostGrowPointMapSizes([]int{
1 << 10,
1 << 20,
1 << 23,
})
if !*longTestFlag {
bms = []benchmark{
{"map size 1000000", 1_000_000},
}
}
for _, bm := range bms {
b.Run(bm.name, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
m := New(10)
for j := Key(0); j < Key(bm.mapElements); j++ {
m.Set(j, Value(j))
}
}
})
}
}
func BenchmarkFillGrow_Std(b *testing.B) {
bms := almostGrowPointMapSizes([]int{
1 << 10,
1 << 20,
1 << 23,
})
if !*longTestFlag {
bms = []benchmark{
{"map size 1000000", 1_000_000},
}
}
for _, bm := range bms {
b.Run(bm.name, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
m := make(map[int64]int64, 10)
for j := int64(0); j < int64(bm.mapElements); j++ {
m[j] = j
}
}
})
}
}
func BenchmarkFillPresize_Swiss(b *testing.B) {
bms := almostGrowPointMapSizes([]int{
1 << 10,
1 << 20,
1 << 23,
})
if !*longTestFlag {
bms = []benchmark{
{"map size 1000000", 1_000_000},
}
}
for _, bm := range bms {
b.Run(bm.name, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
m := New(bm.mapElements)
for j := Key(0); j < Key(bm.mapElements); j++ {
m.Set(j, Value(j))
}
}
})
}
}
func BenchmarkFillPresize_Std(b *testing.B) {
bms := almostGrowPointMapSizes([]int{
1 << 10,
1 << 20,
1 << 23,
})
if !*longTestFlag {
bms = []benchmark{
{"map size 1000000", 1_000_000},
}
}
for _, bm := range bms {
b.Run(bm.name, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
m := make(map[int64]int64, bm.mapElements)
for j := int64(0); j < int64(bm.mapElements); j++ {
m[j] = j
}
}
})
}
}
// TODO: probably change over to sinkKey, sinkValue, and use map[Key]Value as the runtime maps
var sinkUint uint64
var sinkInt int64
var sinkValue Value
var sinkBool bool
func BenchmarkGetHitHot_Swiss(b *testing.B) {
hotKeyCount := 20
lookupEachKey := 50
bms := almostGrowPointMapSizes([]int{
1 << 10,
1 << 20,
1 << 23,
})
if !*longTestFlag {
bms = []benchmark{
{"map size 1000000", 1_000_000},
}
}
for _, bm := range bms {
b.Run(bm.name, func(b *testing.B) {
// Fill the map under test
m := New(bm.mapElements)
for i := Key(0); i < Key(bm.mapElements); i++ {
m.Set(i, Value(i))
}
// Generate random hot keys repeated N times then shuffled
var hotKeys []Key
for i := 0; i < hotKeyCount; i++ {
hotKeys = append(hotKeys, Key(rand.Intn(bm.mapElements)))
}
var gets []Key
for i := 0; i < hotKeyCount; i++ {
k := hotKeys[i]
for j := 0; j < lookupEachKey; j++ {
gets = append(gets, k)
}
}
rand.Shuffle(len(gets), func(i, j int) {
gets[i], gets[j] = gets[j], gets[i]
})
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, key := range gets {
v, b := m.Get(key)
sinkInt = int64(v)
sinkBool = b
}
}
})
}
}
func BenchmarkGetHitHot_Std(b *testing.B) {
hotKeyCount := 20
lookupEachKey := 50
bms := almostGrowPointMapSizes([]int{
1 << 10,
1 << 20,
1 << 23,
})
if !*longTestFlag {
bms = []benchmark{
{"map size 1000000", 1_000_000},
}
}
for _, bm := range bms {
b.Run(bm.name, func(b *testing.B) {
// Fill the map under test
m := make(map[int64]int64, bm.mapElements)
for i := 0; i < bm.mapElements; i++ {
m[int64(i)] = int64(i)
}
// Generate random hot keys repeated N times then shuffled
var hotKeys []int64
for i := 0; i < hotKeyCount; i++ {
hotKeys = append(hotKeys, int64(rand.Intn(bm.mapElements)))
}
var gets []int64
for i := 0; i < hotKeyCount; i++ {
k := hotKeys[i]
for j := 0; j < lookupEachKey; j++ {
gets = append(gets, k)
}
}
rand.Shuffle(len(gets), func(i, j int) {
gets[i], gets[j] = gets[j], gets[i]
})
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, key := range gets {
sinkInt, sinkBool = m[key]
}
}
})
}
}
func BenchmarkGetMissHot_Swiss(b *testing.B) {
hotKeyCount := 20
lookupEachKey := 50
bms := almostGrowPointMapSizes([]int{
1 << 10,
1 << 20,
1 << 23,
})
if !*longTestFlag {
bms = []benchmark{
{"map size 1000000", 1_000_000},
}
}
for _, bm := range bms {
b.Run(bm.name, func(b *testing.B) {
// Fill the map under test
m := New(bm.mapElements)
for i := Key(0); i < Key(bm.mapElements); i++ {
m.Set(i, Value(i))
}
// Generate keys that don't exist, repeated N times then shuffled
var missKeys []Key
for i := 0; i < hotKeyCount; i++ {
missKeys = append(missKeys, Key(i+(1<<40)))
}
var gets []Key
for i := 0; i < hotKeyCount; i++ {
k := missKeys[i]
for j := 0; j < lookupEachKey; j++ {
gets = append(gets, k)
}
}
rand.Shuffle(len(gets), func(i, j int) {
gets[i], gets[j] = gets[j], gets[i]
})
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, key := range gets {
v, b := m.Get(key)
sinkInt = int64(v)
sinkBool = b
}
}
})
}
}
func BenchmarkGetMissHot_Std(b *testing.B) {
hotKeyCount := 20
lookupEachKey := 50
bms := almostGrowPointMapSizes([]int{
1 << 10,
1 << 20,
1 << 23,
})
if !*longTestFlag {
bms = []benchmark{
{"map size 1000000", 1_000_000},
}
}
for _, bm := range bms {
b.Run(bm.name, func(b *testing.B) {
// Fill the map under test
m := make(map[int64]int64, bm.mapElements)
for i := 0; i < bm.mapElements; i++ {
m[int64(i)] = int64(i)
}