turbo-bitset: make block storage pluggable (#100779)
### What? `turbo_bitset::Bitset` can now use different storage for its blocks. A new public `BlockStorage` trait defines what that storage must support. - `Bitset` is now `Bitset<S: BlockStorage = Vec<usize>>`, so every existing user keeps `Vec<usize>` storage. - The trait is implemented for `Vec<usize>`, `SmallVec<[usize; N]>` and `ArrayVec<usize, N>`. `turbo-bitset` now depends on `smallvec` and `arrayvec` unconditionally. - All `Bitset` methods and operators, plus `Encode`, `Decode` and `ShrinkToFit`, work with any storage. Set operations require both sides to use the same storage. The `turbo-tasks` integrations remain on the default `Bitset`. - The encoding doesn't depend on the storage, so bytes written with one storage decode into any other. - With `ArrayVec`, operations that would need more than its capacity of `N` blocks panic. Decoding a set that doesn't fit returns a `DecodeError`. - `shrink_to_fit` only touches the storage when that frees capacity. An `ArrayVec` or an inline `SmallVec` is left unchanged. ### Why? This lets small sets use inline storage where that helps, without making every `Bitset` user change its storage type. ### How? - **Bounded storage:** when an operation would exceed an `ArrayVec`'s capacity, empty blocks at either end of the storage are dropped first. This fixes a case where an insert at the back panicked although the bits fit, because earlier growth at the front had filled the capacity with zero blocks. - **`intersection`:** builds its result with `FromIterator`, so a `Vec` keeps its exact-size allocation. - **Inlining:** only the small methods of the non-generic `Vec` storage are marked `#[inline]`. The generic `SmallVec` and `ArrayVec` implementations and `Iter::next` have no annotation; removing those had no effect on the benchmark binary (see below). - **Tests:** the shared semantic tests run for all three storages. New tests cover `ArrayVec` overflow and decode errors, `SmallVec` inline/spill behaviour, `shrink_to_fit`, zero-capacity storage, and encoding equality across storages. The benchmarks gain cases for `SmallVec` and `ArrayVec`. ### Verification - `cargo fmt --check -p turbo-bitset -p turbo-tasks -p turbopack-core` - `cargo clippy -q -p turbo-bitset -p turbo-tasks -p turbopack-core -p next-api --all-targets -- -D warnings` - `RUSTDOCFLAGS="-D warnings" cargo doc -q --no-deps -p turbo-bitset -p turbopack-core` - `cargo check -q -p next-api` - `cargo test -q -p turbo-bitset` - `cargo test -q -p turbo-tasks --lib` - `cargo test -q -p turbopack-core --lib` - `cargo test -q -p turbopack-tests --test snapshot` (no snapshot changes) - `cargo bench -q -p turbo-bitset --bench mod -- --test 'bitset/storage_'` (216 cases) - `cargo run -q --release -p turbo-bitset --example sizes` gives output identical to before this change. - Not run: a 32-bit cross-check, because the `i686-unknown-linux-gnu` standard library is not installed locally. The test and benchmark changes avoid assuming a 64-bit `usize`. ### Performance caveats I compared release benchmark binaries for the previous `Vec`-only implementation and this change. Each side was built in its own target directory, I checked that the binary hashes differ, and the runs alternated old/new/new/old. - Before the final storage changes, the full comparison (336 cases) averaged **0.992×** of the old timings. - Those final changes switched `intersection` to `collect` and added `#[inline]`. After them, a focused rerun of 84 previously slower cases averaged **1.040×**: | Group | New / old (geomean) | | --- | ---: | | `intersection` | 0.907 | | `union` | 0.974 | | `contains` | 1.049 | | `insert` | 1.057 | | `iter` | 1.112 | | `from_iter` | 1.165 | - **`iter` and `contains`:** some inputs remain slower even though their normalized hot-loop instructions are the same in both binaries. The cause is unresolved. - **Empty `from_iter`:** about 3 ns becomes about 5 ns because of an extra out-of-line call. ### `#[inline]` annotations At `17be1d5e`, which annotated `Iter::next` and every `BlockStorage` method, I compared builds with and without all 43 of those annotations: full `mod` bench, 768 cases, runs alternating with/without/without/with, separate target dirs, release profile (thin LTO, one codegen unit). - Without any of them, the timings were **1.091×** overall (1.086× and 1.095× in the two pairs). The same binary run twice drifted 1.024× and 1.033×, but per group the same-binary drift reached about 30%, so this is a shared-VM microbenchmark trend, not a proven whole-build effect. - Removing only the annotation on `Iter::next`, or only the 28 on the generic `SmallVec`/`ArrayVec` implementations, gives a bench binary **byte-identical** to the annotated one. - Removing the 14 on the `Vec` implementation reproduces the whole difference. Without them, `Vec`'s `with_capacity` and `resize` wrappers remain separate functions in the binary, even with thin LTO. - So only the `Vec` annotations are kept. The bench binary of this PR's tip is byte-identical to the one built at `17be1d5e`. <!-- NEXT_JS_LLM --> <!-- fleet b56269ed-2145-4f30-b0fe-dc9762520521 --> --------- Co-authored-by:vercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com> Co-authored-by:
Vercel Fleet <fleet@vercel.com> Co-authored-by:
Tobias Koppers <1365881+sokra@users.noreply.github.com> Co-authored-by:
Luke Sandberg <210140+lukesandberg@users.noreply.github.com>
Showing
This diff is collapsed.
This diff is collapsed.
Please
register
or
sign in
to comment