fxhash
A fast, non-cryptographic hashing algorithm from Firefox and rustc for high-speed Rust HashMaps and HashSets.
Repository Health
Technical Analysis
fxhash is a small, dependency-light Rust crate implementing the FxHash algorithm extracted from the rustc compiler, the same family of hasher used internally by Firefox. It processes 8 bytes at a time on 64-bit platforms (versus one byte at a time for FNV), delivering substantially faster hashing for the short keys that dominate real workloads.
The crate ships drop-in FxHashMap and FxHashSet type aliases plus FxHasher, FxHasher32, and FxHasher64 implementations of the standard std::hash::Hasher trait, so you can swap it in wherever you already use the standard library’s collections. It is explicitly non-cryptographic and offers no DoS resistance, trading collision-attack safety for raw speed on trusted input.
What You Get
FxHashMapandFxHashSettype aliases that are drop-in replacements for the standard collections with a faster default hasher.- Three hasher implementations —
FxHasher(pointer-width),FxHasher32, andFxHasher64— all implementingstd::hash::Hasher. - Convenience one-shot functions
hash,hash32, andhash64for quickly hashing anyHashvalue. - A tiny, near-zero-dependency crate (only
byteorder) with the exact algorithm used inside rustc and Firefox.
Common Use Cases
- Speeding up internal
HashMap/HashSetlookups on trusted keys in compilers, interpreters, and hot data-processing loops. - Replacing the default SipHash hasher where DoS resistance is not needed and latency matters.
- Computing quick 32- or 64-bit hashes of arbitrary hashable values for indexing, deduplication, or bucketing.
- Serving as the hasher behind caches and symbol tables where keys are short strings or integers.
Under The Hood
Architecture
The entire crate lives in a single lib.rs (~300 lines). The core is a private HashWord trait implemented via macro for usize, u32, and u64, whose hash_word step is rotate_left(5) ^ word * SEED. Free functions write32/write64 consume input in word-sized chunks — 8 bytes at a time on 64-bit, then 4/2/1-byte tail handling — and are wired through three public structs (FxHasher, FxHasher32, FxHasher64) that each implement std::hash::Hasher. FxHashMap/FxHashSet are just HashMap/HashSet aliases parameterized with BuildHasherDefault<FxHasher>.
Tech Stack
Pure Rust with a single runtime dependency, byteorder (1.x), used for native-endian word reads. Dev-dependencies seahash and fnv back the comparison benchmarks in bench.rs. Seed constants (0x51_7c_c1_b7_27_22_0a_95 for 64-bit, 0x9e_37_79_b9 for 32-bit) and pointer-width selection are handled with #[cfg(target_pointer_width = ...)], so the same code compiles optimally on 32- and 64-bit targets.
Code Quality
The code is idiomatic and dense, with #[inline] on every hot function and #![deny(missing_docs)] enforcing documentation on all public items. There is no dedicated unit-test module in the source; correctness rests on the algorithm being lifted directly from rustc plus the benchmark harness. The implementation is small enough to audit at a glance and has been stable since 0.2.1 (2017).
API Design
The public surface is deliberately tiny and ergonomic: swap an import to get FxHashMap/FxHashSet, or reach for hash/hash32/hash64 one-shot helpers. Because everything implements the standard Hasher/BuildHasher traits, it composes with the whole standard-library collections ecosystem with zero boilerplate. The main friction is that it must be created with ::default() rather than ::new(), which the docs call out explicitly.
Used by 2 apps in this directory
Fyrox
Game Development
A production-ready 2D/3D game engine written in Rust with a built-in scene editor, physics, and hot-reloading game scripts
Meilisearch
Search
Lightning-fast hybrid search engine with AI-powered semantic and full-text retrieval for modern applications.