-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbbhash.go
433 lines (364 loc) · 9.5 KB
/
bbhash.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
// bbhash.go - fast minimal perfect hashing for massive key sets
//
// Implements the BBHash algorithm in: https://arxiv.org/abs/1702.03154
//
// Inspired by D Gryski's implementation of bbHash (/~https://github.com/dgryski/go-boomphf)
//
// (c) Sudhi Herle 2018
//
// License GPLv2
// If you need a commercial license for this work, please contact
// the author.
//
// This software does not come with any express or implied
// warranty; it is provided "as is". No claim is made to its
// suitability for any purpose.
package mph
import (
"bytes"
"fmt"
"io"
"os"
"runtime"
"sync"
)
// bbHash represents a computed minimal perfect hash for a given set of keys using
// the bbHash algorithm: https://arxiv.org/abs/1702.03154.
type bbHash struct {
bits []*bitVector
ranks []uint64
salt uint64
g float64 // gamma - rankvector size expansion factor
n int // number of keys
}
// state used by go-routines when we concurrentize the algorithm
type state struct {
sync.Mutex
A *bitVector
coll *bitVector
redo []uint64
lvl uint32
bb *bbHash
}
// Gamma is an expansion factor for each of the bitvectors we build.
// Empirically, 2.0 is found to be a good balance between speed and
// space usage. See paper for more details.
const _Gamma float64 = 2.0
// Maximum number of attempts (level) at making a perfect hash function.
// Per the paper, each successive level exponentially reduces the
// probability of collision.
const _MaxLevel uint32 = 4000
// Minimum number of keys before bbhash switches to a concurrent
// construction algorithm
const MinParallelKeys int = 20000
// set to true for verbose debug
const debug bool = false
type bbHashBuilder struct {
keys []uint64
g float64
}
// NewBBHashBuilder enables creation of a minimal perfect hash function via the
// BBHash algorithm. Once created, callers can add keys to it before Freezing
// the MPH and generating a constant time lookup table. The parameter 'g'
// is "Gamma" from the paper; the recommended value is >= 2.0; larger values
// increase the constructed table size and also decreases probability of
// construction failure.
// Once the construction is frozen, callers can use "Find()" to find the
// unique mapping for each key in 'keys'.
func NewBBHashBuilder(g float64) (MPHBuilder, error) {
b := &bbHashBuilder{
keys: make([]uint64, 0, 1024),
g: g,
}
return b, nil
}
// Add a new key to the MPH builder
func (b *bbHashBuilder) Add(key uint64) error {
b.keys = append(b.keys, key)
return nil
}
// New creates a new minimal hash function to represent the keys in 'keys'.
// This constructor selects a faster concurrent algorithm if the number of
// keys are greater than 'MinParallelKeys'.
// Once the construction is complete, callers can use "Find()" to find the
// unique mapping for each key in 'keys'.
func (b *bbHashBuilder) Freeze() (MPH, error) {
bb := &bbHash{
salt: rand64(),
g: b.g,
n: len(b.keys),
}
s := bb.newState()
var err error
if bb.n > MinParallelKeys {
err = s.concurrent(b.keys)
} else {
err = s.singleThread(b.keys)
}
if err != nil {
return nil, err
}
return bb, nil
}
func (bb *bbHash) Len() int {
return bb.n
}
// Find returns a unique integer representing the minimal hash for key 'k'.
// The return value is meaningful ONLY for keys in the original key set (provided
// at the time of construction of the minimal-hash).
// If the key is in the original key-set
func (bb *bbHash) Find(k uint64) (uint64, bool) {
for lvl, bv := range bb.bits {
i := bhash(k, bb.salt, uint32(lvl)) % bv.Size()
if !bv.IsSet(i) {
continue
}
rank := 1 + bb.ranks[lvl] + bv.Rank(i)
// bbhash returns a 1-based index.
return rank - 1, true
}
return 0, false
}
// DumpMeta dumps the metadata of the underlying bbhash
func (bb *bbHash) DumpMeta(w io.Writer) {
var b bytes.Buffer
b.WriteString(fmt.Sprintf("bbHash: salt %#x; %d levels\n", bb.salt, len(bb.bits)))
for i, bv := range bb.bits {
sz := humansize(bv.Words() * 8)
b.WriteString(fmt.Sprintf(" %d: %d bits (%s)\n", i, bv.Size(), sz))
}
w.Write(b.Bytes())
}
// NewSerial creates a new minimal hash function to represent the keys in 'keys'.
// This constructor explicitly uses a single-threaded (non-concurrent) construction.
func newSerial(g float64, keys []uint64) (*bbHash, error) {
if g <= 1.0 {
g = 2.0
}
bb := &bbHash{
salt: rand64(),
g: g,
n: len(keys),
}
s := bb.newState()
err := s.singleThread(keys)
if err != nil {
return nil, err
}
return bb, nil
}
// NewConcurrent creates a new minimal hash function to represent the keys in 'keys'.
// This gives callers explicit control over when to use a concurrent algorithm vs. serial.
func newConcurrent(g float64, keys []uint64) (*bbHash, error) {
if g <= 1.0 {
g = 2.0
}
bb := &bbHash{
salt: rand64(),
g: g,
n: len(keys),
}
s := bb.newState()
err := s.concurrent(keys)
if err != nil {
return nil, err
}
return bb, nil
}
// return optimal size for bitvector
func (bb *bbHash) bvSize() uint64 {
return uint64(float64(bb.n) * bb.g)
}
// setup state for serial or concurrent execution
func (bb *bbHash) newState() *state {
sz := bb.bvSize()
s := &state{
A: newBitVector(sz),
coll: newBitVector(sz),
redo: make([]uint64, 0, sz),
bb: bb,
}
//printf("bbhash: salt %#x, gamma %4.2f %d keys A %d bits", bb.salt, bb.g, nkeys, s.A.Size())
return s
}
// single-threaded serial invocation of the bbHash algorithm
func (s *state) singleThread(keys []uint64) error {
A := s.A
for {
//printf("lvl %d: %d keys A %d bits", s.lvl, len(keys), A.Size())
preprocess(s, keys)
A.Reset()
assign(s, keys)
keys, A = s.nextLevel()
if keys == nil {
break
}
if s.lvl > _MaxLevel {
return fmt.Errorf("can't find minimal perf hash after %d tries", s.lvl)
}
}
s.bb.preComputeRank()
return nil
}
// run the bbHash algorithm concurrently on a sharded set of keys.
// entry: len(keys) > MinParallelKeys
func (s *state) concurrent(keys []uint64) error {
ncpu := runtime.NumCPU()
A := s.A
for {
nkey := uint64(len(keys))
z := nkey / uint64(ncpu)
r := nkey % uint64(ncpu)
var wg sync.WaitGroup
// Pre-process keys and detect colliding entries
wg.Add(ncpu)
for i := 0; i < ncpu; i++ {
i := i
x := z * uint64(i)
y := x + z
if i == (ncpu - 1) {
y += r
}
go func(x, y uint64) {
//printf("lvl %d: cpu %d; Pre-process shard %d:%d", s.lvl, i, x, y)
preprocess(s, keys[x:y])
wg.Done()
}(x, y)
}
// synchronization point
wg.Wait()
// Assignment step
A.Reset()
wg.Add(ncpu)
for i := 0; i < ncpu; i++ {
i := i
x := z * uint64(i)
y := x + z
if i == (ncpu - 1) {
y += r
}
go func(x, y uint64) {
//printf("lvl %d: cpu %d; Assign shard %d:%d", s.lvl, i, x, y)
assign(s, keys[x:y])
wg.Done()
}(x, y)
}
// synchronization point #2
wg.Wait()
keys, A = s.nextLevel()
if keys == nil {
break
}
// Now, see if we have enough keys to concurrentize
if len(keys) < MinParallelKeys {
return s.singleThread(keys)
}
if s.lvl > _MaxLevel {
return fmt.Errorf("can't find minimal perf hash after %d tries", s.lvl)
}
}
s.bb.preComputeRank()
return nil
}
// pre-process to detect colliding bits
func preprocess(s *state, keys []uint64) {
A := s.A
coll := s.coll
salt := s.bb.salt
sz := A.Size()
//printf("lvl %d => sz %d", s.lvl, sz)
for _, k := range keys {
//printf(" key %#x..", k)
i := bhash(k, salt, s.lvl) % sz
if coll.IsSet(i) {
continue
}
if A.IsSet(i) {
coll.Set(i)
continue
}
A.Set(i)
}
}
// phase-2 -- assign non-colliding bits; this too can be concurrentized
// the redo-list can be local until we finish scanning all the keys.
// XXX "A" could also be kept local and finally merged via bitwise-union.
func assign(s *state, keys []uint64) {
A := s.A
coll := s.coll
salt := s.bb.salt
sz := A.Size()
redo := make([]uint64, 0, len(keys)/4)
for _, k := range keys {
i := bhash(k, salt, s.lvl) % sz
if coll.IsSet(i) {
redo = append(redo, k)
continue
}
A.Set(i)
}
if len(redo) > 0 {
s.appendRedo(redo)
}
}
// add the local copy of 'redo' list to the central list.
func (s *state) appendRedo(k []uint64) {
s.Lock()
s.redo = append(s.redo, k...)
//printf("lvl %d: redo += %d keys", s.lvl, len(k))
s.Unlock()
}
// append the current A to the bits vector and begin new iteration
// return new keys and a new A.
// NB: This is *always* called from a single-threaded context
//
// (i.e., synchronization point).
func (s *state) nextLevel() ([]uint64, *bitVector) {
s.bb.bits = append(s.bb.bits, s.A)
s.A = nil
//printf("lvl %d: next-step: remaining: %d keys", s.lvl, len(s.redo))
keys := s.redo
if len(keys) == 0 {
return nil, nil
}
s.redo = s.redo[:0]
s.A = newBitVector(s.bb.bvSize())
s.coll.Reset()
s.lvl++
return keys, s.A
}
// Precompute ranks for each level so we can answer queries quickly.
func (bb *bbHash) preComputeRank() {
var pop uint64
bb.ranks = make([]uint64, len(bb.bits))
// We omit the first level in rank calculation; this avoids a special
// case in Find() when we are looking at elements in level-0.
for l, bv := range bb.bits {
bb.ranks[l] = pop
pop += bv.ComputeRank()
}
}
// One round of Zi Long Tan's superfast hash
func bhash(key, salt uint64, lvl uint32) uint64 {
const m uint64 = 0x880355f21e6d1965
var h uint64 = m
h ^= mix(key)
h *= m
h ^= mix(salt)
h *= m
h ^= mix(uint64(lvl))
h *= m
h = mix(h)
return h
}
func printf(f string, v ...interface{}) {
if !debug {
return
}
s := fmt.Sprintf(f, v...)
if n := len(s); s[n-1] != '\n' {
s += "\n"
}
os.Stdout.WriteString(s)
os.Stdout.Sync()
}