fxhash

A fast, non-cryptographic hashing algorithm from Firefox and rustc for high-speed Rust HashMaps and HashSets.

Library
Cargo
v0.2.1
290stars
Apache-2.0/MIT

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
33/100Needs Attention
Development Activity0
Maintenance0
Community52
Maturity60
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
82/100Excellent
Architecture80
Code Quality82
Innovation78
Learning Curve90

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

  • FxHashMap and FxHashSet type aliases that are drop-in replacements for the standard collections with a faster default hasher.
  • Three hasher implementations — FxHasher (pointer-width), FxHasher32, and FxHasher64 — all implementing std::hash::Hasher.
  • Convenience one-shot functions hash, hash32, and hash64 for quickly hashing any Hash value.
  • A tiny, near-zero-dependency crate (only byteorder) with the exact algorithm used inside rustc and Firefox.

Common Use Cases

  • Speeding up internal HashMap/HashSet lookups 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.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search