gxhash
A blazingly fast, SIMD-accelerated non-cryptographic hashing algorithm for Rust with zero dependencies.
Repository Health
Technical Analysis
GxHash is a non-cryptographic hashing algorithm for Rust built around heavy use of SIMD intrinsics (AES-NI on x86, ARM AES+NEON on aarch64), high instruction-level parallelism, and a deliberately small bytecode footprint that inlines and caches well. It hashes arbitrary byte streams to 32-, 64-, or 128-bit outputs and ships a Hasher/BuildHasher implementation (GxHasher, GxBuildHasher) with drop-in HashMap/HashSet type aliases, making it usable as a fast general replacement for Rust’s default SipHash-based hasher.
Despite the aggressive unsafe optimizations used to hit its throughput numbers, GxHash uses several rounds of hardware-accelerated AES block cipher for bit mixing and passes the full SMHasher test suite, the standard quality benchmark for non-cryptographic hashers, giving it low collision rates and strong avalanche behavior. It has zero cargo dependencies in its core hashing path, supports no_std environments, and is seeded with randomization by default (or deterministically via a feature flag) to resist hash-flooding DOS attacks against structures like HashMap.
What You Get
- Direct hash functions -
gxhash32,gxhash64, andgxhash128for hashing an arbitrary byte slice with a seed, no setup required. Hashertrait implementation -GxHasherimplementsstd::hash::Hasherso it plugs into any API that accepts a custom hasher.- DOS-resistant
BuildHasher-GxBuildHasherrandomizes its seed by default (backed byRandomState) so repeatedHashMap::default()calls aren’t predictable to an attacker. - Ready-made collection aliases -
gxhash::HashMapandgxhash::HashSettype aliases plusHashMapExt/HashSetExtconvenience traits fornew()/with_capacity(). no_stdsupport - the core hashing functions build without the standard library; theHasher/collection types are gated behind the default-onstdfeature.- Optional hybrid mode - a
hybridfeature flag (nightly-only) enables widerVAES+AVX2registers for higher throughput on large inputs while preserving identical hash output.
Common Use Cases
- Fast in-memory
HashMap/HashSetkeys - swapping Rust’s default SipHash forgxhash::HashMapin hot paths where keys are trusted or DOS resistance from randomized seeding is sufficient. - Checksumming and deduplication - hashing byte buffers (files, network payloads, cache entries) to detect duplicates or changes without cryptographic guarantees.
- Content-addressed caching - using
gxhash128output as a cache key or bucket identifier where speed matters more than cryptographic collision resistance. - High-throughput data pipelines - hashing large volumes of data (e.g. in databases, search indexes, or bloom filters) where hash computation is a measurable bottleneck.
Under The Hood
Architecture
The crate is organized around a tiny public surface (src/lib.rs) that re-exports two modules: gxhash (the core algorithm in src/gxhash/mod.rs, no_std-compatible) and hasher (the std-gated Hasher/BuildHasher/collection-alias layer in src/hasher.rs). The actual SIMD primitives live behind a platform module with parallel x86.rs and arm.rs implementations selected at compile time via cfg(target_arch), each defining the same primitive set (create_empty, create_seed, load_unaligned, aes_encrypt, get_partial_safe/get_partial_unsafe) so the algorithm code in mod.rs is architecture-agnostic. compress_all branches by input length (empty, single-vector, multi-vector with unrolled 8-block processing in compress_many) to keep small inputs cheap while giving large inputs a high-ILP loop; a companion ffi/ crate exposes a C ABI over the same core. A build.rs plus compile_error! guards in the platform modules hard-fail the build on CPUs lacking AES-NI/SSE2 or AES/NEON rather than falling back to a slower path.