turbo-persistence: space-amplification driven compaction with adaptive hash shards (#99268)
## Summary Turbo-tasks GC can only shrink the persistent cache if compaction merges each tombstone with the data it deletes. Canary's compaction algorithm leaves many tombstones behind so with GC enabled the cache is only 5% smaller than without it. This PR replaces compaction with a space-amplification policy on hash shards, modeled on RocksDB universal compaction and Cassandra UCS. With GC enabled, the cache stays ~flat. ### Cache size over 31 consecutive vercel-site builds Same canary base and the same commits in every arm. The size is measured after each build, including its compaction on shutdown. | build | canary, GC off | canary, GC on | this PR, GC off | this PR, GC on | |---|---|---|---|---| | 1 | 2.59 GB | 2.60 GB | 2.63 GB | 2.64 GB | | 10 | 3.15 GB | 3.12 GB | 3.24 GB | 3.07 GB | | 20 | 3.42 GB | 2.92 GB | 3.20 GB | 3.06 GB | | 22 | 4.57 GB | 3.87 GB | 3.55 GB | 3.18 GB | | 31 | **4.19 GB** | **3.96 GB** | **3.75 GB** | **3.00 GB** | | growth, build 1 → 31 | +1.60 GB | +1.36 GB | +1.12 GB | **+0.36 GB** | - **GC saves 5% with canary's compaction and 20% with this one** (final size, GC on vs. off). A full compaction of the final cache puts the live data at about 2.8 GB, so this PR with GC is within about 8% of it. - **Build 22 rewrites a lot of task data.** Canary grows by about 1 GB at that build in both modes and never recovers the space. This PR absorbs it and merges the superseded versions away within a few builds. ### Why canary's compaction can't keep up with GC - **Old versions and tombstones don't reliably meet the data they supersede.** Canary wrote recently read keys and the rest into separate warm and cold files and never merged across them. - **Its merge triggers couldn't see the duplication.** There were duplication-byte thresholds and `max_merge_bytes`. Most of the garbage is superseded versions of a relatively small set of large, frequently rewritten `TaskData` values, so those triggers missed it and produced inefficient merge groups. ### New Compaction policy - **Shards:** each family's key space is split into power-of-two hash-prefix shards. The count starts at `FamilyConfig::initial_shard_bits` (TaskData 8 shards, TaskMeta 4, TaskCache and Infra 1) and then follows the family's size, targeting about 256 MB per shard. - It doubles once shards exceed 1.5× the target and halves below 0.5×, so a size near a boundary doesn't flip it. - Commits and merges split SST files at shard boundaries, so each shard is compacted on its own. Files written with an old count are re-split by their shard's next merge, not by a rewrite. - Write batches still buffer a family in at most 4 collectors (64 MB each) as on canary, split by a prefix of the shard bits, so their memory doesn't grow with the shard count. Families with more than 4 shards write proportionally smaller commit files. - **Bottom merge:** when the files above a shard's bottom run exceed 50% of it, the whole shard is merged into a new bottom run. Tombstones count as the entries they delete. This is the merge that reliably drops superseded entries and tombstones. - This is analogous to a 'full compaction' but it targets a single shard and is targeting files covering typically <0.5G of data. - **Intermediate merge:** more than 4 files above the bottom run are merged together, which bounds the number of files a lookup consults. - **Budget:** bottom merges per family stop after rewriting 3× the family's fresh (not yet compacted) bytes, so cost follows new writes and shards spread over multiple compactions. 3× is how often a written byte is copied in steady state: a bottom merge at the 50% trigger rewrites 1.5× the bytes above the bottom run for every 0.5× of them. - **Hard cap:** a shard over twice the trigger (100% space amplification), or with more than twice `max_files_above_bottom` files above its bottom run, is bottom-merged even when the budget is spent. This bounds how far a shard can fall behind, e.g. after a GC purge: its tombstones count as the entries they delete, but they're small and give little budget. - **Tiny families:** a 1 MB floor keeps families like Infra, which rewrite ~everything on every commit, out of bottom merges. - **Ranking:** jobs are ranked across families by how far over their trigger they are, so intermediate merges aren't starved. **Hot/cold split, only in bottom merges.** A bottom merge writes recently read keys and the rest into separate hot and cold files. It always includes every file of its shard, hot and cold, so this does not interfere with deduplication. Used-key tracking is unchanged from canary: each commit records the keys it read in its meta file, and compaction uses the union of all live meta files. The marks are never copied into meta files written by compaction, so they expire with the commit that recorded them. - This Hot/cold split is less aggressive than canary. See below for Read locality impacts. **Reads:** a lookup only consults the SST files of its key's shard. A per-family index keeps each shard's hash ranges densely, newest first, so lookups scan them sequentially. ## Verification - The cache size benchmark above: vercel-site, 31 commits, GC with `rootTtlMs=0`. Earlier runs on the same base reproduce the GC-on results (canary 3.93–3.99 GB, this PR 3.00–3.05 GB). - **Read locality**, measured with temporary instrumentation that isn't part of this PR. It counts the distinct SST blocks each build reads, and the distinct 128 KiB file regions they touch as an estimate of squashfs decompression. Same base, same builds, builds 2–31, GC on: | | canary | no split | this PR (bottom split) | |---|---|---|---| | distinct blocks read per build, median | 1.35 GB | 1.80 GB | 1.55 GB | | 128 KiB regions touched per build, median / total | 2.42 / 64.2 GB | — | 2.63 / 68.8 GB | The bottom split recovers most of the locality that dropping the split cost. Light builds still read about a third more than on canary, because a shard's hot set only refreshes at its bottom merge. Splitting intermediate merges as well gained about 1%, so it isn't included. - **Lookup microbenchmark**, 16 shards × 20 uncompacted commits (320 SSTs), alternating before and after: `get` hit −14%, miss −28%, `batch_get` −5%. - `cargo test -p turbo-persistence` and `cargo test -p turbo-tasks-backend --tests` pass. The selector has unit tests, and a simulation whose sweep supports the 50% threshold. <!-- NEXT_JS_LLM -->
Showing
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Please
register
or
sign in
to comment