winit icon indicating copy to clipboard operation
winit copied to clipboard

Using a custom hasher in hash maps

Open notgull opened this issue 2 years ago • 4 comments

At the moment, the Wayland backend uses fnv for its hash maps, while all of the other backends (to my knowledge) just use the std SipHash algorithm. According to @kchibisov in Matrix, this was done because it improved benchmark times in Alacritty. If so, it would be nice to port this optimization to the other backends.

There are other hashing algorithms as well, like ahash, rustc_hash and nohash. We should compare all of these, as well as plain std hash maps, and see which one best fits our use case.

notgull avatar Jun 07 '23 04:06 notgull

fnv is unmaintained, in the meantime the Rust ecosystem has largely moved towards hashbrown, which uses ahash under the hood.

daxpedda avatar Jun 07 '23 08:06 daxpedda

I wouldn't call it unmaintained, it's complete, no changes required for it.

Though, ahash is a better default.

kchibisov avatar Jun 22 '23 03:06 kchibisov

I've compared the some of the hash functions in alacritty and ahash was much better when doing get than fnv, I've also included default std, but it was way slower... The benchmark I've used is available here https://git.sr.ht/~kchibisov/hash-bench.

https://github.com/alacritty/alacritty/pull/7106#issue-1816728904

kchibisov avatar Jul 22 '23 15:07 kchibisov

Found this while looking into Bevy's compile times. ahash takes on a few dependencies, including zerocopy, which is a surprisingly heavy crate to compile for what it does, and optionally serde. rustc-hash was benchmarked to be roughly ~1-4% faster in rustc, and comes without any dependencies. It might be worth looking into using it over ahash.

james7132 avatar Sep 21 '25 07:09 james7132

@james7132 I've filed an MR to move to foldhash, which has no dependencies and is faster than ahash.

notgull avatar Dec 20 '25 22:12 notgull

Sounds good to me! I think this was the only instance of ahash left in Bevy's dependency tree.

james7132 avatar Dec 22 '25 01:12 james7132