Skip to content

perf(frequency): Back Frequencies with hashbrown 0.17, use entry_ref#26

Merged
jqnatividad merged 1 commit into
masterfrom
perf/frequency-hashbrown
Jun 4, 2026
Merged

perf(frequency): Back Frequencies with hashbrown 0.17, use entry_ref#26
jqnatividad merged 1 commit into
masterfrom
perf/frequency-hashbrown

Conversation

@jqnatividad

Copy link
Copy Markdown
Collaborator

Summary

Swaps the Frequencies backing map from std HashMap (via foldhash::HashMap) to hashbrown::HashMap, and switches the byte-slice insert hot path to hashbrown's entry_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

foldhash was used in exactly one place (frequency.rs, the crate's only HashMap user). After the swap:

  • Direct production deps stay at 4: foldhashhashbrown (num-traits, rayon, serde unchanged).
  • foldhash 0.2 remains in the tree transitively under hashbrown — a single copy, no duplication.
  • Net-new transitive crates: allocator-api2, equivalent (both tiny).

The hot-path change

add_borrowed / increment_by_borrowed previously did get_mut(&[u8]) then insert(v.to_vec()) on a miss. std's HashMap has no stable raw-entry API, so a new key was hashed + probed twice. hashbrown's entry_ref probes 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):

shape std hashbrown delta
low_card_50 3.82 ms 3.05 ms −20%
low_card_5000 6.12 ms 5.28 ms −14%
high_card_short 54.3 ms 50.0 ms −8%
high_card_long 93.3 ms 87.0 ms −7%

merge (Commute::merge pattern):

shape std hashbrown delta
low_card_5000 1.18 ms 1.00 ms −15%
high_card_short 40.2 ms 38.8 ms −3.5% (noisy)

The high-cardinality build is the dominant wall-clock cost in qsv's frequency command, and it improves ~7–8%.

foldhash is kept as a dev-dependency solely to retain the std baseline in the comparison bench.

Test plan

  • cargo test --lib — 161 passed, 0 failed
  • cargo clippy --lib — clean
  • cargo bench --bench freqhb

🤖 Generated with Claude Code

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>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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::HashMap usage in Frequencies<T> with hashbrown::HashMap.
  • Update the byte-slice counting hot path (add_borrowed / increment_by_borrowed) to use entry_ref.
  • Add a new Criterion benchmark (benches/freqhb.rs) and wire it up in Cargo.toml; update CLAUDE.md documentation 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 foldhashhashbrown, 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.

Comment thread benches/freqhb.rs
Comment thread benches/freqhb.rs
@jqnatividad
jqnatividad merged commit c04cdf2 into master Jun 4, 2026
1 check passed
@jqnatividad
jqnatividad deleted the perf/frequency-hashbrown branch June 4, 2026 10:48
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.

2 participants