Expand description
An implementation of theFowler–Noll–Vo hash function.
§About
The FNV hash function is a customHasher
implementation that is moreefficient for smaller hash keys.
The Rust FAQ states that while the defaultHasher
implementation,SipHash, is good in many cases, it is notably slower than other algorithmswith short keys, such as when you have a map of integers to other values.In cases like these,FNV is demonstrably faster.
Its disadvantages are that it performs badly on larger inputs, andprovides no protection against collision attacks, where a malicious usercan craft specific keys designed to slow a hasher down. Thus, it isimportant to profile your program to ensure that you are using small hashkeys, and be certain that your program could not be exposed to maliciousinputs (including being a networked server).
The Rust compiler itself uses FNV, as it is not worried aboutdenial-of-service attacks, and can assume that its inputs are going to besmall—a perfect use case for FNV.
§Using FNV in aHashMap
TheFnvHashMap
type alias is the easiest way to use the standard library’sHashMap
with FNV.
usefnv::FnvHashMap;letmutmap = FnvHashMap::default();map.insert(1,"one");map.insert(2,"two");map = FnvHashMap::with_capacity_and_hasher(10, Default::default());map.insert(1,"one");map.insert(2,"two");
Note, the standard library’sHashMap::new
andHashMap::with_capacity
are only implemented for theRandomState
hasher, so usingDefault
toget the hasher is the next best option.
§Using FNV in aHashSet
Similarly,FnvHashSet
is a type alias for the standard library’sHashSet
with FNV.
usefnv::FnvHashSet;letmutset = FnvHashSet::default();set.insert(1);set.insert(2);set = FnvHashSet::with_capacity_and_hasher(10, Default::default());set.insert(1);set.insert(2);
Structs§
- FnvHasher
- An implementation of the Fowler–Noll–Vo hash function.
Type Aliases§
- FnvBuild
Hasher - A builder for default FNV hashers.
- FnvHash
Map - A
HashMap
using a default FNV hasher. - FnvHash
Set - A
HashSet
using a default FNV hasher.