Skip to content

Commit

Permalink
Moving kickout bucket selection to fastrandn
Browse files Browse the repository at this point in the history
For faster initialization through avoiding mutex on global random.
Benchmark:

goos: linux
goarch: amd64
pkg: github.com/panmari/cuckoofilter
cpu: 12th Gen Intel(R) Core(TM) i7-1265U
                 │ master.bench │           fastrandn.bench           │
                 │    sec/op    │   sec/op     vs base                │
Filter_Insert-12    36.74n ± 3%   33.76n ± 1%  -8.11% (p=0.000 n=100)
  • Loading branch information
panmari committed Oct 4, 2023
1 parent d389eb2 commit 53e0653
Showing 1 changed file with 1 addition and 2 deletions.
3 changes: 1 addition & 2 deletions cuckoofilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/binary"
"fmt"
"math/rand"
)

// maxCuckooKickouts is the maximum number of times reinsert
Expand Down Expand Up @@ -85,7 +84,7 @@ func (cf *Filter) insert(fp fingerprint, i uint) bool {

func (cf *Filter) reinsert(fp fingerprint, i uint) bool {
for k := 0; k < maxCuckooKickouts; k++ {
j := rand.Intn(bucketSize)
j := fastrandn(bucketSize)
// Swap fingerprint with bucket entry.
cf.buckets[i][j], fp = fp, cf.buckets[i][j]

Expand Down

0 comments on commit 53e0653

Please sign in to comment.