Switch default hasher to foldhash::quality#126
Merged
Conversation
BREAKING (0.7.0). `DefaultHashBuilder` was a public type alias for `ahash::RandomState`, leaking the concrete hasher; the `ahash` feature leaked it too. Both are now implementation-agnostic: - `DefaultHashBuilder` is an opaque newtype wrapping a private, cfg-selected inner hasher. The algorithm can change with no public-API churn. - The `ahash` feature is renamed to `custom-hasher` and pulls the hasher crate via `dep:ahash`, so the crate name no longer appears as a feature. Migration: replace feature `ahash` with `custom-hasher`; don't name the concrete hasher behind `DefaultHashBuilder`.
Exercises the DefaultHashBuilder abstraction: swapping the inner hasher touches only the private `DefaultHasherImpl` alias and the optional dependency — the public `DefaultHashBuilder` type and the `custom-hasher` feature are unchanged. foldhash's `quality` variant gives stronger distribution than its `fast` variant while staying far faster than the std SipHash fallback.
a223f7f to
8217dc2
Compare
2c74856 to
dbfcea7
Compare
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
Makes the default hasher an implementation detail and switches it to foldhash::quality.
Stacked on #124 (base
evolve-lifecycle-with-state) — review/merge #124 first.1. Abstract the default hasher behind an opaque newtype
DefaultHashBuilderwas a public type alias (= ahash::RandomState), which leaked the concrete hasher — naming it or calling ahash-specific methods made the choice part of the public API. It's now an opaque newtype wrapping a private, cfg-selected inner hasher, so the algorithm can change with no public-API churn. Theahashfeature is likewise renamed tocustom-hasherand wired viadep:foldhash, so the crate name no longer appears in the public feature list either.2. Switch the underlying hasher: ahash → foldhash (quality)
With the type/feature opaque, the inner hasher is just
type DefaultHasherImpl = …. This moves it fromahash::RandomStatetofoldhash::quality::RandomState. Thequalityvariant (overfast) is deliberate: the cache reuses one hash for both shard selection and hashbrown's bucket/tag bits, slicing it into independent bit-windows that need good avalanche (see thecompute_shard_indexcomment).RandomStatekeeps per-instance random seeding.3. Doc note on
compute_shard_indexDocuments why the shard-bit rotation is tied to
usize::BITSrather thanu64::BITS: on 32-bit targets a usize-width hasher (e.g. FxHash) leaves the top 32 bits zero, so the low word is where the entropy lives —u64::BITS / 2there would send every key to shard 0 (hashbrown guards against the same thing inh1/Tag::full).Breaking
features = ["ahash"]→["custom-hasher"].DefaultHashBuilder.custom-hasherto fall back to the std SipHash hasher for adversarial-key workloads.Verification
cargo build/cargo test(56 + doctests) /cargo clippy/cargo doc -D warningsall clean;cargo check --no-default-features(std fallback) compiles.