Skip to content

Commit 34cf1f7

Browse files
authored
fix: increase default cuckoo filter fingerprint size (#2636)
Calculate the default fingerprint size from the `f ≥ log2(1/ε) + log2(2b)` equation from the cucko filter paper. Also lowers the reliable threshold to 90% from 95% as sometimes we go over the threshold for smaller filter sizes.
1 parent aa5528f commit 34cf1f7

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

packages/utils/src/filters/cuckoo-filter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export class CuckooFilter implements Filter {
150150
}
151151

152152
get reliable (): boolean {
153-
return Math.floor(100 * (this.count / this.filterSize)) <= 95
153+
return Math.floor(100 * (this.count / this.filterSize)) <= 90
154154
}
155155
}
156156

@@ -182,7 +182,7 @@ export function optimize (maxItems: number, errorRate: number = 0.001): CuckooFi
182182

183183
// https://stackoverflow.com/questions/57555236/how-to-size-a-cuckoo-filter/57617208#57617208
184184
const filterSize = Math.round(maxItems / load)
185-
const fingerprintSize = Math.min(Math.ceil(Math.log(filterSize / bucketSize)) + 2, MAX_FINGERPRINT_SIZE)
185+
const fingerprintSize = Math.min(Math.ceil(Math.log2(1 / errorRate) + Math.log2(2 * bucketSize)), MAX_FINGERPRINT_SIZE)
186186

187187
return {
188188
filterSize,

0 commit comments

Comments
 (0)