-
Notifications
You must be signed in to change notification settings - Fork 45
/
map.go
370 lines (328 loc) · 10.1 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
package haxmap
import (
"reflect"
"strconv"
"sync/atomic"
"unsafe"
"github.com/alphadose/haxmap/hash"
)
const (
// hash input allowed sizes
byteSize = 1 << iota
wordSize
dwordSize
qwordSize
owordSize
// DefaultSize is the default size for a zero allocated map
DefaultSize = 256
// 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 (
// allowed map key types constraint
hashable interface {
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | uintptr | float32 | float64 | string | complex64 | complex128
}
hashMapData[K hashable, V any] struct {
keyshifts uintptr // Pointer size - log2 of array size, to be used as index in the data array
count atomic.Uintptr // count of filled elements in the slice
data unsafe.Pointer // pointer to slice data array
index []*element[K, V]
}
// HashMap implements a read optimized hash map.
HashMap[K hashable, V any] struct {
listHead *element[K, V] // key sorted linked list of elements
hasher func(K) uintptr
datamap atomic.Pointer[hashMapData[K, V]] // pointer to a map instance that gets replaced if the map resizes
resizing atomic.Uint32
numItems atomic.Uintptr
}
)
// New returns a new HashMap instance with an optional specific initialization size.
func New[K hashable, V any](size ...uintptr) *HashMap[K, V] {
m := &HashMap[K, V]{listHead: newListHead[K, V]()}
m.numItems.Store(0)
if len(size) > 0 {
m.allocate(size[0])
} else {
m.allocate(DefaultSize)
}
// default hash functions
switch any(*new(K)).(type) {
case int, uint, uintptr:
m.hasher = func(key K) uintptr {
return hash.Sum(*(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(&key)),
Len: intSizeBytes,
Cap: intSizeBytes,
})))
}
case int8, uint8:
m.hasher = func(key K) uintptr {
return hash.Sum(*(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(&key)),
Len: byteSize,
Cap: byteSize,
})))
}
case int16, uint16:
m.hasher = func(key K) uintptr {
return hash.Sum(*(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(&key)),
Len: wordSize,
Cap: wordSize,
})))
}
case int32, uint32, float32:
m.hasher = func(key K) uintptr {
return hash.Sum(*(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(&key)),
Len: dwordSize,
Cap: dwordSize,
})))
}
case int64, uint64, float64, complex64:
m.hasher = func(key K) uintptr {
return hash.Sum(*(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(&key)),
Len: qwordSize,
Cap: qwordSize,
})))
}
case complex128:
m.hasher = func(key K) uintptr {
return hash.Sum(*(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(&key)),
Len: owordSize,
Cap: owordSize,
})))
}
case string:
m.hasher = func(key K) uintptr {
sh := (*reflect.StringHeader)(unsafe.Pointer(&key))
return hash.Sum(*(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
Data: sh.Data,
Len: sh.Len,
Cap: sh.Len,
})))
}
}
return m
}
// returns the index of a hash key, returns `nil` if absent
func (mapData *hashMapData[K, V]) indexElement(hashedKey uintptr) *element[K, V] {
index := hashedKey >> mapData.keyshifts
ptr := (*unsafe.Pointer)(unsafe.Pointer(uintptr(mapData.data) + index*intSizeBytes))
item := (*element[K, V])(atomic.LoadPointer(ptr))
for (item == nil || hashedKey < item.keyHash) && index > 0 {
index--
ptr = (*unsafe.Pointer)(unsafe.Pointer(uintptr(mapData.data) + index*intSizeBytes))
item = (*element[K, V])(atomic.LoadPointer(ptr))
}
return item
}
// Del deletes the key from the map.
func (m *HashMap[K, V]) Del(key K) {
h := m.hasher(key)
element := m.datamap.Load().indexElement(h)
loop:
for ; element != nil; element = element.next() {
if element.keyHash == h && element.key == key {
break loop
}
if element.keyHash > h {
return
}
}
if element == nil {
return
}
element.remove()
for {
data := m.datamap.Load()
index := element.keyHash >> data.keyshifts
ptr := (*unsafe.Pointer)(unsafe.Pointer(uintptr(data.data) + index*intSizeBytes))
next := element.next()
if next != nil && element.keyHash>>data.keyshifts != index {
next = nil // do not set index to next item if it's not the same slice index
}
atomic.CompareAndSwapPointer(ptr, unsafe.Pointer(element), unsafe.Pointer(next))
if data == m.datamap.Load() { // check that no resize happened
m.numItems.Add(marked)
return
}
}
}
// Get retrieves an element from the map under given hash key.
func (m *HashMap[K, V]) Get(key K) (value V, ok bool) {
h := m.hasher(key)
// inline search
for elem := m.datamap.Load().indexElement(h); elem != nil; elem = elem.nextPtr.Load() {
if elem.keyHash == h && elem.key == key {
value, ok = *elem.value.Load(), true
return
}
if elem.keyHash == marked || elem.keyHash < h {
continue
} else {
break
}
}
ok = false
return
}
// Set sets the value under the specified key to the map. An existing item for this key will be overwritten.
// If a resizing operation is happening concurrently while calling Set, the item might show up in the map only after the resize operation is finished.
func (m *HashMap[K, V]) Set(key K, value V) {
h, valPtr := m.hasher(key), &value
var (
alloc *element[K, V]
created = false
)
for {
data := m.datamap.Load()
if data == nil {
m.Grow(DefaultSize)
continue // read mapdata and slice item again
}
existing := data.indexElement(h)
if existing == nil {
existing = m.listHead
}
if alloc, created = existing.inject(h, key, valPtr); created {
m.numItems.Add(1)
}
count := data.addItemToIndex(alloc)
if resizeNeeded(uintptr(len(data.index)), count) && m.resizing.CompareAndSwap(notResizing, resizingInProgress) {
go m.grow(0, true)
}
return
}
}
// adds an item to the index if needed and returns the new item counter if it changed, otherwise 0
func (mapData *hashMapData[K, V]) addItemToIndex(item *element[K, V]) uintptr {
index := item.keyHash >> mapData.keyshifts
ptr := (*unsafe.Pointer)(unsafe.Pointer(uintptr(mapData.data) + index*intSizeBytes))
for { // loop until the smallest key hash is in the index
element := (*element[K, V])(atomic.LoadPointer(ptr)) // get the current item in the index
if element == nil { // no item yet at this index
if atomic.CompareAndSwapPointer(ptr, nil, unsafe.Pointer(item)) {
return mapData.count.Add(1)
}
continue // a new item was inserted concurrently, retry
}
if item.keyHash < element.keyHash {
// the new item is the smallest for this index?
if !atomic.CompareAndSwapPointer(ptr, unsafe.Pointer(element), unsafe.Pointer(item)) {
continue // a new item was inserted concurrently, retry
}
}
return 0
}
}
func (m *HashMap[K, V]) fillIndexItems(mapData *hashMapData[K, V]) {
first := m.listHead
item := first
lastIndex := uintptr(0)
for item != nil {
index := item.keyHash >> mapData.keyshifts
if item == first || index != lastIndex { // store item with smallest hash key for every index
mapData.addItemToIndex(item)
lastIndex = index
}
item = item.next()
}
}
// ForEach iterates over key-value pairs and executes the lambda provided for each such pair.
func (m *HashMap[K, V]) ForEach(lambda func(K, V)) {
for item := m.listHead.next(); item != nil; item = item.next() {
lambda(item.key, *item.value.Load())
}
}
// 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.
// This function returns immediately, the resize operation is done in a goroutine.
// No resizing is done in case of another resize operation already being in progress.
func (m *HashMap[K, V]) Grow(newSize uintptr) {
if m.resizing.CompareAndSwap(notResizing, resizingInProgress) {
m.grow(newSize, true)
}
}
// SetHasher sets the hash function to the one provided by the user
func (m *HashMap[K, V]) SetHasher(hs func(K) uintptr) {
m.hasher = hs
}
// Len returns the number of key-value pairs within the map.
func (m *HashMap[K, V]) Len() uintptr {
return m.numItems.Load()
}
// Fillrate returns the fill rate of the map as an percentage integer.
func (m *HashMap[K, V]) Fillrate() uintptr {
data := m.datamap.Load()
return (data.count.Load() * 100) / uintptr(len(data.index))
}
func (m *HashMap[K, V]) allocate(newSize uintptr) {
if m.resizing.CompareAndSwap(notResizing, resizingInProgress) {
m.grow(newSize, false)
}
}
// grow to the new size
func (m *HashMap[K, V]) grow(newSize uintptr, loop bool) {
defer m.resizing.CompareAndSwap(resizingInProgress, notResizing)
for {
currentStore := m.datamap.Load()
if newSize == 0 {
newSize = uintptr(len(currentStore.index)) << 1
} else {
newSize = roundUpPower2(newSize)
}
index := make([]*element[K, V], newSize, newSize)
header := (*reflect.SliceHeader)(unsafe.Pointer(&index))
newdata := &hashMapData[K, V]{
keyshifts: strconv.IntSize - log2(newSize),
data: unsafe.Pointer(header.Data), // use address of slice data storage
index: index,
}
m.fillIndexItems(newdata) // initialize new index slice with longer keys
m.datamap.Store(newdata)
m.fillIndexItems(newdata) // make sure that the new index is up-to-date with the current state of the linked list
if !loop {
return
}
// check if a new resize needs to be done already
if !resizeNeeded(newSize, uintptr(m.Len())) {
return
}
newSize = 0 // 0 means double the current size
}
}
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) uintptr {
var n, p uintptr
for p = 1; p < i; p += p {
n++
}
return n
}