Skip to content

Commit 391231c

Browse files
committed
unroll bucket loops
1 parent 056df0e commit 391231c

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

bucket.go

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,41 +21,46 @@ const (
2121
// insert a fingerprint into a bucket. Returns true if there was enough space and insertion succeeded.
2222
// Note it allows inserting the same fingerprint multiple times.
2323
func (b *bucket) insert(fp fingerprint) bool {
24-
for i, tfp := range b {
25-
if tfp == nullFp {
26-
b[i] = fp
27-
return true
28-
}
24+
if i := b.index(nullFp); i != 4 {
25+
b[i] = fp
26+
return true
2927
}
3028
return false
3129
}
3230

3331
// delete a fingerprint from a bucket.
3432
// Returns true if the fingerprint was present and successfully removed.
3533
func (b *bucket) delete(fp fingerprint) bool {
36-
for i, tfp := range b {
37-
if tfp == fp {
38-
b[i] = nullFp
39-
return true
40-
}
34+
if i := b.index(fp); i != 4 {
35+
b[i] = nullFp
36+
return true
4137
}
4238
return false
4339
}
4440

4541
func (b *bucket) contains(needle fingerprint) bool {
46-
for _, fp := range b {
47-
if fp == needle {
48-
return true
49-
}
42+
return b.index(needle) != 4
43+
}
44+
45+
func (b *bucket) index(needle fingerprint) uint8 {
46+
if b[0] == needle {
47+
return 0
5048
}
51-
return false
49+
if b[1] == needle {
50+
return 1
51+
}
52+
if b[2] == needle {
53+
return 2
54+
}
55+
if b[3] == needle {
56+
return 3
57+
}
58+
return 4
5259
}
5360

5461
// reset deletes all fingerprints in the bucket.
5562
func (b *bucket) reset() {
56-
for i := range b {
57-
b[i] = nullFp
58-
}
63+
*b = [bucketSize]fingerprint{nullFp, nullFp, nullFp, nullFp}
5964
}
6065

6166
func (b *bucket) String() string {

0 commit comments

Comments
 (0)