-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathmap.go
541 lines (488 loc) · 15.3 KB
/
map.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
package haxmap
import (
"encoding/json"
"reflect"
"sort"
"strconv"
"sync/atomic"
"unsafe"
"golang.org/x/exp/constraints"
)
const (
// defaultSize is the default size for a zero allocated map
defaultSize = 8
// maxFillRate is the maximum fill rate for the slice before a resize will happen
maxFillRate = 50
// intSizeBytes is the size in byte of an int or uint value
intSizeBytes = strconv.IntSize >> 3
)
// indicates resizing operation status enums
const (
notResizing uint32 = iota
resizingInProgress
)
type (
hashable interface {
constraints.Integer | constraints.Float | constraints.Complex | ~string | uintptr | ~unsafe.Pointer
}
// metadata of the hashmap
metadata[K hashable, V any] struct {
keyshifts uintptr // array_size - log2(array_size)
count atomicUintptr // number of filled items
data unsafe.Pointer // pointer to array of map indexes
// use a struct element with generic params to enable monomorphization (generic code copy-paste) for the parent metadata struct by golang compiler leading to best performance (truly hax)
// else in other cases the generic params will be unnecessarily passed as function parameters everytime instead of monomorphization leading to slower performance
index []*element[K, V]
}
// Map implements the concurrent hashmap
Map[K hashable, V any] struct {
listHead *element[K, V] // Harris lock-free list of elements in ascending order of hash
hasher func(K) uintptr
metadata atomicPointer[metadata[K, V]] // atomic.Pointer for safe access even during resizing
resizing atomicUint32
numItems atomicUintptr
defaultSize uintptr
}
// used in deletion of map elements
deletionRequest[K hashable] struct {
keyHash uintptr
key K
}
)
// New returns a new HashMap instance with an optional specific initialization size
func New[K hashable, V any](size ...uintptr) *Map[K, V] {
m := &Map[K, V]{listHead: newListHead[K, V]()}
m.numItems.Store(0)
m.defaultSize = defaultSize
if len(size) > 0 && size[0] > 0 {
m.defaultSize = size[0]
}
m.allocate(m.defaultSize)
m.setDefaultHasher()
return m
}
// Del deletes key/keys from the map
// Bulk deletion is more efficient than deleting keys one by one
func (m *Map[K, V]) Del(keys ...K) {
size := len(keys)
switch {
case size == 0:
return
case size == 1: // delete one
var (
h = m.hasher(keys[0])
existing = m.metadata.Load().indexElement(h)
)
if existing == nil || existing.keyHash > h {
existing = m.listHead.next()
}
for ; existing != nil && existing.keyHash <= h; existing = existing.next() {
if existing.key == keys[0] {
if existing.remove() { // mark node for lazy removal on next pass
m.removeItemFromIndex(existing) // remove node from map index
}
return
}
}
default: // delete multiple entries
var (
delQ = make([]deletionRequest[K], size)
iter = 0
)
for idx := 0; idx < size; idx++ {
delQ[idx].keyHash, delQ[idx].key = m.hasher(keys[idx]), keys[idx]
}
// sort in ascending order of keyhash
sort.Slice(delQ, func(i, j int) bool {
return delQ[i].keyHash < delQ[j].keyHash
})
elem := m.metadata.Load().indexElement(delQ[0].keyHash)
if elem == nil || elem.keyHash > delQ[0].keyHash {
elem = m.listHead.next()
}
for elem != nil && iter < size {
if elem.keyHash == delQ[iter].keyHash && elem.key == delQ[iter].key {
if elem.remove() { // mark node for lazy removal on next pass
m.removeItemFromIndex(elem) // remove node from map index
}
iter++
elem = elem.next()
} else if elem.keyHash > delQ[iter].keyHash {
iter++
} else {
elem = elem.next()
}
}
}
}
// Get retrieves an element from the map
// returns `false“ if element is absent
func (m *Map[K, V]) Get(key K) (value V, ok bool) {
h := m.hasher(key)
// inline search
for elem := m.metadata.Load().indexElement(h); elem != nil && elem.keyHash <= h; elem = elem.nextPtr.Load() {
if elem.key == key {
value, ok = *elem.value.Load(), !elem.isDeleted()
return
}
}
ok = false
return
}
// Set tries to update an element if key is present else it inserts a new element
// If a resizing operation is happening concurrently while calling Set()
// then the item might show up in the map only after the resize operation is finished
func (m *Map[K, V]) Set(key K, value V) {
var (
h = m.hasher(key)
valPtr = &value
alloc *element[K, V]
created = false
data = m.metadata.Load()
existing = data.indexElement(h)
)
if existing == nil || existing.keyHash > h {
existing = m.listHead
}
if alloc, created = existing.inject(h, key, valPtr); alloc != nil {
if created {
m.numItems.Add(1)
}
} else {
for existing = m.listHead; alloc == nil; alloc, created = existing.inject(h, key, valPtr) {
}
if created {
m.numItems.Add(1)
}
}
count := data.addItemToIndex(alloc)
if resizeNeeded(uintptr(len(data.index)), count) && m.resizing.CompareAndSwap(notResizing, resizingInProgress) {
m.grow(0) // double in size
}
}
// GetOrSet returns the existing value for the key if present
// Otherwise, it stores and returns the given value
// The loaded result is true if the value was loaded, false if stored
func (m *Map[K, V]) GetOrSet(key K, value V) (actual V, loaded bool) {
var (
h = m.hasher(key)
data = m.metadata.Load()
existing = data.indexElement(h)
)
// try to get the element if present
for elem := existing; elem != nil && elem.keyHash <= h; elem = elem.nextPtr.Load() {
if elem.key == key && !elem.isDeleted() {
actual, loaded = *elem.value.Load(), true
return
}
}
// Get() failed because element is absent
// store the value given by user
actual, loaded = value, false
var (
alloc *element[K, V]
created = false
valPtr = &value
)
if existing == nil || existing.keyHash > h {
existing = m.listHead
}
if alloc, created = existing.inject(h, key, valPtr); alloc != nil {
if created {
m.numItems.Add(1)
}
} else {
for existing = m.listHead; alloc == nil; alloc, created = existing.inject(h, key, valPtr) {
}
if created {
m.numItems.Add(1)
}
}
count := data.addItemToIndex(alloc)
if resizeNeeded(uintptr(len(data.index)), count) && m.resizing.CompareAndSwap(notResizing, resizingInProgress) {
m.grow(0) // double in size
}
return
}
// GetOrCompute is similar to GetOrSet but the value to be set is obtained from a constructor
// the value constructor is called only once
func (m *Map[K, V]) GetOrCompute(key K, valueFn func() V) (actual V, loaded bool) {
var (
h = m.hasher(key)
data = m.metadata.Load()
existing = data.indexElement(h)
)
// try to get the element if present
for elem := existing; elem != nil && elem.keyHash <= h; elem = elem.nextPtr.Load() {
if elem.key == key && !elem.isDeleted() {
actual, loaded = *elem.value.Load(), true
return
}
}
// Get() failed because element is absent
// compute the value from the constructor and store it
value := valueFn()
actual, loaded = value, false
var (
alloc *element[K, V]
created = false
valPtr = &value
)
if existing == nil || existing.keyHash > h {
existing = m.listHead
}
if alloc, created = existing.inject(h, key, valPtr); alloc != nil {
if created {
m.numItems.Add(1)
}
} else {
for existing = m.listHead; alloc == nil; alloc, created = existing.inject(h, key, valPtr) {
}
if created {
m.numItems.Add(1)
}
}
count := data.addItemToIndex(alloc)
if resizeNeeded(uintptr(len(data.index)), count) && m.resizing.CompareAndSwap(notResizing, resizingInProgress) {
m.grow(0) // double in size
}
return
}
// GetAndDel deletes the key from the map, returning the previous value if any.
func (m *Map[K, V]) GetAndDel(key K) (value V, ok bool) {
var (
h = m.hasher(key)
existing = m.metadata.Load().indexElement(h)
)
if existing == nil || existing.keyHash > h {
existing = m.listHead.next()
}
for ; existing != nil && existing.keyHash <= h; existing = existing.next() {
if existing.key == key {
value, ok = *existing.value.Load(), !existing.isDeleted()
if existing.remove() {
m.removeItemFromIndex(existing)
}
return
}
}
return
}
// CompareAndSwap atomically updates a map entry given its key by comparing current value to `oldValue`
// and setting it to `newValue` if the above comparison is successful
// It returns a boolean indicating whether the CompareAndSwap was successful or not
func (m *Map[K, V]) CompareAndSwap(key K, oldValue, newValue V) bool {
var (
h = m.hasher(key)
existing = m.metadata.Load().indexElement(h)
)
if existing == nil || existing.keyHash > h {
existing = m.listHead
}
if _, current, _ := existing.search(h, key); current != nil {
if oldPtr := current.value.Load(); reflect.DeepEqual(*oldPtr, oldValue) {
return current.value.CompareAndSwap(oldPtr, &newValue)
}
}
return false
}
// Swap atomically swaps the value of a map entry given its key
// It returns the old value if swap was successful and a boolean `swapped` indicating whether the swap was successful or not
func (m *Map[K, V]) Swap(key K, newValue V) (oldValue V, swapped bool) {
var (
h = m.hasher(key)
existing = m.metadata.Load().indexElement(h)
)
if existing == nil || existing.keyHash > h {
existing = m.listHead
}
if _, current, _ := existing.search(h, key); current != nil {
oldValue, swapped = *current.value.Swap(&newValue), true
} else {
swapped = false
}
return
}
// ForEach iterates over key-value pairs and executes the lambda provided for each such pair
// lambda must return `true` to continue iteration and `false` to break iteration
func (m *Map[K, V]) ForEach(lambda func(K, V) bool) {
for item := m.listHead.next(); item != nil && lambda(item.key, *item.value.Load()); item = item.next() {
}
}
// Grow resizes the hashmap to a new size, gets rounded up to next power of 2
// To double the size of the hashmap use newSize 0
// No resizing is done in case of another resize operation already being in progress
// Growth and map bucket policy is inspired from https://github.com/cornelk/hashmap
func (m *Map[K, V]) Grow(newSize uintptr) {
if m.resizing.CompareAndSwap(notResizing, resizingInProgress) {
m.grow(newSize)
}
}
// Clear the map by removing all entries in the map.
// This operation resets the underlying metadata to its initial state.
func (m *Map[K, V]) Clear() {
index := make([]*element[K, V], m.defaultSize)
header := (*reflect.SliceHeader)(unsafe.Pointer(&index))
newdata := &metadata[K, V]{
keyshifts: strconv.IntSize - log2(m.defaultSize),
data: unsafe.Pointer(header.Data),
index: index,
}
m.listHead.nextPtr.Store(nil)
m.metadata.Store(newdata)
m.numItems.Store(0)
}
// SetHasher sets the hash function to the one provided by the user
func (m *Map[K, V]) SetHasher(hs func(K) uintptr) {
m.hasher = hs
}
// Len returns the number of key-value pairs within the map
func (m *Map[K, V]) Len() uintptr {
return m.numItems.Load()
}
// Fillrate returns the fill rate of the map as an percentage integer
func (m *Map[K, V]) Fillrate() uintptr {
data := m.metadata.Load()
return (data.count.Load() * 100) / uintptr(len(data.index))
}
// MarshalJSON implements the json.Marshaler interface.
func (m *Map[K, V]) MarshalJSON() ([]byte, error) {
gomap := make(map[K]V)
for i := m.listHead.next(); i != nil; i = i.next() {
gomap[i.key] = *i.value.Load()
}
return json.Marshal(gomap)
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (m *Map[K, V]) UnmarshalJSON(i []byte) error {
gomap := make(map[K]V)
err := json.Unmarshal(i, &gomap)
if err != nil {
return err
}
for k, v := range gomap {
m.Set(k, v)
}
return nil
}
// allocate map with the given size
func (m *Map[K, V]) allocate(newSize uintptr) {
if m.resizing.CompareAndSwap(notResizing, resizingInProgress) {
m.grow(newSize)
}
}
// fillIndexItems re-indexes the map given the latest state of the linked list
func (m *Map[K, V]) fillIndexItems(mapData *metadata[K, V]) {
var (
first = m.listHead.next()
item = first
lastIndex = uintptr(0)
)
for item != nil {
index := item.keyHash >> mapData.keyshifts
if item == first || index != lastIndex {
mapData.addItemToIndex(item)
lastIndex = index
}
item = item.next()
}
}
// removeItemFromIndex removes an item from the map index
func (m *Map[K, V]) removeItemFromIndex(item *element[K, V]) {
for {
data := m.metadata.Load()
index := item.keyHash >> data.keyshifts
ptr := (*unsafe.Pointer)(unsafe.Pointer(uintptr(data.data) + index*intSizeBytes))
next := item.next()
if next != nil && next.keyHash>>data.keyshifts != index {
next = nil // do not set index to next item if it's not the same slice index
}
swappedToNil := atomic.CompareAndSwapPointer(ptr, unsafe.Pointer(item), unsafe.Pointer(next)) && next == nil
if data == m.metadata.Load() { // check that no resize happened
m.numItems.Add(^uintptr(0)) // decrement counter
if swappedToNil { // decrement the metadata count if the index is set to nil
data.count.Add(^uintptr(0))
}
return
}
}
}
// grow to the new size
func (m *Map[K, V]) grow(newSize uintptr) {
for {
currentStore := m.metadata.Load()
if newSize == 0 {
newSize = uintptr(len(currentStore.index)) << 1
} else {
newSize = roundUpPower2(newSize)
}
index := make([]*element[K, V], newSize)
header := (*reflect.SliceHeader)(unsafe.Pointer(&index))
newdata := &metadata[K, V]{
keyshifts: strconv.IntSize - log2(newSize),
data: unsafe.Pointer(header.Data),
index: index,
}
m.fillIndexItems(newdata) // re-index with longer and more widespread keys
m.metadata.Store(newdata)
if !resizeNeeded(newSize, uintptr(m.Len())) {
m.resizing.Store(notResizing)
return
}
newSize = 0 // 0 means double the current size
}
}
// indexElement returns the index of a hash key, returns `nil` if absent
func (md *metadata[K, V]) indexElement(hashedKey uintptr) *element[K, V] {
index := hashedKey >> md.keyshifts
ptr := (*unsafe.Pointer)(unsafe.Pointer(uintptr(md.data) + index*intSizeBytes))
item := (*element[K, V])(atomic.LoadPointer(ptr))
for (item == nil || hashedKey < item.keyHash || item.isDeleted()) && index > 0 {
index--
ptr = (*unsafe.Pointer)(unsafe.Pointer(uintptr(md.data) + index*intSizeBytes))
item = (*element[K, V])(atomic.LoadPointer(ptr))
}
return item
}
// addItemToIndex adds an item to the index if needed and returns the new item counter if it changed, otherwise 0
func (md *metadata[K, V]) addItemToIndex(item *element[K, V]) uintptr {
index := item.keyHash >> md.keyshifts
ptr := (*unsafe.Pointer)(unsafe.Pointer(uintptr(md.data) + index*intSizeBytes))
for {
elem := (*element[K, V])(atomic.LoadPointer(ptr))
if elem == nil {
if atomic.CompareAndSwapPointer(ptr, nil, unsafe.Pointer(item)) {
return md.count.Add(1)
}
continue
}
if item.keyHash < elem.keyHash {
if !atomic.CompareAndSwapPointer(ptr, unsafe.Pointer(elem), unsafe.Pointer(item)) {
continue
}
}
return 0
}
}
// check if resize is needed
func resizeNeeded(length, count uintptr) bool {
return (count*100)/length > maxFillRate
}
// roundUpPower2 rounds a number to the next power of 2
func roundUpPower2(i uintptr) uintptr {
i--
i |= i >> 1
i |= i >> 2
i |= i >> 4
i |= i >> 8
i |= i >> 16
i |= i >> 32
i++
return i
}
// log2 computes the binary logarithm of x, rounded up to the next integer
func log2(i uintptr) (n uintptr) {
for p := uintptr(1); p < i; p, n = p<<1, n+1 {
}
return
}