perf(frequency): Back Frequencies with hashbrown 0.17, use entry_ref#26
Merged
Conversation
Swaps the Frequencies backing map from std HashMap (via foldhash::HashMap) to hashbrown::HashMap. hashbrown 0.16+ uses foldhash 0.2 as its default hasher — the same hash qsv-stats already used — so hashing is unchanged; only the table implementation differs. This is a dependency SWAP, not an addition: direct prod deps stay at 4 (foldhash -> hashbrown), and foldhash remains in the tree transitively under hashbrown (no duplication). The byte-slice insert hot path (add_borrowed / increment_by_borrowed) now uses hashbrown's entry_ref, a single borrowed probe that only allocates on the vacant branch. std's HashMap has no stable raw-entry API, so the old get_mut-then-insert path hashed+probed twice for every new key. Benchmarked at 1M rows (benches/freqhb.rs), identical hashing: - build: -7-8% high-cardinality (dominant cost), -14-20% low-cardinality - merge: -15% low-card, ~-3% high-card foldhash kept as a dev-dependency only, to retain the std baseline in the comparison bench. All 161 lib tests pass; clippy clean. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes the crate’s frequency-counting hot path by switching Frequencies from a foldhash-backed std HashMap to hashbrown::HashMap, and by using hashbrown’s borrowed-key entry_ref API to avoid double hashing/probing and unnecessary allocations when counting &[u8] keys.
Changes:
- Replace
foldhash::HashMapusage inFrequencies<T>withhashbrown::HashMap. - Update the byte-slice counting hot path (
add_borrowed/increment_by_borrowed) to useentry_ref. - Add a new Criterion benchmark (
benches/freqhb.rs) and wire it up inCargo.toml; updateCLAUDE.mddocumentation accordingly.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/frequency.rs | Switch backing map to hashbrown::HashMap and use entry_ref for borrowed byte-slice insertion/counting. |
| CLAUDE.md | Update documentation to reflect the new hashbrown backing and entry_ref usage. |
| Cargo.toml | Swap prod dependency foldhash → hashbrown, keep foldhash as a dev-dependency for benchmarking, and add the freqhb bench target. |
| benches/freqhb.rs | New benchmark comparing std+foldhash vs hashbrown for build/merge paths. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Swaps the
Frequenciesbacking map from stdHashMap(viafoldhash::HashMap) tohashbrown::HashMap, and switches the byte-slice insert hot path to hashbrown'sentry_ref.The enabling detail from hashbrown's changelog: hashbrown 0.16+ uses foldhash 0.2 as its default hasher — the exact hash qsv-stats already used. So hashing is unchanged; only the table implementation differs.
Why it's a dependency swap, not an addition
foldhashwas used in exactly one place (frequency.rs, the crate's only HashMap user). After the swap:foldhash→hashbrown(num-traits,rayon,serdeunchanged).foldhash0.2 remains in the tree transitively under hashbrown — a single copy, no duplication.allocator-api2,equivalent(both tiny).The hot-path change
add_borrowed/increment_by_borrowedpreviously didget_mut(&[u8])theninsert(v.to_vec())on a miss. std's HashMap has no stable raw-entry API, so a new key was hashed + probed twice. hashbrown'sentry_refprobes once with the borrowed key and only allocates ([u8]::to_owned()) on the vacant branch. Result is identical (verified by the unchanged test suite + a cardinality assertion in the bench).Benchmarks (1M rows, identical hashing —
benches/freqhb.rs)build (
add_borrowed):merge (
Commute::mergepattern):The high-cardinality build is the dominant wall-clock cost in qsv's
frequencycommand, and it improves ~7–8%.foldhashis kept as a dev-dependency solely to retain thestdbaseline in the comparison bench.Test plan
cargo test --lib— 161 passed, 0 failedcargo clippy --lib— cleancargo bench --bench freqhb🤖 Generated with Claude Code