Internals

raftdb-bloom, the cache-local bloom filter

How a one-cache-line bloom filter answers "definitely absent", its sizing, stored form and measured false-positive rate.

raftdb-bloom answers "is this key definitely absent?" with one cache-line read. It uses only the standard library and has no unsafe code. The design follows the RocksDB study. The LSM engine uses one filter per sorted file.

Design#

  • The filter is a Vec<Block>; a block is 64 bytes ([u64; 8], 64-aligned), that is 512 bits.
  • The input is a 64-bit hash chosen by the caller. The low 32 bits pick the block (fast_range: multiply-shift, no division). The high 32 bits give the probes inside that block: probe i uses bit h >> 23 (9 bits), then h *= 0x9e3779b9.
  • The probe count k comes from choose_probes(millibits), RocksDB's measured table (10 bits per key gives 6).
  • Size: ceil(keys * bits_per_key / 512) blocks, at least one. At 10 bits per key that is 1.25 bytes per key.

API#

CallBehaviour
Bloom::new(keys, bits_per_key)An empty filter sized for keys.
insert(hash)Sets k bits in one block.
may_contain(hash)false means definitely absent; true means present or a false positive (about 1 % at 10 bits per key).
to_bytes() / from_bytes()Stored form [k: u8][blocks as u64 little-endian words]; from_bytes rejects a bad length or k.

A filter has no false negatives. It cannot delete keys and does not grow: a new one is built when the key count changes materially (it is rebuilt with each on-disk run).

Measured#

0.97 % false positives and 1.25 bytes per key at 10 bits per key; 71.9 M absent-key checks per second on a 10 MB filter.

Tests#

No false negatives and under 1.3 % false positives over 1,000,000 absent keys; the probe table equals RocksDB's; the stored form round-trips and rejects garbage; stored bytes are identical on every platform.

Open#

A present-key lookup benchmark, batched prefetch lookup, and an optional SIMD path (which would need an equivalence test).