-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashtable.go
285 lines (245 loc) · 6.44 KB
/
hashtable.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
package cuckoo
import (
"github.com/upamanyus/go-spinlock"
)
type KVPair struct {
k uint64
v uint64
}
const SLOTS_PER_BUCKET = 4
const LOCK_STRIPES = (1 << 16)
const LOCK_STRIPE_MASK = LOCK_STRIPES - 1
// Should be as big as a single cache-line
type Bucket struct {
kvpairs [SLOTS_PER_BUCKET]KVPair // must have size SLOTS_PER_BUCKET
occupied [SLOTS_PER_BUCKET]bool // must have size SLOTS_PER_BUCKET
}
func (b *Bucket) tryGet(k uint64, v *uint64) bool {
var ret bool
ret = false
for j := 0; j < SLOTS_PER_BUCKET; j++ {
if b.kvpairs[j].k == k && b.occupied[j] {
*v = b.kvpairs[j].v
ret = true
break
} else {
continue
}
}
return ret
}
type CuckooMap struct {
numBuckets uint64 // fixed capacity; never gets larger
mask uint64
buckets []Bucket
locks []*spinlock.SMutex
}
func MakeCuckooMap(hashpower uint64) *CuckooMap {
r := new(CuckooMap)
r.numBuckets = 1 << hashpower
r.mask = (r.numBuckets - 1)
r.buckets = make([]Bucket, r.numBuckets)
r.locks = make([]*spinlock.SMutex, LOCK_STRIPES)
for i, _ := range r.locks {
r.locks[i] = new(spinlock.SMutex)
}
return r
}
func (m *CuckooMap) index1(k uint64) uint64 {
return (k & m.mask)
}
func (m *CuckooMap) index2(k uint64) uint64 {
// from cuckoohash_map.hh
t1 := k ^ (k>>32)&0xffffffff
t2 := t1 ^ (t1>>16)&0xffff
partial := t2 ^ (t2>>8)&0xff
nonzero_tag := partial + uint64(1)
return (k ^ (nonzero_tag * 0xc6a4a7935bd1e995)) & m.mask
}
func lockind(i uint64) uint64 {
return i & LOCK_STRIPE_MASK
}
func (m *CuckooMap) Get(k uint64, v *uint64) bool {
i1 := m.index1(k)
i2 := m.index2(k)
m.lock_two(i1, i2)
if m.buckets[i1].tryGet(k, v) {
m.unlock_two(i1, i2)
return true
}
if m.buckets[i2].tryGet(k, v) {
m.unlock_two(i1, i2)
return true
}
m.unlock_two(i1, i2)
return false
}
const (
INSERT_OK = iota
INSERT_DUP
INSERT_FAIL
)
func (m *CuckooMap) tryInsert(i1 uint64, i2 uint64, k uint64, v uint64) uint64 {
temp := new(uint64)
if m.buckets[i1].tryGet(k, temp) {
m.unlock_two(i1, i2)
return INSERT_DUP
}
if m.buckets[i1].tryGet(k, temp) {
m.unlock_two(i1, i2)
return INSERT_DUP
}
for j, o := range m.buckets[i1].occupied {
if !o {
m.buckets[i1].kvpairs[j] = KVPair{k: k, v: v}
m.buckets[i1].occupied[j] = true
m.unlock_two(i1, i2)
return INSERT_OK
}
}
for j, o := range m.buckets[i2].occupied {
if !o {
m.buckets[i2].kvpairs[j] = KVPair{k: k, v: v}
m.buckets[i2].occupied[j] = true
m.unlock_two(i1, i2)
return INSERT_OK
}
}
return INSERT_FAIL
}
func (m *CuckooMap) Insert(k uint64, v uint64) uint64 {
// try to see if we can insert in one of the two buckets.
// If we can't, then try cuckoo eviction.
i1 := m.index1(k)
i2 := m.index2(k)
triesLeft := 1
m.lock_two(i1, i2)
for {
r := m.tryInsert(i1, i2, k, v)
m.unlock_two(i1, i2)
if r != INSERT_FAIL {
return r
}
if triesLeft == 0 {
return INSERT_FAIL
}
triesLeft--
p := m.cuckoo_search(i1, i2)
if len(p) == 0 {
m.lock_two(i1, i2)
continue
}
if !m.cuckoo_move(p) {
panic("unable to move along cuckoo path")
}
m.lock_two(i1, i2)
}
}
func (m *CuckooMap) lock_two(i1 uint64, i2 uint64) {
j1 := lockind(i1)
j2 := lockind(i2)
if j1 == j2 {
m.locks[j1].Lock()
} else if j2 < j1 {
m.locks[j2].Lock()
m.locks[j1].Lock()
} else { // j1 < j2
m.locks[j1].Lock()
m.locks[j2].Lock()
}
return
}
func (m *CuckooMap) unlock_two(i1 uint64, i2 uint64) {
j1 := lockind(i1)
j2 := lockind(i2)
if j1 == j2 {
m.locks[j1].Unlock()
} else {
m.locks[j1].Unlock()
m.locks[j2].Unlock()
}
}
type SlotNum = uint64
type cuckooPathRecord struct {
bucket uint64
slot SlotNum
hash uint64 // used for confirming that the hash is as expected, so we can safely move to the alternative bucket
}
type cuckooPath = []cuckooPathRecord
type cuckooSearchEntry struct {
path cuckooPath
i uint64
}
func (m *CuckooMap) cuckoo_search(i1 uint64, i2 uint64) cuckooPath {
q := make([]cuckooSearchEntry, 0, 256)
q = append(q, cuckooSearchEntry{path: nil, i: i1})
q = append(q, cuckooSearchEntry{path: nil, i: i2})
for len(q) > 0 {
e := q[0]
q = q[1:]
m.locks[lockind(e.i)].Lock()
for j, o := range m.buckets[e.i].occupied {
if !o {
hash := uint64(0) // no key there; this hash value is never used
e.path = append(e.path, cuckooPathRecord{bucket: e.i, slot: uint64(j), hash: hash})
m.locks[lockind(e.i)].Unlock()
return e.path
} else if len(e.path) < 5 {
slotToEvict := uint64(j)
hash := m.buckets[e.i].kvpairs[slotToEvict].k // XXX: identity hash fn
altI := m.index1(m.buckets[e.i].kvpairs[slotToEvict].k)
if altI == e.i {
altI = m.index2(m.buckets[e.i].kvpairs[slotToEvict].k)
}
newPath := make([]cuckooPathRecord, 0, len(e.path)+1)
newPath = append(newPath, e.path...)
newPath = append(newPath, cuckooPathRecord{bucket: e.i, slot: slotToEvict, hash: hash})
newE := cuckooSearchEntry{path: newPath, i: altI}
q = append(q, newE)
}
}
m.locks[lockind(e.i)].Unlock()
}
return cuckooPath{}
}
// attempts to clear the slot in bucket cuckooPath[0].bucket and slotNum
// cuckooPath[0].slot
// returns false if there was a failure, true if success.
func (m *CuckooMap) cuckoo_move(path cuckooPath) bool {
var j uint64
j = uint64(len(path)) - 1
for j > 0 {
fromI := path[j-1].bucket
toI := path[j].bucket
m.lock_two(fromI, toI)
fromSlot := path[j-1].slot
toSlot := path[j].slot
hash := path[j-1].hash
j = j - 1
if !m.buckets[fromI].occupied[fromSlot] {
// we got lucky, and don't even need to move anything to make space!
m.unlock_two(fromI, toI)
continue
} else if m.buckets[toI].occupied[toSlot] {
// the bucket that we're supposed to move to is full; failure
m.unlock_two(fromI, toI)
return false
} else if m.buckets[fromI].kvpairs[fromSlot].k != hash {
m.unlock_two(fromI, toI)
return false
}
// Otherwise, there is an entry in fromI.fromSlot and space in
// toI.toSlot. And, the hash of the kvpair that we want to move is the
// hash needed for the path to be valid.
// move one kvpair along the path
m.buckets[toI].kvpairs[toSlot] = m.buckets[fromI].kvpairs[fromSlot]
m.buckets[toI].occupied[toSlot] = true
m.buckets[fromI].occupied[fromSlot] = false
m.unlock_two(fromI, toI)
}
// after this loop, we've made space in one of i1 or i2; we've let go of the
// lock before we exit the loop. This means that the caller can't be
// sure that there's still space in the bucket, since we briefly unlocked
// the bucket.
return true
}