foldhash
A fast, non-cryptographic, DoS-resistant hashing algorithm for Rust data structures.
Repository Health
Technical Analysis
foldhash is a fast, non-cryptographic hashing algorithm implemented in Rust, designed for computational uses such as hash maps, bloom filters, and count/HyperLogLog sketching. It is minimally DoS-resistant by default via randomized internal state, while being dramatically faster than SipHash (Rust’s standard default) and competitive with or faster than aHash and fxhash.
The crate ships two variants: foldhash::fast, optimized for raw throughput in structures like hash maps, and foldhash::quality, optimized for statistical distribution in algorithms like HyperLogLog and MinHash. It works in #![no_std] environments by disabling the default std feature.
What You Get
- A fast non-cryptographic hasher usable as a
BuildHasherforHashMap/HashSet - Two variants:
foldhash::fastfor throughput andfoldhash::qualityfor statistical quality - Minimal HashDoS resistance through randomized per-instance internal state
#![no_std]support by disabling the defaultstdfeature- Performance competitive with or exceeding aHash and fxhash, far ahead of SipHash
Common Use Cases
- Speeding up
HashMap/HashSetlookups by swapping the default hasher - Hashing keys in bloom filters and count sketches
- Providing well-distributed hashes for HyperLogLog and MinHash cardinality estimation
- Hashing in embedded or no_std contexts where SipHash is too slow
Under The Hood
Architecture - foldhash centers on a folded-multiply mixing scheme: the core combines input words with multiplications and folds the 128-bit product back to 64 bits, seeded by randomized state to resist trivial collision attacks. The crate splits into fast and quality modules, each exposing a FoldHasher plus RandomState/FixedState builders that implement Rust’s BuildHasher trait. Tech Stack - Pure Rust with no runtime dependencies, gated by a std default feature so it degrades cleanly to no_std; it relies only on core integer arithmetic, making it portable across targets. Code Quality - The repository documents its design and threat model thoroughly in the README (including explicit non-goals around cryptography and cross-version stability) and benchmarks against aHash, fxhash, and SipHash; its enormous download count (400M+) reflects wide adoption, notably as hashbrown/std HashMap’s fast hasher. API Design - Usage is idiomatic: HashMap::with_hasher(foldhash::fast::RandomState::default()), mirroring the standard BuildHasher pattern, so adoption requires no new mental model. Clear guidance on when not to use it (persistence, security) keeps misuse low.