int_hash
Very fast, very simple hash algorithm designed for use in integer hash maps & sets.
This crate attempts to provide the fastest option for integer key hashmaps in the Rust language. So the algorithm may change if a better method is found for this use case.
use IntHashMap;
let mut map: = default;
map.insert;
When hashing data larger than 64-bits the hasher will fallback to a secondary algorithm suitable for arbitrary data (defaults to FxHasher
).
Benchmark Performance
For more info see the the full benchmark report.
Hash Algorithm | Integer Sample Set | int_hash is |
---|---|---|
Rust default aka SipHash | Random numbers | 1.23-9.61x faster |
Rust default aka SipHash | Natural numbers | 3.82-21.6x faster |
Rust default aka SipHash | 32× table | 1.59-4.94x faster |
fnv |
Random numbers | 0.99-1.58x faster |
fnv |
Natural numbers | 2.11-11.08x faster |
fnv |
32× table | 0.57-1.09x faster |
rustc-hash aka fx |
Random numbers | 0.95-1.29x faster |
rustc-hash aka fx |
Natural numbers | 1.26-2.15x faster |
rustc-hash aka fx |
32× table | 0.94-1.28x faster |
seahash |
Random numbers | 1.16-5.57x faster |
seahash |
Natural numbers | 3.65-19.35x faster |
seahash |
32× table | 1.33-3.11x faster |
twox_hash aka xx |
Random numbers | 1.25-9.55x faster |
twox_hash aka xx |
Natural numbers | 4.05-23.95x faster |
twox_hash aka xx |
32× table | 1.68-5.67xx faster |
Note: For > 64-bit keys int_hash will perform inline with rustc_hash
.
Limitations
The algorithm is non-cryptographic.
Why is it so fast
int_hash
is dedicated at solving integer-sized hashing. Producing a unique u64
from an integer is not a very difficult problem, though getting a good spread of values to minimise hashmap collisions is a little harder.
The current implementation uses simple u64
XOR mixing to spread values. The sheer simplicity of this approach makes the hashing operation very fast and the primitive spreading is good enough to produce best-in-class hashmap performance.