@@ -2,24 +2,32 @@ package cuckoo
2
2
3
3
import (
4
4
"encoding/binary"
5
- "math/rand "
5
+ "math/bits "
6
6
7
- metro "github.com/dgryski/go-metro"
7
+ "github.com/zeebo/wyhash"
8
+ "github.com/zeebo/xxh3"
8
9
)
9
10
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
+
10
21
// 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 {
13
24
return i1
14
25
}
15
26
return i2
16
27
}
17
28
18
29
func 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
23
31
}
24
32
25
33
func getFingerprint (hash uint64 ) fingerprint {
@@ -32,21 +40,13 @@ func getFingerprint(hash uint64) fingerprint {
32
40
33
41
// getIndexAndFingerprint returns the primary bucket index and fingerprint to be used
34
42
func getIndexAndFingerprint (data []byte , bucketIndexMask uint ) (uint , fingerprint ) {
35
- hash := metro . Hash64 (data , 1337 )
43
+ hash := xxh3 . Hash (data )
36
44
f := getFingerprint (hash )
37
45
// Use least significant bits for deriving index.
38
46
i1 := uint (hash ) & bucketIndexMask
39
47
return i1 , f
40
48
}
41
49
42
50
func 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 ))
52
52
}
0 commit comments