# Help wanted: fast hash maps in std

**URL:** <https://internals.rust-lang.org/t/help-wanted-fast-hash-maps-in-std/4066>\
**Category:** Uncategorized\
**Created:** [September 13, 2016, 7:36pm UTC](https://internals.rust-lang.org/t/help-wanted-fast-hash-maps-in-std/4066 "2016-09-13T19:36:22Z")\
**Posts on this page:** 10\
**Page:** 2

<div class="post-metadata">

**Author:** ![sfackler](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/sfackler/32/9_2.png) [@sfackler](https://internals.rust-lang.org/u/sfackler)\
**Post date:** [September 15, 2016, 4:29am UTC](https://internals.rust-lang.org/t/help-wanted-fast-hash-maps-in-std/4066/22 "2016-09-15T04:29:32Z")

</div>

How is the implementation going to find the lengths of all variable length sub components?

---

<div class="post-metadata">

**Author:** ![comex](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/comex/32/2587_2.png) [@comex](https://internals.rust-lang.org/u/comex)\
**Post date:** [September 15, 2016, 5:13am UTC](https://internals.rust-lang.org/t/help-wanted-fast-hash-maps-in-std/4066/23 "2016-09-15T05:13:09Z")

</div>

See also:

[https://github.com/contain-rs/hashmap2/pull/5/files](https://github.com/contain-rs/hashmap2/pull/5/files)

a fairly old (March) PR to a fork of the current HashMap implementation, which implements this already. I’m not sure what the current state is.

I really should try hacking on HashMap myself as there are a bunch of other potential optimizations I’d like to look at.

---

<div class="post-metadata">

**Author:** ![Ichoran](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ichoran/32/1780_2.png) [@Ichoran](https://internals.rust-lang.org/u/Ichoran)\
**Post date:** [September 17, 2016, 3:49am UTC](https://internals.rust-lang.org/t/help-wanted-fast-hash-maps-in-std/4066/24 "2016-09-17T03:49:45Z")

</div>

For some reason, MurmurHash3 hasn’t been extensively investigated. It performs vastly better than xxHash for short keys because it has much less state to populate, but has top-quality (non-cryptographic) properties. For example, a 1-byte array can give a 32 bit hash in ~4 ns in Scala, which handily beats everything on [https://cdn.rawgit.com/Gankro/hash-rs/7b9cf787a830c1e52dcaf6ec37d2985c8a30bce1/index.html](https://cdn.rawgit.com/Gankro/hash-rs/7b9cf787a830c1e52dcaf6ec37d2985c8a30bce1/index.html) save FNV. A somewhat-carefully optimized Rust implementation could probably outdo that substantially.

I strongly support the idea of having context-dependent hashing (switch to a slower algorithm if many collisions are detected), but I am not sure that FNV is a good choice for a default for anything. You don’t have to spend very much more time to get at least somewhat better hashing even at very short key sizes.

---

<div class="post-metadata">

**Author:** ![arthurprs](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/arthurprs/32/233_2.png) [@arthurprs](https://internals.rust-lang.org/u/arthurprs)\
**Post date:** [September 24, 2016, 9:46am UTC](https://internals.rust-lang.org/t/help-wanted-fast-hash-maps-in-std/4066/25 "2016-09-24T09:46:29Z")

</div>

I’m improving the cache awareness in this PR [https://github.com/rust-lang/rust/pull/36692](https://github.com/rust-lang/rust/pull/36692) This will add up to any hashing improvements.

---

<div class="post-metadata">

**Author:** ![Veedrac](https://avatars.discourse-cdn.com/v4/letter/v/d78d45/32.png) [@Veedrac](https://internals.rust-lang.org/u/Veedrac)\
**Post date:** [October 3, 2016, 4:46am UTC](https://internals.rust-lang.org/t/help-wanted-fast-hash-maps-in-std/4066/26 "2016-10-03T04:46:22Z")

</div>

How much does the extra `hash_u8(0xff)` cost? If it’s large, it’s worth noting we can avoid this for `String` where `s.capacity() > s.len()` by writing the `0xff` to the underlying buffer.

---

<div class="post-metadata">

**Author:** ![arthurprs](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/arthurprs/32/233_2.png) [@arthurprs](https://internals.rust-lang.org/u/arthurprs)\
**Post date:** [October 12, 2016, 7:52pm UTC](https://internals.rust-lang.org/t/help-wanted-fast-hash-maps-in-std/4066/27 "2016-10-12T19:52:42Z")

</div>

> [@Veedrac](#):
>
> How much does the extra hash\_u8(0xff) cost? If it's large, it's worth noting we can avoid this for String where s.capacity() \> s.len() by writing the 0xff to the underlying buffer.

A lot, I did benchmark something similar here [Extend the `Hasher` trait with `fn delimit` to support one-shot hashing by pczarn · Pull Request #1666 · rust-lang/rfcs · GitHub](https://github.com/rust-lang/rfcs/pull/1666#issuecomment-230730783)

We should get rid of it altogether (there's already open issues on the subject).

---

<div class="post-metadata">

**Author:** ![nnethercote](https://avatars.discourse-cdn.com/v4/letter/n/dfb087/32.png) [@nnethercote](https://internals.rust-lang.org/u/nnethercote)\
**Post date:** [October 27, 2016, 12:41am UTC](https://internals.rust-lang.org/t/help-wanted-fast-hash-maps-in-std/4066/28 "2016-10-27T00:41:34Z")

</div>

FWIW, even FnvHasher is on the slow side. Look at [https://github.com/rust-lang/rust/pull/37229](https://github.com/rust-lang/rust/pull/37229) where I replaced FnvHasher with a worse-but-faster hash function and sped up rustc itself by 2–6% across most workloads…

---

<div class="post-metadata">

**Author:** ![arthurprs](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/arthurprs/32/233_2.png) [@arthurprs](https://internals.rust-lang.org/u/arthurprs)\
**Post date:** [October 27, 2016, 7:44am UTC](https://internals.rust-lang.org/t/help-wanted-fast-hash-maps-in-std/4066/29 "2016-10-27T07:44:53Z")

</div>

> [@nnethercote](#):
>
> FWIW, even FnvHasher is on the slow side.

I think it's important to contextualize a bit here. That workload is mostly 98%+ usize and u64 writes, and for those the PR hasher is a _single instruction_ instead of 16 non-parallelizable ones (fnv). Even if the hash-fn is a bit bad and has bad distribution at the lower bits, solving partial-collisions is not that expensive in rustc hashmap implementation, thus the speedup.

---

<div class="post-metadata">

**Author:** ![bluss](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bluss/32/2264_2.png) [@bluss](https://internals.rust-lang.org/u/bluss)\
**Post date:** [October 27, 2016, 12:43pm UTC](https://internals.rust-lang.org/t/help-wanted-fast-hash-maps-in-std/4066/30 "2016-10-27T12:43:00Z")

</div>

You can avoid the string terminator hashing on a case by case basis.

Comparing inserting 10,000 elements (no reallocation) on HashMap with the default hasher (SipHash13)

Some benches with very short strings (stringified integers 0…10,000)

```rust
test insert_hashmap_string_10_000 ... bench: 1,248,610 ns/iter (+/- 19,435)
test insert_hashmap_string_oneshot_10_000 ... bench: 1,211,014 ns/iter (+/- 18,908)
test lookup_hashmap_10_000_exist_string ... bench: 199,742 ns/iter (+/- 1,870)
test lookup_hashmap_10_000_exist_string_oneshot ... bench: 177,365 ns/iter (+/- 2,821)

```

“one shot” hashing adaptor for strings [code in a gist](https://play.rust-lang.org/?gist=e2c15dac9a907369f3d7559e9eb9fda9&version=stable&backtrace=1)

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:26am UTC](https://internals.rust-lang.org/t/help-wanted-fast-hash-maps-in-std/4066/31 "2019-03-25T08:26:57Z")

</div>

Closed on March 25, 2019, 8:26am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

[Previous page](https://internals.rust-lang.org/t/help-wanted-fast-hash-maps-in-std/4066.md?page=1)
