The new hashing algorithms in foldhash
,
used by hashbrown
as of
version 0.15,
and rustc-hash
were both designed
and written by Orson Peters using folded multiplies.
The new hashing algorithm in rustc-hash addresses issues with the original
FxHasher
being slow for longer strings and not finalizing, leading to
collisions for unchanging least-significant bits. It is both faster and has less
collisions. foldhash generalizes it to be appropriate for non-compiler programs.
Since they are so similar, I do a more thorough comparison.
This analysis was conducted against foldhash commit c55c471 (2024-10-04) and rustc-hash commit 6745258 (2024-10-18). foldhash was adopted by hashbrown on 2024-10-01 and the new algorithm for rustc-hash was merged on 2024-06-02.
&[u8]
self.hash = (self.hash + value) * 0xf1357aea2e62a9c5
(wrapping), i.e., a multiplicative congruential psueodrandom number generator.This suggests that hashing &[u8]
is more efficient than looping hashing
scalars, because it batches in chunks. For example, in a compiler that hashes a
lot of &[Id]
, where Id
is a newtype of u32
, transmuting it to a &[u8]
would be faster.
rustc-hash is optimized for shorter strings.