Abstract default hasher + switch to foldhash (quality)#129
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.
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).Re-lands the hasher work from #126, which got merged into its stacked base (
evolve-lifecycle-with-state) instead ofmasterand so never reachedmaster. This branches directly offmaster.1. Abstract the default hasher behind an opaque newtype
DefaultHashBuilderwas a public type alias (= ahash::RandomState), leaking the concrete hasher. 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 renamed tocustom-hasher(wired viadep:foldhash), so the crate name no longer appears in the public feature list.2. Switch the underlying hasher: ahash → foldhash (quality)
Moves
type DefaultHasherImplfromahash::RandomStatetofoldhash::quality::RandomState.quality(overfast) is deliberate: the cache reuses one hash for both shard selection and hashbrown's bucket/tag bits, needing 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::BITS, notu64::BITS: on 32-bit a usize-width hasher leaves the top 32 bits zero, sou64::BITS / 2there would send every key to shard 0.Breaking
features = ["ahash"]→["custom-hasher"]; don't depend on the concrete hasher behindDefaultHashBuilder.custom-hasherfor SipHash on adversarial-key workloads.Verification
cargo build/test(58 + doctests) /clippy/doc -D warningsclean;--no-default-featurescompiles.