-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmap.go
414 lines (371 loc) · 10.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
package umap
import (
"unsafe"
)
const (
emptySlot uint8 = 0b1111_1111
deletedSlot uint8 = 0b1000_0000
msbs uint64 = 0x8080_8080_8080_8080
lsbs uint64 = 0x0101_0101_0101_0101
allEmpty uint64 = uint64(emptySlot) + uint64(emptySlot)<<8 + uint64(emptySlot)<<16 +
uint64(emptySlot)<<24 + uint64(emptySlot)<<32 + uint64(emptySlot)<<40 +
uint64(emptySlot)<<48 + uint64(emptySlot)<<56
// LoadFactor.
// 0 -> invalid
// 1 -> 0.5(min)
// 2 -> 0.75
// 3 -> 0.875(max)
// 4 -> 0.9375, invalid if bucketCnt=8
loadFactorShift = 3
emptyItemInBucket = bucketCnt >> loadFactorShift // used for optimazation.
maxItemInBucket = bucketCnt - (bucketCnt >> loadFactorShift)
)
type Uint64Map struct {
count int
growthLeft int
bucketmask uint64
buckets unsafe.Pointer
}
// bmapuint64 represents a bucket.
type bmapuint64 struct {
tophash [bucketCnt]uint8
data [bucketCnt]uint64kv
}
type uint64kv struct {
key uint64
value uint64
}
// New64 returns an empty Uint64Map.
//
// If length <= 0, it returns an empty map(including a pre-allocated bucket).
func New64(length int) *Uint64Map {
// Make sure the compiler can inline New64(cost < 80).
bucketnum := uint(1)
if length >= bucketCnt {
minBucket := uint(nextPowerOfTwo(length) / bucketCnt)
// Make sure (capcity * LoadFactor) >= length.
if minBucket*maxItemInBucket < uint(length) {
minBucket *= 2
}
bucketnum = minBucket
}
x := make([]bmapuint64, bucketnum)
for i := range x {
*(*uint64)(unsafe.Pointer(&x[i].tophash)) = allEmpty
}
return &Uint64Map{
buckets: (*sliceHeader)(unsafe.Pointer(&x)).Data,
bucketmask: uint64(bucketnum) - 1,
growthLeft: int(bucketnum * bucketCnt),
}
}
func (h *Uint64Map) Load(key uint64) (value uint64, ok bool) {
hashres := hashUint64(uint64(key))
tophash := tophash(hashres)
indexMask := uint64(h.bucketmask)
index := hashres & indexMask
indexStride := uint64(0)
for {
bucket := bmapPointer(h.buckets, uint(index))
status := matchTopHash(bucket.tophash, tophash)
for {
sloti := status.NextMatch()
if sloti >= bucketCnt {
break
}
if bucket.data[sloti].key == key {
return bucket.data[sloti].value, true
}
status.RemoveLowestBit()
}
if bucket.MatchEmpty().AnyMatch() {
return
}
// Update index, go to next bucket.
indexStride += 1
index += indexStride
index &= indexMask
}
}
// Store sets the value for a key.
func (h *Uint64Map) Store(key uint64, value uint64) {
if h.needGrow() {
h.growWork()
}
hashres := hashUint64(uint64(key))
tophash := tophash(hashres)
indexMask := uint64(h.bucketmask)
index := hashres & indexMask
indexStride := uint64(0)
var (
bucket *bmapuint64
status bitmask64
)
// Check if the key is in the map.
for {
bucket = bmapPointer(h.buckets, uint(index))
status = matchTopHash(bucket.tophash, tophash)
for {
sloti := status.NextMatch()
if sloti >= bucketCnt {
break
}
if bucket.data[sloti].key == key {
// Found.
bucket.data[sloti].value = value
return
}
status.RemoveLowestBit()
}
if bucket.MatchEmpty().AnyMatch() {
break
}
// Update index, go to next bucket.
indexStride += 1
index += indexStride
index &= indexMask
}
// The key is not in the map.
index = hashres & indexMask
indexStride = 0
for {
bucket = bmapPointer(h.buckets, uint(index))
// Can't find the key in this bucket.
// Check empty slot or deleted slot.
status = bucket.MatchEmptyOrDeleted()
sloti := status.NextMatch()
if sloti < bucketCnt {
bucket.tophash[sloti] = tophash
bucket.data[sloti].key = key
bucket.data[sloti].value = value
h.growthLeft -= 1
h.count += 1
return
}
// No idle slot
// Update index, go to next bucket.
indexStride += 1
index += indexStride
index &= indexMask
}
}
// storeWithoutGrow inserts values into the hashmap without growing.
// This function also assumes that
// - There are always enough empty slots in the hashmap.
// - No deleted slots in the hashmap. (not used for now)
func (h *Uint64Map) storeWithoutGrow(key uint64, value uint64) {
hashres := hashUint64(uint64(key))
tophash := tophash(hashres)
indexMask := uint64(h.bucketmask)
index := hashres & indexMask
indexStride := uint64(0)
for {
bucket := bmapPointer(h.buckets, uint(index))
// Just checking the empty slot is fine, but using this function is faster.
status := bucket.MatchEmptyOrDeleted()
sloti := status.NextMatch()
if sloti < bucketCnt {
bucket.tophash[sloti] = tophash
bucket.data[sloti].key = key
bucket.data[sloti].value = value
return
}
// No idle slot, update index then go to next bucket.
indexStride += 1
index += indexStride
index &= indexMask
}
}
// Delete deletes the value for a key.
func (h *Uint64Map) Delete(key uint64) {
hashres := hashUint64(uint64(key))
tophash := tophash(hashres)
indexMask := uint64(h.bucketmask)
index := hashres & indexMask
indexStride := uint64(0)
for {
bucket := bmapPointer(h.buckets, uint(index))
status := matchTopHash(bucket.tophash, tophash)
for {
sloti := status.NextMatch()
if sloti >= bucketCnt {
break
}
if bucket.data[sloti].key == key {
// Found this key.
h.count -= 1
if !bucket.MatchEmpty().AnyMatch() {
bucket.tophash[sloti] = deletedSlot
} else {
h.growthLeft += 1
bucket.tophash[sloti] = emptySlot
}
return
}
status.RemoveLowestBit()
}
if bucket.MatchEmpty().AnyMatch() {
// The key is not in this map.
return
}
// Update index, go to next bucket.
indexStride += 1
index += indexStride
index &= indexMask
}
}
// Range calls f sequentially for each key and value present in the map.
// If f returns false, range stops the iteration.
func (h *Uint64Map) Range(f func(k uint64, v uint64) bool) {
initBuckets := h.buckets
bucketNum := int(h.bucketmask + 1)
indexMask := uint64(h.bucketmask)
index := uint64(0)
indexStride := uint64(0)
for indexStride < uint64(bucketNum) {
bucket := bmapPointer(initBuckets, uint(index))
for sloti := 0; sloti < bucketCnt; sloti++ {
if isDeletedOrEmpty(bucket.tophash[sloti]) {
continue
}
kv := bucket.data[sloti]
key := kv.key
value := kv.value
if !f(key, value) {
return
}
}
// Update index, go to next bucket.
indexStride += 1
index += indexStride
index &= indexMask
}
}
// Len returns the length of the hashmap.
func (h *Uint64Map) Len() int {
return int(h.count)
}
func (h *Uint64Map) growWork() {
oldCap := (h.bucketmask + 1) * bucketCnt
if uint64(h.count) < oldCap>>1 {
// Too many slots are locked by deleted items.
h.sameSizeGrow()
} else {
h.grow()
}
}
func (h *Uint64Map) sameSizeGrow() {
// Algorithm:
// - mark all DELETED slots as EMPTY
// - mark all FULL slots as DELETED
// - for each slot marked as DELETED
// hash = Hash(element)
// target = find_first_non_full(hash)
// if target is in the same group
// mark slot as FULL
// else if target is EMPTY
// transfer element to target
// mark slot as EMPTY
// mark target as FULL
// else if target is DELETED
// swap current element with target element
// mark target as FULL
// repeat procedure for current slot with moved from element (target)
// - hashmap grow left = cap - items
bucketNum := uint(h.bucketmask + 1)
for index := uint(0); index < bucketNum; index++ {
bucket := bmapPointer(h.buckets, uint(index))
bucket.PrepareSameSizeGrow()
}
for index := uint(0); index < bucketNum; index++ {
bucket := bmapPointer(h.buckets, uint(index))
for sloti := uint(0); sloti < bucketCnt; {
if bucket.tophash[sloti] != deletedSlot {
sloti += 1
continue
}
slotHashRes := hashUint64(uint64(bucket.data[sloti].key))
tophash := tophash(slotHashRes)
targetBucket, targetBucketIdx, targetSlotIdx := h.findFirstNotNull(slotHashRes)
if targetBucketIdx == index {
// Just mark the slot as FULL.
bucket.tophash[sloti] = tophash
sloti += 1
continue
}
switch targetBucket.tophash[targetSlotIdx] {
case emptySlot:
// 1. Transfer element to target
// 2. Mark target as FULL
// 3. Mark slot as EMPTY
targetBucket.data[targetSlotIdx].key = bucket.data[sloti].key
targetBucket.data[targetSlotIdx].value = bucket.data[sloti].value
targetBucket.tophash[targetSlotIdx] = tophash
bucket.tophash[sloti] = emptySlot
sloti++
case deletedSlot:
// 1. Swap current element with target element
// 2. Mark target as FULL
// 3. Repeat procedure for current slot with moved from element (target)
targetBucket.data[targetSlotIdx].key, bucket.data[sloti].key = bucket.data[sloti].key, targetBucket.data[targetSlotIdx].key
targetBucket.data[targetSlotIdx].value, bucket.data[sloti].value = bucket.data[sloti].value, targetBucket.data[targetSlotIdx].value
targetBucket.tophash[targetSlotIdx] = tophash
}
}
}
h.growthLeft = int(bucketNum*bucketCnt) - h.count
}
func (h *Uint64Map) findFirstNotNull(hashres uint64) (bucket *bmapuint64, bucketi, sloti uint) {
indexMask := uint64(h.bucketmask)
index := hashres & indexMask
indexStride := uint64(0)
for {
bucket := bmapPointer(h.buckets, uint(index))
status := bucket.MatchEmptyOrDeleted()
sloti := status.NextMatch()
if sloti < bucketCnt {
return bucket, uint(index), uint(sloti)
}
// Update index, go to next bucket.
indexStride += 1
index += indexStride
index &= indexMask
}
}
func (h *Uint64Map) grow() {
oldBucketnum := h.bucketmask + 1
newBucketnum := oldBucketnum * 2
newBucketMask := newBucketnum - 1
newCap := newBucketnum * bucketCnt
newMap := &Uint64Map{
buckets: makeUint64BucketArray(int(newBucketnum)),
bucketmask: newBucketMask,
}
for index := uint64(0); index < oldBucketnum; index++ {
bucket := bmapPointer(h.buckets, uint(index))
for i := 0; i < bucketCnt; i++ {
if isFull(bucket.tophash[i]) {
kv := bucket.data[i]
newMap.storeWithoutGrow(kv.key, kv.value)
}
}
}
h.bucketmask = newBucketMask
// h.items is the same.
h.buckets = newMap.buckets
h.growthLeft = int(newCap) - h.count
}
func (h *Uint64Map) needGrow() bool {
return h.growthLeft <= growThresholdUint64(h.bucketmask)
}
func tophash(v uint64) uint8 {
return uint8(v >> 57)
}
// isDeletedOrEmpty returns true if the slot is deleted or empty.
func isDeletedOrEmpty(v uint8) bool {
return v >= 128
}
// isFull returns true if the slot is full.
func isFull(v uint8) bool {
return v < 128
}