Skip to content

Commit eb87121

Browse files
authored
core/bloombits: faster generator (#21625)
* core/bloombits: add benchmark * core/bloombits: optimize inserts
1 parent 2b2fd74 commit eb87121

2 files changed

Lines changed: 53 additions & 9 deletions

File tree

‎core/bloombits/generator.go‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,23 @@ func (b *Generator) AddBloom(index uint, bloom types.Bloom) error {
6565
}
6666
// Rotate the bloom and insert into our collection
6767
byteIndex := b.nextSec / 8
68-
bitMask := byte(1) << byte(7-b.nextSec%8)
69-
70-
for i := 0; i < types.BloomBitLength; i++ {
71-
bloomByteIndex := types.BloomByteLength - 1 - i/8
72-
bloomBitMask := byte(1) << byte(i%8)
73-
74-
if (bloom[bloomByteIndex] & bloomBitMask) != 0 {
75-
b.blooms[i][byteIndex] |= bitMask
68+
bitIndex := byte(7 - b.nextSec%8)
69+
for byt := 0; byt < types.BloomByteLength; byt++ {
70+
bloomByte := bloom[types.BloomByteLength-1-byt]
71+
if bloomByte == 0 {
72+
continue
7673
}
74+
base := 8 * byt
75+
b.blooms[base+7][byteIndex] |= ((bloomByte >> 7) & 1) << bitIndex
76+
b.blooms[base+6][byteIndex] |= ((bloomByte >> 6) & 1) << bitIndex
77+
b.blooms[base+5][byteIndex] |= ((bloomByte >> 5) & 1) << bitIndex
78+
b.blooms[base+4][byteIndex] |= ((bloomByte >> 4) & 1) << bitIndex
79+
b.blooms[base+3][byteIndex] |= ((bloomByte >> 3) & 1) << bitIndex
80+
b.blooms[base+2][byteIndex] |= ((bloomByte >> 2) & 1) << bitIndex
81+
b.blooms[base+1][byteIndex] |= ((bloomByte >> 1) & 1) << bitIndex
82+
b.blooms[base][byteIndex] |= (bloomByte & 1) << bitIndex
7783
}
7884
b.nextSec++
79-
8085
return nil
8186
}
8287

‎core/bloombits/generator_test.go‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,42 @@ func TestGenerator(t *testing.T) {
5858
}
5959
}
6060
}
61+
62+
func BenchmarkGenerator(b *testing.B) {
63+
var input [types.BloomBitLength][types.BloomByteLength]byte
64+
b.Run("empty", func(b *testing.B) {
65+
b.ReportAllocs()
66+
b.ResetTimer()
67+
for i := 0; i < b.N; i++ {
68+
// Crunch the input through the generator and verify the result
69+
gen, err := NewGenerator(types.BloomBitLength)
70+
if err != nil {
71+
b.Fatalf("failed to create bloombit generator: %v", err)
72+
}
73+
for j, bloom := range input {
74+
if err := gen.AddBloom(uint(j), bloom); err != nil {
75+
b.Fatalf("bloom %d: failed to add: %v", i, err)
76+
}
77+
}
78+
}
79+
})
80+
for i := 0; i < types.BloomBitLength; i++ {
81+
rand.Read(input[i][:])
82+
}
83+
b.Run("random", func(b *testing.B) {
84+
b.ReportAllocs()
85+
b.ResetTimer()
86+
for i := 0; i < b.N; i++ {
87+
// Crunch the input through the generator and verify the result
88+
gen, err := NewGenerator(types.BloomBitLength)
89+
if err != nil {
90+
b.Fatalf("failed to create bloombit generator: %v", err)
91+
}
92+
for j, bloom := range input {
93+
if err := gen.AddBloom(uint(j), bloom); err != nil {
94+
b.Fatalf("bloom %d: failed to add: %v", i, err)
95+
}
96+
}
97+
}
98+
})
99+
}

0 commit comments

Comments
 (0)