@@ -2,24 +2,32 @@ package cuckoo
22
33import (
44 "encoding/binary"
5- "math/rand "
5+ "math/bits "
66
7- metro "github.com/dgryski/go-metro"
7+ "github.com/zeebo/wyhash"
8+ "github.com/zeebo/xxh3"
89)
910
11+ var altHash [maxFingerprint + 1 ]uint
12+
13+ func init () {
14+ b := make ([]byte , 2 )
15+ for i := 0 ; i < maxFingerprint + 1 ; i ++ {
16+ binary .LittleEndian .PutUint16 (b , uint16 (i ))
17+ altHash [i ] = (uint (xxh3 .Hash (b )))
18+ }
19+ }
20+
1021// randi returns either i1 or i2 randomly.
11- func randi (i1 , i2 uint ) uint {
12- if rand . Int31 () % 2 == 0 {
22+ func randi (rng * wyhash. RNG , i1 , i2 uint ) uint {
23+ if rng . Uint64 () & 1 == 0 {
1324 return i1
1425 }
1526 return i2
1627}
1728
1829func getAltIndex (fp fingerprint , i uint , bucketIndexMask uint ) uint {
19- b := make ([]byte , 2 )
20- binary .LittleEndian .PutUint16 (b , uint16 (fp ))
21- hash := uint (metro .Hash64 (b , 1337 ))
22- return (i ^ hash ) & bucketIndexMask
30+ return (i ^ altHash [fp ]) & bucketIndexMask
2331}
2432
2533func getFingerprint (hash uint64 ) fingerprint {
@@ -32,21 +40,13 @@ func getFingerprint(hash uint64) fingerprint {
3240
3341// getIndexAndFingerprint returns the primary bucket index and fingerprint to be used
3442func getIndexAndFingerprint (data []byte , bucketIndexMask uint ) (uint , fingerprint ) {
35- hash := metro . Hash64 (data , 1337 )
43+ hash := xxh3 . Hash (data )
3644 f := getFingerprint (hash )
3745 // Use least significant bits for deriving index.
3846 i1 := uint (hash ) & bucketIndexMask
3947 return i1 , f
4048}
4149
4250func getNextPow2 (n uint64 ) uint {
43- n --
44- n |= n >> 1
45- n |= n >> 2
46- n |= n >> 4
47- n |= n >> 8
48- n |= n >> 16
49- n |= n >> 32
50- n ++
51- return uint (n )
51+ return uint (1 << bits .Len64 (n - 1 ))
5252}
0 commit comments