forked from salviati/cuckoo
/
cuckoo.go
501 lines (418 loc) · 11 KB
/
cuckoo.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
// Copyright (c) 2014-2015 Utkan Güngördü <utkan@freeconsole.org>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Package cuckoo implements d-ary bucketized cuckoo hashing with stash (bucketized cuckoo hashing is also known as splash tables).
// This implementation uses configurable number of hash functions and cells per bucket.
// Greedy algorithm for collision resolution is a random walk.
package cuckoo
import (
"math/rand"
"runtime"
)
const (
blen = 1 << bshift
bmask = blen - 1
nhash = 1 << nhashshift
nhashmask = nhash - 1
)
type bucket struct {
keys [blen]Key
vals [blen]Value
}
type stash struct {
keys [stashSize]Key
vals [stashSize]Value
}
// Cuckoo implements a memory-efficient map[Key]Value equivalent where Key is an integer and Value can be anything.
// Similar to built-in maps Cuckoo is not thread-safe. In a parallel environment you may need to wrap access with mutexes.
type Cuckoo struct {
logsize int // len(buckets) is 1<<logsize.
buckets []bucket
nentries int
ngrow int
nshrink int
nrehash int
// To avoid allocating a bitmap for bucket usage, we use the default value of key (which is 0) to indicate that the entry is not used.
// Instead of forbidding items with key==0 (and exposing an implementation quirk to the user), we use zeroValue and zeroIsSet to store
// an item with 0 key. Hence, there is no key/value with key==0 within buckets and any bucket with key==0 is empty.
zeroValue Value // Value of the item with Key==0 is placed here.
zeroIsSet bool // true if there is an item with Key==0.
stash stash // stash, Insert's last resort before doing a grow
eitem bool // evacuated leftover item,
ekey Key // ...and its key.
eval Value
seed [nhash]hash // seed for hash functions.
}
var zero Value
func alloc(n int) []bucket {
return make([]bucket, n, n)
}
func init() {
// ensures the sanity of the config
if nhash*nhashshift+bshift+nhashshift > 63 {
panic("cuckoo: tryGreedyAdd needs nhash*nhashshift + bshift + nhashshift bits of random data; either modify tryGreedyAdd or reduce nhash/bshift.")
}
if nhashshift > 8 {
panic("cuckoo: nhashshift is too large. either modify Cuckoo.shuffle or reduce nhashshift.")
}
}
// NewCuckoo creates a new cuckoo hash table with 2^logsize number of key/value cells initially.
//
// If you can estimate the number of unique items n (unique here refers to keys, not values) you are going to insert,
// choosing a proper logsize [which is math.Ceil(math.Log2(n))] here is strongly advised.
// Doing so will avoid grows, which are computationally expensive and require allocation.
// Logsize mustn't exceed hashBits, defined in hash.go.
func NewCuckoo(logsize int) *Cuckoo {
logsize -= bshift
if logsize <= 0 {
logsize = 1
}
if logsize > hashBits {
panic("cuckoo: log size is too large")
}
c := &Cuckoo{
buckets: alloc(1 << uint(logsize)),
logsize: logsize,
}
c.reseed()
return c
}
func (c *Cuckoo) reseed() {
for i := range &c.seed {
c.seed[i] = hash(rand.Uint32())
}
}
// Len returns the number of items in the hash map.
func (c *Cuckoo) Len() int {
return c.nentries
}
// default hash function
func defaultHash(k Key, seed hash) hash {
return hash(xx_32(uint32(k), uint32(seed)))
}
func (c *Cuckoo) dohash(key Key, h *[nhash]hash) {
mask := hash((1 << uint(c.logsize)) - 1)
for i := range h {
h[i] = defaultHash(key, c.seed[i]) & mask
}
return
}
// uses lowest nhash*nhashshift bits of r.
func (c *Cuckoo) shuffle(h *[nhash]hash, r int64) {
// Fisher-Yates shuffle
for j := nhash - 1; j > 0; j-- {
i := int(uint8(r&nhashmask) % uint8(j+1)) // we assume nhashshift <= 8 here (some archs lack div instruction).
h[i], h[j] = h[j], h[i]
r >>= nhashshift
}
}
// Search tries to retrieve the value associated with the given key.
// If no such item is found, ok is set to false.
func (c *Cuckoo) Search(k Key) (v Value, ok bool) {
if k == 0 {
if c.zeroIsSet == false {
return
}
return c.zeroValue, true
}
// TODO(utkan): SSE2/AVX2 version
var h [nhash]hash
c.dohash(k, &h)
for _, hval := range &h {
b := &c.buckets[int(hval)]
for i, key := range &b.keys {
if k == key {
return b.vals[i], true
}
}
}
for i, key := range c.stash.keys {
if key == k {
return c.stash.vals[i], true
}
}
return
}
// Delete removes the item corresponding to the given key (if exists).
func (c *Cuckoo) Delete(k Key) {
if c.tryDelete(k) == false {
return
}
if 1<<uint(c.logsize+bshift-shrinkFactor) > c.nentries {
// TODO(utkan): depending on the current load factorm starting from shrinkFactor-1 may be better.
for i := shrinkFactor; i > 0; i-- {
if c.tryGrow(-i) {
break
}
}
}
}
func (c *Cuckoo) tryDelete(k Key) bool {
if k == 0 {
c.zeroIsSet = false
c.zeroValue = zero
c.nentries--
return true
}
var h [nhash]hash
c.dohash(k, &h)
for _, hval := range &h {
b := &c.buckets[int(hval)]
for i, key := range &b.keys {
if k == key {
c.nentries--
b.keys[i] = 0
b.vals[i] = zero
return true
}
}
}
for i, key := range c.stash.keys {
if k == key {
c.stash.keys[i] = 0
c.stash.vals[i] = zero
c.nentries--
return true
}
}
return false
}
// Insert adds given key/value item into the hash map.
// If an item with key k already exists, it will be replaced.
func (c *Cuckoo) Insert(k Key, v Value) {
if k == 0 {
c.zeroIsSet = true
c.zeroValue = v
c.nentries++
return
}
for {
if c.tryInsert(k, v) {
return
}
i0 := 1
if c.LoadFactor() < rehashThreshold {
i0 = 0
}
for i := i0; ; i++ {
if ok := c.tryGrow(i); ok {
break
}
}
}
}
func (c *Cuckoo) tryInsert(k Key, v Value) (inserted bool) {
var h [nhash]hash
c.dohash(k, &h)
// Are we just updating the value for an existing key?
updated, freeSlot, ibucket, index := c.tryUpdate(k, v, &h)
if updated {
return true
}
// Nope, do we have an empty slot?
if freeSlot {
c.addAt(k, v, ibucket, index)
c.nentries++
return true
}
// Nope again, lets try moving the eggs around.
if c.tryGreedyAdd(k, v, &h) {
c.nentries++
return true
}
// All failed.
return false
}
// If we already have an element with the the key k, we just update the value.
// Otherwise, index of an available slot --if exists at all-- is returned.
func (c *Cuckoo) tryUpdate(k Key, v Value, h *[nhash]hash) (updated bool, freeSlot bool, ibucket int, index int) {
// TODO(utkan): SSE2/AVX2 version
for _, bi := range h {
b := &c.buckets[int(bi)]
for i, key := range &b.keys {
if k == key {
b.vals[i] = v
updated = true
return
}
if freeSlot == false && key == 0 {
ibucket = int(bi)
index = i
freeSlot = true
}
}
}
for i, key := range c.stash.keys {
if k == key {
c.stash.vals[i] = v
updated = true
return
}
}
return
}
func (c *Cuckoo) addAt(k Key, v Value, ibucket int, index int) {
b := &c.buckets[ibucket]
b.keys[index] = k
b.vals[index] = v
}
// Used by tryGrow and tryGreedyAdd.
// Similar to tryUpdate, but tryAdd assumes there is no item with key already.
// tryAdd also omits the slot given by the parameter except, when ignore is set to true.
func (c *Cuckoo) tryAdd(k Key, v Value, h *[nhash]hash, ignore bool, except hash) (added bool) {
if k == 0 {
c.zeroIsSet = true
c.zeroValue = v
return
}
for _, hval := range h {
if ignore && except == hval {
continue
}
bi := int(hval)
b := &c.buckets[bi]
for i, key := range &b.keys {
if key == 0 {
b.keys[i] = k
b.vals[i] = v
return true
}
}
}
return false
}
// tryUpdate and tryAdd both failed. Let's try moving the eggs around.
// This implementation uses random walk.
func (c *Cuckoo) tryGreedyAdd(k Key, v Value, h *[nhash]hash) (added bool) {
// Expected maximum number of steps is O(log(n)):
// Frieze, Alan, Páll Melsted, and Michael Mitzenmacher. "An analysis of random-walk cuckoo hashing." SIAM Journal on Computing 40.2 (2011): 291-308.
max := (1 + c.logsize) * randomWalkCoefficient
var ehash [nhash]hash
for step := 0; step < max; step++ {
r := rand.Int63() // need nhash*nhashshift + bshift + nhashshift random bits
c.shuffle(h, r)
r >>= nhash * nhashshift
// randomly choose the item to evict
i := int(r & bmask)
d := int((r >> bshift) & nhashmask)
hval := h[d]
b := &c.buckets[int(hval)]
ekey, eval := b.keys[i], b.vals[i]
b.keys[i], b.vals[i] = k, v
// try to put the evicted item back
c.dohash(ekey, &ehash)
if c.tryAdd(ekey, eval, &ehash, true, hval) {
return true
}
// we're back to where we started, except with a new item.
k = ekey
v = eval
*h = ehash
}
// try to insert into stash as a last resort
for i, key := range c.stash.keys {
if key == 0 {
c.stash.keys[i] = k
c.stash.vals[i] = v
return true
}
}
c.ekey = k
c.eval = v
c.eitem = true
return false
}
// LoadFactor returns the load factor of the hash table, which is the
// ratio of the used cells to the allocated cells.
func (c *Cuckoo) LoadFactor() float64 {
return float64(c.nentries) / float64(len(c.buckets)<<bshift)
}
// Tries to grow the hash table by a factor of 2^δ.
func (c *Cuckoo) tryGrow(δ int) (ok bool) {
// NOTE(utkan): reads during grow are OK.
cnew := &Cuckoo{}
*cnew = *c
cnew.reseed()
if δ == 0 {
cnew.nrehash++
}
if δ > 0 {
cnew.ngrow++
}
if δ < 0 {
if cnew.logsize <= 8 {
return
}
cnew.nshrink++
}
cnew.logsize += δ
if cnew.logsize > hashBits {
panic("cuckoo: cannot grow any furher")
}
cnew.buckets = alloc(1 << uint(cnew.logsize))
// rehash everything; we get better load factors at the expense of CPU time.
defer func() {
if ok {
*c = *cnew
}
cnew = nil
if gc {
runtime.GC()
}
}()
var h [nhash]hash
for bi := range c.buckets {
b := c.buckets[bi]
for i, k := range &b.keys {
if k == 0 {
continue
}
v := b.vals[i]
cnew.dohash(k, &h)
if cnew.tryAdd(k, v, &h, false, 0) {
continue
}
if ok = cnew.tryGreedyAdd(k, v, &h); !ok {
return
}
}
}
if cnew.eitem {
if ok = cnew.tryInsert(cnew.ekey, cnew.eval); !ok {
return
}
cnew.eitem = false
}
ok = true
return
}
// ForRange loops over all (key,value) pairs in the hash map and calls f for each.
func (c *Cuckoo) ForRange(f func(Key, Value)) {
if c.zeroIsSet {
f(0, c.zeroValue)
}
for bi := range c.buckets {
b := &c.buckets[bi]
for i, key := range &b.keys {
if key != 0 {
f(key, b.vals[i])
}
}
}
for i, key := range c.stash.keys {
if key != 0 {
f(key, c.stash.vals[i])
}
}
}