SimpleHash
A simple Rust implementation of common non-cryptographic hash functions that are compatible with Rust's standard collections.
Currently Implemented
- FNV-1 (32-bit and 64-bit)
- FNV-1a (32-bit and 64-bit)
- MurmurHash3 (32-bit and 128-bit)
These hash functions (except for MurmurHash3 128-bit) implement the std::hash::Hasher trait, making them usable with HashMap and HashSet as faster alternatives to the default SipHash.
Usage
Basic Usage
use ;
Using with HashMap and HashSet
You can use these hashers with Rust's standard collections for better performance:
use Fnv1aHasher64;
use MurmurHasher32;
use ;
use ;
// Using FNV-1a with HashMap
let mut map: =
with_hasher;
map.insert;
// Using FNV-1a with HashSet
let mut set: =
with_hasher;
set.insert;
// For MurmurHash3, create a BuildHasher implementation
;
// Using MurmurHash3 with HashMap
let mut murmur_map: =
with_hasher;
murmur_map.insert;
When to Use Alternative Hashers
- For performance-critical code: When dealing with a large number of hash operations or collections with many elements
- For small keys: FNV performs exceptionally well with small keys, such as integers or short strings
- For medium to large inputs: MurmurHash3 offers better performance for larger inputs
- For internal/trusted data only: These hash functions lack the DoS protection of SipHash (Rust's default)
Based on benchmarks, these hashers can provide significant performance improvements:
- FNV-1a is generally 1.5-2x faster than SipHash for small keys
- MurmurHash3 shows better performance for larger keys and provides better collision resistance
Command Line Usage
# Build the project
# Run the CLI
Verification
This project includes verification scripts to ensure the hash implementations match reference implementations:
FNV Verification
# Generate the FNV test corpus using Go
# Verify FNV implementations against Go's reference implementation
MurmurHash3 Verification
# Generate the MurmurHash3 test corpus
# Run the verification tests
Benchmarks
SimpleHash includes benchmarks using the Criterion.rs library. To run the benchmarks:
# Run all benchmarks
# Run only FNV benchmarks
# Run comparative hash benchmarks
# Run HashMap/HashSet performance benchmarks
The benchmarks compare:
- FNV hashing implementations (FNV-1 and FNV-1a, 32-bit and 64-bit variants)
- MurmurHash3 implementations (32-bit and 128-bit)
- Performance across various input sizes
- Different input patterns (zeros, ones, alternating, incremental)
- Realistic data inputs (strings, URLs, JSON, UUIDs)
- HashMap and HashSet performance with different hashers (SipHash vs FNV vs MurmurHash3)
- Collision resistance evaluation with similar keys
License
MIT