-
Notifications
You must be signed in to change notification settings - Fork 17
/
hash.go
427 lines (391 loc) · 11.8 KB
/
hash.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
package gotomic
import (
"bytes"
"fmt"
"hash/crc32"
"sync/atomic"
"unsafe"
)
const max_exponent = 32
const default_load_factor = 0.5
type HashIterator func(k Hashable, v Thing) bool
type hashHit hit
func (self *hashHit) search(cmp *entry) (rval *hashHit) {
rval = &hashHit{self.left, self.element, self.right}
for {
if rval.element == nil {
break
}
rval.right = rval.element.next()
e := rval.element.value.(*entry)
if e.hashKey != cmp.hashKey {
rval.right = rval.element
rval.element = nil
break
}
if cmp.key.Equals(e.key) {
break
}
rval.left = rval.element
rval.element = rval.left.next()
rval.right = nil
}
return
}
func (self *hashHit) String() string {
return fmt.Sprint("&hashHit{", self.left.val(), self.element.val(), self.right.val(), "}")
}
type Equalable interface {
Equals(Thing) bool
}
/*
Hashable types can be in a Hash.
*/
type Hashable interface {
Equalable
HashCode() uint32
}
/*
Convenience type to simplify using ints as keys in a Hash
*/
type IntKey int
func (self IntKey) HashCode() uint32 {
return uint32(self)
}
func (self IntKey) Equals(t Thing) bool {
if ik, ok := t.(IntKey); ok {
return int(self) == int(ik)
}
return false
}
/*
Convenience type to simplify using strings as keys in a Hash
*/
type StringKey string
func (self StringKey) HashCode() uint32 {
return crc32.ChecksumIEEE([]byte(self))
}
func (self StringKey) Equals(t Thing) bool {
if sk, ok := t.(StringKey); ok {
return string(self) == string(sk)
}
return false
}
type entry struct {
hashCode uint32
hashKey uint32
key Hashable
value unsafe.Pointer
}
func newRealEntryWithHashCode(k Hashable, v Thing, hc uint32) *entry {
return &entry{hc, reverse(hc) | 1, k, unsafe.Pointer(&v)}
}
func newRealEntry(k Hashable, v Thing) *entry {
return newRealEntryWithHashCode(k, v, k.HashCode())
}
func newMockEntry(hashCode uint32) *entry {
return &entry{hashCode, reverse(hashCode) &^ 1, nil, nil}
}
func (self *entry) real() bool {
return self.hashKey&1 == 1
}
func (self *entry) val() Thing {
if self.value == nil {
return nil
}
return *(*Thing)(atomic.LoadPointer(&self.value))
}
func (self *entry) String() string {
return fmt.Sprintf("&entry{%0.32b/%0.32b, %v=>%v}", self.hashCode, self.hashKey, self.key, self.val())
}
func (self *entry) Compare(t Thing) int {
if t == nil {
return 1
}
if e, ok := t.(*entry); ok {
if self.hashKey > e.hashKey {
return 1
} else if self.hashKey < e.hashKey {
return -1
} else {
return 0
}
}
panic(fmt.Errorf("%v can only compare itself against other *entry, not against %v", self, t))
}
/*
Hash is a hash table based on "Split-Ordered Lists: Lock-Free Extensible Hash Tables" by Ori Shalev and Nir Shavit <http://www.cs.ucf.edu/~dcm/Teaching/COT4810-Spring2011/Literature/SplitOrderedLists.pdf>.
TL;DR: It creates a linked list containing all hashed entries, and an extensible table of 'shortcuts' into said list. To enable future extensions to the shortcut table, the list is ordered in reversed bit order so that new table entries point into finer and finer sections of the potential address space.
To enable growing the table a two dimensional slice of unsafe.Pointers is used, where each consecutive slice is twice the size of the one before.
This makes it simple to allocate exponentially more memory for the table with only a single extra indirection.
*/
type Hash struct {
exponent uint32
buckets []unsafe.Pointer
size int64
loadFactor float64
}
func NewHash() *Hash {
rval := &Hash{0, make([]unsafe.Pointer, max_exponent), 0, default_load_factor}
b := make([]unsafe.Pointer, 1)
rval.buckets[0] = unsafe.Pointer(&b)
return rval
}
func (self *Hash) Size() int {
return int(atomic.LoadInt64(&self.size))
}
/*
Each will run i on each key and value.
It returns true if the iteration was interrupted.
This is the case when one of the HashIterator calls returned true, indicating
the iteration should be stopped.
*/
func (self *Hash) Each(i HashIterator) bool {
return self.getBucketByHashCode(0).each(func(t Thing) bool {
e := t.(*entry)
return e.real() && i(e.key, e.val())
})
}
/*
Verify the integrity of the Hash. Used mostly in my own tests but go ahead and call it if you fear corruption.
*/
func (self *Hash) Verify() error {
bucket := self.getBucketByHashCode(0)
if e := bucket.verify(); e != nil {
return e
}
for bucket != nil {
e := bucket.value.(*entry)
if e.real() {
if ok, index, super, sub := self.isBucket(bucket); ok {
return fmt.Errorf("%v has %v that should not be a bucket but is bucket %v (%v, %v)", self, e, index, super, sub)
}
} else {
if ok, _, _, _ := self.isBucket(bucket); !ok {
return fmt.Errorf("%v has %v that should be a bucket but isn't", self, e)
}
}
bucket = bucket.next()
}
return nil
}
/*
ToMap returns a map[Hashable]Thing that is logically identical to the Hash.
*/
func (self *Hash) ToMap() map[Hashable]Thing {
rval := make(map[Hashable]Thing)
self.Each(func(k Hashable, v Thing) bool {
rval[k] = v
return false
})
return rval
}
func (self *Hash) isBucket(n *element) (isBucket bool, index, superIndex, subIndex uint32) {
e := n.value.(*entry)
index = e.hashCode & ((1 << self.exponent) - 1)
superIndex, subIndex = self.getBucketIndices(index)
subBucket := *(*[]unsafe.Pointer)(atomic.LoadPointer(&self.buckets[superIndex]))
if subBucket[subIndex] == unsafe.Pointer(n) {
isBucket = true
}
return
}
/*
Describe returns a multi line description of the contents of the map for
those of you interested in debugging it or seeing an example of how split-ordered lists work.
*/
func (self *Hash) Describe() string {
buffer := bytes.NewBufferString(fmt.Sprintf("&Hash{%p size:%v exp:%v maxload:%v}\n", self, self.size, self.exponent, self.loadFactor))
element := self.getBucketByIndex(0)
for element != nil {
e := element.value.(*entry)
if ok, index, super, sub := self.isBucket(element); ok {
fmt.Fprintf(buffer, "%3v:%3v,%3v: %v *\n", index, super, sub, e)
} else {
fmt.Fprintf(buffer, " %v\n", e)
}
element = element.next()
}
return string(buffer.Bytes())
}
func (self *Hash) String() string {
return fmt.Sprint(self.ToMap())
}
/*
GetHC returns the key with hashCode that equals k.
Use this when you already have the hash code and don't want to force gotomic to calculate it again.
*/
func (self *Hash) GetHC(hashCode uint32, k Hashable) (rval Thing, ok bool) {
testEntry := newRealEntryWithHashCode(k, nil, hashCode)
bucket := self.getBucketByHashCode(testEntry.hashCode)
hit := (*hashHit)(bucket.search(testEntry))
if hit2 := hit.search(testEntry); hit2.element != nil {
rval = hit2.element.value.(*entry).val()
ok = true
}
return
}
/*
Get returns the value at k and whether it was present in the Hash.
*/
func (self *Hash) Get(k Hashable) (Thing, bool) {
return self.GetHC(k.HashCode(), k)
}
/*
DeleteHC removes the key with hashCode that equals k and returns any value it removed.
Use this when you already have the hash code and don't want to force gotomic to calculate it again.
*/
func (self *Hash) DeleteHC(hashCode uint32, k Hashable) (rval Thing, ok bool) {
testEntry := newRealEntryWithHashCode(k, nil, hashCode)
for {
bucket := self.getBucketByHashCode(testEntry.hashCode)
hit := (*hashHit)(bucket.search(testEntry))
if hit2 := hit.search(testEntry); hit2.element != nil {
if hit2.element.doRemove() {
hit2.left.next()
rval = hit2.element.value.(*entry).val()
ok = true
self.addSize(-1)
break
}
} else {
break
}
}
return
}
/*
Delete removes k from the Hash and returns any value it removed.
*/
func (self *Hash) Delete(k Hashable) (Thing, bool) {
return self.DeleteHC(k.HashCode(), k)
}
/*
PutIfMissing will insert v under k if k contains expected in the Hash, and return whether it inserted anything.
*/
func (self *Hash) PutIfPresent(k Hashable, v Thing, expected Equalable) (rval bool) {
newEntry := newRealEntry(k, v)
for {
bucket := self.getBucketByHashCode(newEntry.hashCode)
hit := (*hashHit)(bucket.search(newEntry))
if hit2 := hit.search(newEntry); hit2.element == nil {
break
} else {
oldEntry := hit2.element.value.(*entry)
oldValuePtr := atomic.LoadPointer(&oldEntry.value)
if expected.Equals(*(*Thing)(oldValuePtr)) {
if atomic.CompareAndSwapPointer(&oldEntry.value, oldValuePtr, unsafe.Pointer(newEntry.value)) {
rval = true
break
}
} else {
break
}
}
}
return
}
/*
PutIfMissing will insert v under k if k was missing from the Hash, and return whether it inserted anything.
*/
func (self *Hash) PutIfMissing(k Hashable, v Thing) (rval bool) {
newEntry := newRealEntry(k, v)
alloc := &element{}
for {
bucket := self.getBucketByHashCode(newEntry.hashCode)
hit := (*hashHit)(bucket.search(newEntry))
if hit2 := hit.search(newEntry); hit2.element == nil {
if hit2.left.addBefore(newEntry, alloc, hit2.right) {
self.addSize(1)
return true
}
} else {
break
}
}
return
}
/*
PutHC will put k and v in the Hash using hashCode and return the overwritten value and whether any value was overwritten.
Use this when you already have the hash code and don't want to force gotomic to calculate it again.
*/
func (self *Hash) PutHC(hashCode uint32, k Hashable, v Thing) (rval Thing, ok bool) {
newEntry := newRealEntryWithHashCode(k, v, hashCode)
alloc := &element{}
for {
bucket := self.getBucketByHashCode(newEntry.hashCode)
hit := (*hashHit)(bucket.search(newEntry))
if hit2 := hit.search(newEntry); hit2.element == nil {
if hit2.left.addBefore(newEntry, alloc, hit2.right) {
self.addSize(1)
break
}
} else {
oldEntry := hit2.element.value.(*entry)
rval = oldEntry.val()
ok = true
atomic.StorePointer(&oldEntry.value, newEntry.value)
break
}
}
return
}
/*
Put k and v in the Hash and return the overwritten value and whether any value was overwritten.
*/
func (self *Hash) Put(k Hashable, v Thing) (rval Thing, ok bool) {
return self.PutHC(k.HashCode(), k, v)
}
func (self *Hash) addSize(i int) {
atomic.AddInt64(&self.size, int64(i))
if atomic.LoadInt64(&self.size) > int64(self.loadFactor*float64(uint32(1)<<atomic.LoadUint32(&self.exponent))) {
self.grow()
}
}
func (self *Hash) grow() {
oldExponent := atomic.LoadUint32(&self.exponent)
newExponent := oldExponent + 1
newBuckets := make([]unsafe.Pointer, 1<<oldExponent)
if atomic.CompareAndSwapPointer(&self.buckets[newExponent], nil, unsafe.Pointer(&newBuckets)) {
atomic.CompareAndSwapUint32(&self.exponent, oldExponent, newExponent)
}
}
func (self *Hash) getPreviousBucketIndex(bucketKey uint32) uint32 {
exp := atomic.LoadUint32(&self.exponent)
return reverse(((bucketKey >> (max_exponent - exp)) - 1) << (max_exponent - exp))
}
func (self *Hash) getBucketByHashCode(hashCode uint32) *element {
return self.getBucketByIndex(hashCode & ((1 << atomic.LoadUint32(&self.exponent)) - 1))
}
func (self *Hash) getBucketIndices(index uint32) (superIndex, subIndex uint32) {
if index > 0 {
superIndex = log2(index)
subIndex = index - (1 << superIndex)
superIndex++
}
return
}
func (self *Hash) getBucketByIndex(index uint32) (bucket *element) {
superIndex, subIndex := self.getBucketIndices(index)
subBuckets := *(*[]unsafe.Pointer)(atomic.LoadPointer(&self.buckets[superIndex]))
for {
bucket = (*element)(atomic.LoadPointer(&subBuckets[subIndex]))
if bucket != nil {
break
}
mockEntry := newMockEntry(index)
if index == 0 {
bucket := &element{nil, mockEntry}
atomic.CompareAndSwapPointer(&subBuckets[subIndex], nil, unsafe.Pointer(bucket))
} else {
prev := self.getPreviousBucketIndex(mockEntry.hashKey)
previousBucket := self.getBucketByIndex(prev)
if hit := previousBucket.search(mockEntry); hit.element == nil {
hit.left.addBefore(mockEntry, &element{}, hit.right)
} else {
atomic.CompareAndSwapPointer(&subBuckets[subIndex], nil, unsafe.Pointer(hit.element))
}
}
}
return bucket
}