Skip to content

tests: type-erase hash_batch consumer under cfg(test) (-76% test compile time, -79% peak RSS) - #322

Merged
devonjonte merged 1 commit into
nextfrom
test/dedup-hash-batch-monomorph
Dec 2, 2026
Merged

devonjonte merged 1 commit into
nextfrom
test/dedup-hash-batch-monomorph

Conversation

@devonjonte

Copy link
Copy Markdown
Collaborator

Follow-up root-cause fix for the hosted macos-latest cargo nextest run --release compile blowup seen on next (5b4c6d08, cancelled at 40 min) and #321 (46856012371, took 55m 49s).

Root cause

hash_batch() was generic over consume: impl FnMut(&[u8]). Production calls it from one site (use_digest), producing 210 timed_cell symbols (0.41 MB, 1.24 GiB peak RSS). Thirteen unit tests each passed a distinct closure type into hash_batch(), monomorphizing all Algorithm::ALL × UseCase::ALL timed_cell instances 14 times over (2,455 timed_cell symbols, 5.41 MB = 52.9% of .text, 54.6 MB test binary). Under [profile.release] (lto = "fat", codegen-units = 1), peak rustc RSS hit 7.33 GiB—exceeding GitHub-hosted macos-latest RAM and causing severe swap thrashing.

Fix

Keep hash_batch() unchanged under #[cfg(not(test))] (zero change to production benchmark binaries or timed_cell inlining of use_digest), and type-erase consume to &mut dyn FnMut(&[u8]) inside #[cfg(test)] fn hash_batch() so all current and future test callers share one timed_cell monomorphization.

Measured impact (CARGO_PROFILE_RELEASE_DEBUG=line-tables-only cargo test --release --no-run --locked)

Metric Before After Change
Release test build time (x86-64 i7) 198.9 s 46.9 s -76.4% faster
Peak rustc RSS 7.33 GiB 1.51 GiB -79.4%
timed_cell symbols in test binary 2,455 (5.41 MB) 210 (0.42 MB) -91.4%
Test binary size 54.6 MB 27.1 MB -50.5%
cargo nextest run --release --workspace --locked 246/246 246/246 (5.8s) pass

Next move: devonjonte (watch CI including macos-latest, then merge to next).

…ed_cell bloat

Every test that calls hash_batch() passed its own unique closure type,
causing rustc to monomorphize all Algorithm::ALL x UseCase::ALL
timed_cell<A, U, Closure> instances once per test closure (14 distinct
consumers = 2,455 timed_cell symbols, 5.41 MB of .text, 54.6 MB test
binary). Under fat LTO + codegen-units=1 + line-tables-only, that drove
peak rustc RSS to 7.33 GiB—exceeding the 7 GiB RAM of GitHub-hosted
macos-latest runners and pushing cargo nextest run --release into heavy
swap thrashing (17-55 min per run).

Keep production hash_batch() completely unchanged under cfg(not(test))
so benchmark binaries keep direct monomorphized use_digest calls, and
erase the consumer to &mut dyn FnMut(&[u8]) under cfg(test) inside
hash_batch() itself so every current and future test shares one set of
210 timed_cell symbols:

- Local x86-64 release test build: 198.9s -> 46.9s (-76.4% faster)
- Peak rustc RSS: 7.33 GiB -> 1.51 GiB (-79.4%)
- timed_cell symbols in test binary: 2,455 (5.41 MB) -> 210 (0.42 MB)
- Test binary size: 54.6 MB -> 27.1 MB
- cargo nextest run --release --workspace --locked: 246/246 passed
@devonjonte devonjonte self-assigned this Dec 2, 2026
@devonjonte
devonjonte merged commit 3b36c22 into next Dec 2, 2026
18 checks passed
@devonjonte
devonjonte deleted the test/dedup-hash-batch-monomorph branch December 2, 2026 11:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant