rust-farmhash
A pure-Rust port of Google's FarmHash providing fast 32-bit and 64-bit non-cryptographic hash functions.
Repository Health
Technical Analysis
farmhash is a pure-Rust port of Google’s FarmHash 1.1, a family of fast non-cryptographic hash functions that succeeded CityHash and borrowed ideas from Austin Appleby’s MurmurHash. It exposes 32-bit and 64-bit hashing, seeded variants, and version-stable fingerprint functions for hashing arbitrary byte slices.
The crate also ships a FarmHasher type implementing Rust’s standard std::hash::Hasher trait, so it can be dropped directly into HashMap/HashSet via BuildHasherDefault as a faster alternative to the default SipHash implementation, with output verified against known-good FarmHash results in its test suite.
What You Get
- 32-bit and 64-bit hash functions with optional seed(s): hash32, hash64, hash32_with_seed, hash64_with_seed, hash64_with_seeds
- Version-stable fingerprint32 and fingerprint64 functions for portable, upgrade-safe identifiers
- A FarmHasher type implementing std::hash::Hasher for use with HashMap/HashSet via BuildHasherDefault
- Zero runtime dependencies beyond the Rust standard library
- A test suite validating hash outputs against known FarmHash reference values across multiple input-length buckets
Common Use Cases
- Faster HashMap/HashSet key hashing via BuildHasherDefault<FarmHasher> instead of the default SipHash
- Bit-for-bit-compatible hashing with FarmHash implementations in other languages (Go, C++, Java) for shared sharding or consistent-hashing logic
- Stable content fingerprinting for cache keys and deduplication that must remain consistent across farmhash upgrades and platforms
- Benchmarking non-cryptographic hash algorithm speed against alternatives like FNV
Under The Hood
Architecture
The crate is a flat library with a single public entry point, src/lib.rs, which re-exports five hashing functions (hash32, hash32_with_seed, hash64, hash64_with_seed, hash64_with_seeds), two fingerprint functions, and a FarmHasher struct implementing std::hash::Hasher. Internally, lib.rs delegates into private submodules that each mirror a family of the original C++ FarmHash implementation: farmhashna (backs fingerprint64), farmhashmk (backs the 32-bit API), farmhashxo (backs the 64-bit API), plus farmhashuo, farmhashcc_shared, farmhashna_shared, and farmhashmk_shared as shared byte-mixing helpers, all sitting on top of platform.rs, which supplies low-level primitives (rotate, byte-swap, fetch-as-u32/u64) that every hash family calls into. There is no I/O, configuration, or dependency injection — every function is pure, taking a byte slice and optional seed(s) and returning a fixed-width integer. If platform.rs’s primitives changed behavior, every hash family and therefore every public function would silently produce different values, breaking the fingerprint functions’ stability guarantee.
Tech Stack
The crate has zero runtime dependencies; its only dependency is a dev-dependency on fnv 1.0.0, used solely to benchmark FarmHash against FNV in benches/bench_web2dict.rs. Build tooling is stock cargo (build/test/bench) with a benches/ directory using Rust’s built-in nightly #[bench] harness rather than criterion. There is no async runtime, no I/O layer, and no framework involved — it is a leaf computational library meant to be pulled into other crates, distributed via crates.io under the package name farmhash.
Code Quality
Two integration-test files (tests/test_hash32.rs, tests/test_hash64.rs) assert exact hash outputs against tables of known input strings and expected hex values across multiple length buckets, directly validating the ported algorithm against reference outputs — a sound strategy for verifying a byte-for-byte port. Error handling is effectively absent because every function is total (any byte slice produces a valid hash), which is appropriate for this domain. There is no linter configuration or CI workflow visible in the repository, and documentation is limited to a handful of doctest examples on the public functions. Naming mirrors the original C++/Go FarmHash sources (na_hash64, mk_hash32, xo_hash64_with_seeds), which aids cross-referencing upstream but reads as a direct transliteration rather than idiomatic Rust naming.
API Design
The public API is minimal: five free functions for 32/64-bit hashing with optional seeds, two fingerprint functions for version-stable hashing, and a FarmHasher type implementing std::hash::Hasher so it slots into idiomatic Rust patterns like HashMap<K, V, BuildHasherDefault<FarmHasher>> with no extra boilerplate. Public functions include runnable doctest examples showing exact expected output values, which is a useful touch for a hashing crate where determinism is the entire value proposition. There is nothing architecturally novel here — it is a faithful, minimal port of an existing well-known algorithm rather than an original design; its distinguishing value is bit-for-bit FarmHash compatibility rather than a from-scratch hash function.