Skip to content

Commit

Permalink
fix: remove while loop for setbit & getbit (#2687)
Browse files Browse the repository at this point in the history
Constant time `setbit` and `getbit` in the bloom filter - was hitting performance issues when using a 30mb bloom filter
  • Loading branch information
dozyio authored Sep 10, 2024
1 parent 2022036 commit a142bb6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
26 changes: 26 additions & 0 deletions packages/utils/benchmark/filter/bloom-filter.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* eslint-disable no-console */
const Benchmark = require('benchmark')

const suite = new Benchmark.Suite('bloom-filter')

const bits = [8, 64, 512, 4096, 32768, 262144, 2097152, 251658240]

bits.forEach((bit) => {
suite.add(`Loop ${bit}bits`, () => {
let pos = 0
let shift = bit
while (shift > 7) {
pos++
shift -= 8
}
})

suite.add(`Math Ops ${bit}bits`, () => {
_ = Math.floor(bit / 8)
_ = bit % 8
})
})

suite
.on('cycle', (event) => console.log(String(event.target)))
.run({ async: true })
1 change: 1 addition & 0 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@
"devDependencies": {
"@types/netmask": "^2.0.5",
"aegir": "^44.0.1",
"benchmark": "^2.1.4",
"delay": "^6.0.0",
"it-all": "^3.0.6",
"it-drain": "^3.0.7",
Expand Down
16 changes: 4 additions & 12 deletions packages/utils/src/filters/bloom-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,17 @@ export class BloomFilter implements Filter {
}

setbit (bit: number): void {
let pos = 0
let shift = bit
while (shift > 7) {
pos++
shift -= 8
}
const pos = Math.floor(bit / 8)
const shift = bit % 8

let bitfield = this.buffer[pos]
bitfield |= (0x1 << shift)
this.buffer[pos] = bitfield
}

getbit (bit: number): boolean {
let pos = 0
let shift = bit
while (shift > 7) {
pos++
shift -= 8
}
const pos = Math.floor(bit / 8)
const shift = bit % 8

const bitfield = this.buffer[pos]
return (bitfield & (0x1 << shift)) !== 0
Expand Down

0 comments on commit a142bb6

Please sign in to comment.