Unverified Commit a9c23750 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

turbo-persistence: 32 bit fingerprints for the used keys AMQFs (#99333)

## What

decrease the size of the used key amqfs to 32 bits

## Why
Save disk space, compaction will also be a bit faster due to smaller
filters which are cheaper to query and merge.

All filters keep the same fingerprint size, so their union still uses
qfilter's sorted merge.

## Verification

- Re-encoding the used keys AMQFs of the final cache of a 31-build
vercel-site run (GC on): **61.8 MB → 17.8 MB** (7.96 → 2.29 bytes per
key), about 1.5% of the 2.96 GB cache. GC off: 58.9 → 16.9 MB.
- `cargo test -p turbo-persistence` and `cargo test -p
turbo-tasks-backend --tests` pass.

<!-- NEXT_JS_LLM -->
parent eaa2f075
......@@ -915,14 +915,11 @@ impl<S: ParallelScheduler, const FAMILIES: usize> TurboPersistence<S, FAMILIES>
// len is only a snapshot at that time and it can change while we create the filter.
// So we give it 5% more space to make resizes less likely.
let initial_capacity = set.len() * 20 / 19;
// TODO: Using u64::BITS as fingerprint size is wasteful for a
// probabilistic membership filter. A smaller fingerprint (e.g. via
// Filter::new with a target fp_rate) would significantly reduce size,
// but would make merging slower since mismatched fingerprint sizes
// fall back to one-by-one insertion instead of sorted merge.
let mut amqf =
qfilter::Filter::with_fingerprint_size(initial_capacity as u64, u64::BITS as u8)
.unwrap();
let mut amqf = qfilter::Filter::with_fingerprint_size(
initial_capacity as u64,
USED_KEYS_FINGERPRINT_BITS,
)
.unwrap();
// This drains items from the set. But due to concurrency it might not be empty
// afterwards, but that's fine. It will be part of the next commit.
set.retain(|hash| {
......@@ -2586,6 +2583,12 @@ impl<S: ParallelScheduler, const FAMILIES: usize> TurboPersistence<S, FAMILIES>
}
}
/// The fingerprint size of the used keys AMQFs, in bits. The size sets the false positive rate:
/// about `keys / 2^32`, i.e. 0.06% for 2.5M used keys, for which a few cold keys are written into
/// hot files. This costs about 1.7 bytes per key, compared to about 6 bytes per key with full 64
/// bit hashes.
const USED_KEYS_FINGERPRINT_BITS: u8 = 32;
/// The union of the used keys recorded in the meta files, or `None` if there are none.
fn union_used_key_hashes(meta_files: &[MetaFile]) -> Result<Option<qfilter::Filter>> {
let filters = meta_files
......@@ -2601,8 +2604,9 @@ fn union_used_key_hashes(meta_files: &[MetaFile]) -> Result<Option<qfilter::Filt
_ => {
let total_len = filters.iter().map(|f| f.len()).sum::<u64>();
// The fingerprint size must match the source filters to use qfilter's sorted merge.
let mut merged = qfilter::Filter::with_fingerprint_size(total_len, u64::BITS as u8)
.context("Failed to create the merged used keys AMQF")?;
let mut merged =
qfilter::Filter::with_fingerprint_size(total_len, USED_KEYS_FINGERPRINT_BITS)
.context("Failed to create the merged used keys AMQF")?;
for filter in &filters {
merged
.merge(false, filter)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment