Skip to content

Commit

Permalink
Perf boost with some bit twiddling
Browse files Browse the repository at this point in the history
  • Loading branch information
e-dard committed Sep 20, 2017
1 parent 094b92e commit e4013f6
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pkg/bloom/bloom.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Filter struct {
// If m is not a power of two then it is rounded to the next highest power of 2.
func NewFilter(m uint64, k uint64) *Filter {
m = pow2(m)
return &Filter{k: k, b: make([]byte, m/8), mask: m - 1}
return &Filter{k: k, b: make([]byte, m>>3), mask: m - 1}
}

// NewFilterBuffer returns a new instance of a filter using a backing buffer.
Expand Down Expand Up @@ -60,7 +60,7 @@ func (f *Filter) Insert(v []byte) {
h := f.hash(v)
for i := uint64(0); i < f.k; i++ {
loc := f.location(h, i)
f.b[loc/8] |= 1 << (loc % 8)
f.b[loc>>3] |= 1 << (loc & 7)
}
}

Expand All @@ -70,7 +70,7 @@ func (f *Filter) Contains(v []byte) bool {
h := f.hash(v)
for i := uint64(0); i < f.k; i++ {
loc := f.location(h, i)
if f.b[loc/8]&(1<<(loc%8)) == 0 {
if f.b[loc>>3]&(1<<(loc&7)) == 0 {
return false
}
}
Expand Down

0 comments on commit e4013f6

Please sign in to comment.