use criterion::{BenchmarkId, Criterion, Throughput, black_box, criterion_group, criterion_main};
use fastbloom::BloomFilter;
use feldera_modular_bloom::{ModularBloomFilter, ModularBloomFilterBuilder, ModuleLayout};
const DEFAULT_FP: f64 = 1e-4;
const SMALL_KEYS: u64 = 1_000_000;
const LARGE_KEYS: u64 = 10_000_000;
const PROBES: u64 = 1 << 16;
#[path = "../tests/bloom_common.rs"]
mod bloom_common;
use bloom_common::{absent, present};
fn build_monolithic(keys: u64) -> BloomFilter {
let mut filter = BloomFilter::with_false_pos(DEFAULT_FP)
.seed(&42)
.expected_items(keys as usize);
for i in 0..keys {
filter.insert_hash(present(i));
}
filter
}
fn build_modular(keys: u64, modules: u32) -> ModularBloomFilter {
let layout = ModuleLayout::for_keys(keys, DEFAULT_FP, modules).unwrap();
let mut builder = ModularBloomFilterBuilder::new(layout);
for i in 0..keys {
builder.insert_hash(present(i));
}
builder.finish()
}
fn construction(c: &mut Criterion) {
let mut group = c.benchmark_group("construction");
let keys = SMALL_KEYS;
group.throughput(Throughput::Elements(keys));
group.sample_size(10);
group.bench_function("monolithic_k13", |b| {
b.iter(|| black_box(build_monolithic(keys).num_bits()))
});
for modules in [1u32, 4, 8] {
group.bench_with_input(
BenchmarkId::new("modular", format!("d{modules}")),
&modules,
|b, &modules| b.iter(|| black_box(build_modular(keys, modules).resident_bytes())),
);
}
group.finish();
}
fn probe(c: &mut Criterion, keys: u64, label: &str) {
let monolithic = build_monolithic(keys);
let modular: Vec<(u32, ModularBloomFilter)> = [1u32, 4, 8]
.into_iter()
.map(|modules| (modules, build_modular(keys, modules)))
.collect();
for (case, hash) in [("absent", absent as fn(u64) -> u64), ("present", present)] {
let mut group = c.benchmark_group(format!("probe_{case}_{label}"));
group.throughput(Throughput::Elements(PROBES));
group.bench_function("monolithic_k13", |b| {
b.iter(|| {
let mut hits = 0u64;
for i in 0..PROBES {
hits += u64::from(monolithic.contains_hash(black_box(hash(i))));
}
hits
})
});
for (modules, filter) in &modular {
group.bench_with_input(
BenchmarkId::new("modular", format!("d{modules}")),
filter,
|b, filter| {
b.iter(|| {
let mut hits = 0u64;
for i in 0..PROBES {
hits += u64::from(filter.contains_hash(black_box(hash(i))));
}
hits
})
},
);
}
group.finish();
}
}
fn ladder(c: &mut Criterion, keys: u64, label: &str) {
let full = build_modular(keys, 4);
for (case, hash) in [("absent", absent as fn(u64) -> u64), ("present", present)] {
let mut group = c.benchmark_group(format!("ladder_{case}_{label}"));
group.throughput(Throughput::Elements(PROBES));
for resident in 1..=4u32 {
let mut filter = full.clone();
filter.truncate(resident);
group.bench_with_input(
BenchmarkId::from_parameter(format!(
"d4_resident{resident}_{}KiB",
filter.resident_bytes() / 1024
)),
&filter,
|b, filter| {
b.iter(|| {
let mut hits = 0u64;
for i in 0..PROBES {
hits += u64::from(filter.contains_hash(black_box(hash(i))));
}
hits
})
},
);
}
group.finish();
}
}
fn mixed(c: &mut Criterion) {
let keys = LARGE_KEYS;
let monolithic = build_monolithic(keys);
let modular = build_modular(keys, 4);
let mut group = c.benchmark_group("probe_mixed_large");
group.throughput(Throughput::Elements(PROBES));
for hit_percent in [1u64, 10, 50] {
let probe_hash = move |i: u64| {
if i % 100 < hit_percent {
present(i % keys)
} else {
absent(i)
}
};
group.bench_with_input(
BenchmarkId::new("monolithic_k13", format!("{hit_percent}pct_present")),
&hit_percent,
|b, _| {
b.iter(|| {
let mut hits = 0u64;
for i in 0..PROBES {
hits += u64::from(monolithic.contains_hash(black_box(probe_hash(i))));
}
hits
})
},
);
group.bench_with_input(
BenchmarkId::new("modular_d4", format!("{hit_percent}pct_present")),
&hit_percent,
|b, _| {
b.iter(|| {
let mut hits = 0u64;
for i in 0..PROBES {
hits += u64::from(modular.contains_hash(black_box(probe_hash(i))));
}
hits
})
},
);
}
group.finish();
}
fn resize(c: &mut Criterion) {
let keys = LARGE_KEYS;
let layout = ModuleLayout::for_keys(keys, DEFAULT_FP, 4).unwrap();
let full = build_modular(keys, 4);
let words: Vec<u64> = full.modules().concat();
let per = layout.words_per_module() as usize;
let mut group = c.benchmark_group("resize_large");
group.sample_size(20);
group.bench_function("truncate_4_to_2", |b| {
b.iter_batched(
|| full.clone(),
|mut filter| {
filter.truncate(2);
black_box(filter.resident_bytes())
},
criterion::BatchSize::LargeInput,
)
});
group.bench_function("load_prefix_2_of_4", |b| {
b.iter(|| {
let filter = ModularBloomFilter::from_modules(layout, &words[..2 * per]).unwrap();
black_box(filter.resident_bytes())
})
});
group.bench_function("load_all_4", |b| {
b.iter(|| {
let filter = ModularBloomFilter::from_modules(layout, &words).unwrap();
black_box(filter.resident_bytes())
})
});
group.bench_function("push_one_module", |b| {
b.iter_batched(
|| {
let mut filter = full.clone();
filter.truncate(3);
filter
},
|mut filter| {
filter.push_module(&words[3 * per..4 * per]).unwrap();
black_box(filter.resident_bytes())
},
criterion::BatchSize::LargeInput,
)
});
group.finish();
}
fn all(c: &mut Criterion) {
construction(c);
probe(c, SMALL_KEYS, "small");
probe(c, LARGE_KEYS, "large");
ladder(c, LARGE_KEYS, "large");
mixed(c);
resize(c);
}
criterion_group!(benches, all);
criterion_main!(benches);