Skip to content

Commit bedd749

Browse files
committed
remove mutex
1 parent 59a8672 commit bedd749

File tree

6 files changed

+9
-45
lines changed

6 files changed

+9
-45
lines changed

v2/binarymarshaler.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,21 @@ func (c *counter) Write(p []byte) (n int, err error) {
4545
// MarshallToWriter marshalls the filter into the given io.Writer
4646
// Binary layout (Little Endian):
4747
//
48-
// k 1 uint64
49-
// n 1 uint64
50-
// m 1 uint64
51-
// keys [k]uint64
52-
// bits [(m+63)/64]uint64
53-
// hash sha384 (384 bits == 48 bytes)
54-
//
55-
// size = (3 + k + (m+63)/64) * 8 bytes
48+
// k 1 uint64
49+
// n 1 uint64
50+
// m 1 uint64
51+
// keys [k]uint64
52+
// bits [(m+63)/64]uint64
53+
// hash sha384 (384 bits == 48 bytes)
5654
//
55+
// size = (3 + k + (m+63)/64) * 8 bytes
5756
func (f *Filter) MarshallToWriter(out io.Writer) (int, [sha512.Size384]byte, error) {
5857
var (
5958
c = &counter{0}
6059
hasher = sha512.New384()
6160
mw = io.MultiWriter(out, hasher, c)
6261
hash [sha512.Size384]byte
6362
)
64-
f.lock.RLock()
65-
defer f.lock.RUnlock()
6663

6764
if _, err := mw.Write(headerMagic); err != nil {
6865
return c.bytes, hash, err

v2/binaryunmarshaler.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,6 @@ func (f *Filter) UnmarshalBinary(data []byte) (err error) {
9898
}
9999

100100
func (f *Filter) UnmarshalFromReader(input io.Reader) (n int64, err error) {
101-
f.lock.Lock()
102-
defer f.lock.Unlock()
103-
104101
buf := &hashingReader{
105102
reader: input,
106103
hasher: sha512.New384(),

v2/bloomfilter.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package v2
1818
import (
1919
"errors"
2020
"hash"
21-
"sync"
2221
)
2322

2423
var (
@@ -27,7 +26,6 @@ var (
2726

2827
// Filter is an opaque Bloom filter type
2928
type Filter struct {
30-
lock sync.RWMutex
3129
bits []uint64
3230
keys []uint64
3331
m uint64 // number of bits the "bits" field should recognize
@@ -58,8 +56,6 @@ const rotation = 17
5856
// Adds an already hashes item to the filter.
5957
// Identical to Add (but slightly faster)
6058
func (f *Filter) AddHash(hash uint64) {
61-
f.lock.Lock()
62-
defer f.lock.Unlock()
6359
var (
6460
i uint64
6561
)
@@ -74,8 +70,6 @@ func (f *Filter) AddHash(hash uint64) {
7470
// ContainsHash tests if f contains the (already hashed) key
7571
// Identical to Contains but slightly faster
7672
func (f *Filter) ContainsHash(hash uint64) bool {
77-
f.lock.RLock()
78-
defer f.lock.RUnlock()
7973
var (
8074
i uint64
8175
r = uint64(1)
@@ -97,9 +91,6 @@ func (f *Filter) Contains(v hash.Hash64) bool {
9791

9892
// Copy f to a new Bloom filter
9993
func (f *Filter) Copy() (*Filter, error) {
100-
f.lock.RLock()
101-
defer f.lock.RUnlock()
102-
10394
out, err := f.NewCompatible()
10495
if err != nil {
10596
return nil, err
@@ -115,9 +106,6 @@ func (f *Filter) UnionInPlace(f2 *Filter) error {
115106
return errors.New("incompatible bloom filters")
116107
}
117108

118-
f.lock.Lock()
119-
defer f.lock.Unlock()
120-
121109
for i, bitword := range f2.bits {
122110
f.bits[i] |= bitword
123111
}
@@ -132,9 +120,6 @@ func (f *Filter) Union(f2 *Filter) (out *Filter, err error) {
132120
return nil, errors.New("incompatible bloom filters")
133121
}
134122

135-
f.lock.RLock()
136-
defer f.lock.RUnlock()
137-
138123
out, err = f.NewCompatible()
139124
if err != nil {
140125
return nil, err

v2/fileio.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ func (f *Filter) ReadFrom(r io.Reader) (n int64, err error) {
3030
if err != nil {
3131
return -1, err
3232
}
33-
f.lock.Lock()
34-
defer f.lock.Unlock()
3533
f.m = f2.m
3634
f.n = f2.n
3735
f.bits = f2.bits
@@ -68,9 +66,6 @@ func ReadFile(filename string) (f *Filter, n int64, err error) {
6866

6967
// WriteTo a Writer w from lossless-compressed Bloom Filter f
7068
func (f *Filter) WriteTo(w io.Writer) (n int64, err error) {
71-
f.lock.RLock()
72-
defer f.lock.RUnlock()
73-
7469
rawW := gzip.NewWriter(w)
7570
defer rawW.Close()
7671

v2/iscompatible.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,6 @@ func noBranchCompareUint64s(b0, b1 []uint64) uint64 {
2626

2727
// IsCompatible is true if f and f2 can be Union()ed together
2828
func (f *Filter) IsCompatible(f2 *Filter) bool {
29-
f.lock.RLock()
30-
defer f.lock.RUnlock()
31-
32-
f2.lock.RLock()
33-
defer f2.lock.RUnlock()
34-
3529
// 0 is true, non-0 is false
3630
compat := f.M() ^ f2.M()
3731
compat |= f.K() ^ f2.K()

v2/statistics.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,18 @@ func CountBitsUint64s(b []uint64) int {
3131

3232
// PreciseFilledRatio is an exhaustive count # of 1's
3333
func (f *Filter) PreciseFilledRatio() float64 {
34-
f.lock.RLock()
35-
defer f.lock.RUnlock()
3634
return float64(CountBitsUint64s(f.bits)) / float64(f.M())
3735
}
3836

3937
// N is how many elements have been inserted
4038
// (actually, how many Add()s have been performed?)
4139
func (f *Filter) N() uint64 {
42-
f.lock.RLock()
43-
defer f.lock.RUnlock()
44-
4540
return f.n
4641
}
4742

4843
// FalsePosititveProbability is the upper-bound probability of false positives
49-
// (1 - exp(-k*(n+0.5)/(m-1))) ** k
44+
//
45+
// (1 - exp(-k*(n+0.5)/(m-1))) ** k
5046
func (f *Filter) FalsePosititveProbability() float64 {
5147
k := float64(f.K())
5248
n := float64(f.N())

0 commit comments

Comments
 (0)