Skip to content

Hashbrown in Rust: what it adds over std HashMap and where the speed comes from

hashbrown is a Rust port of Google’s SwissTable hash map. It is worth knowing about, but not for the reason usually given: since Rust 1.36, std::collections::HashMap is hashbrown internally. Swapping one for the other does not change the table algorithm. What changes is the default hasher, and whether you need the standard library at all.

  • A faster default hasher. std’s HashMap uses SipHash-1-3 by default, which resists HashDoS attacks (an attacker choosing keys that all collide). hashbrown defaults to foldhash, which is much faster but, per the hashbrown README, does not give the same level of HashDoS resistance.
  • no_std support. hashbrown works without std, needing only the alloc crate and a global allocator, so you can use it in embedded code or kernels.
  • APIs std doesn’t expose, such as HashTable (a raw table where you supply the hash and equality yourself) and optional rayon and serde integration behind Cargo features.

Add the dependency (0.17.1 is the current release at the time of writing):

[dependencies]
hashbrown = "0.17"

The types are drop-in replacements for the std ones:

use hashbrown::{HashMap, HashSet};
fn main() {
let mut map = HashMap::new();
map.insert("key", "value");
let mut set = HashSet::new();
set.insert("element");
println!("{:?}, {:?}", map, set);
}

The original version of this post timed one million inserts into std’s HashMap and hashbrown’s and printed numbers that were never measured. Because both maps use the same table, the gap you see in a test like that comes almost entirely from SipHash versus foldhash. You can confirm this by adding a third map: std’s HashMap with foldhash as the hasher.

[dependencies]
hashbrown = "0.17"
foldhash = "0.2"
use std::collections::HashMap as StdHashMap;
use std::hint::black_box;
use std::time::{Duration, Instant};
use foldhash::{HashMap as StdFoldHashMap, HashMapExt};
use hashbrown::HashMap as HbHashMap;
const N: u64 = 1_000_000;
fn time(label: &str, mut insert_all: impl FnMut()) {
let start = Instant::now();
insert_all();
let elapsed: Duration = start.elapsed();
println!("{label:<24} {elapsed:?}");
}
fn main() {
time("std (SipHash)", || {
let mut m = StdHashMap::new();
for i in 0..N {
m.insert(i, i);
}
black_box(m);
});
time("std + foldhash", || {
let mut m = StdFoldHashMap::new();
for i in 0..N {
m.insert(i, i);
}
black_box(m);
});
time("hashbrown (foldhash)", || {
let mut m = HbHashMap::new();
for i in 0..N {
m.insert(i, i);
}
black_box(m);
});
}

Run it with cargo run --release; debug builds are not meaningful for this. Expect the two foldhash maps to land close together and the SipHash map to be slower. Exact numbers depend on your CPU, key type and map size, so measure your own workload. For anything you plan to act on, use a benchmarking harness such as criterion, which runs many iterations and reports variance; a single Instant timing is easily skewed by allocation and cache warm-up.

  • You only want faster hashing in a normal std program: keep std::collections::HashMap and plug in a faster hasher (foldhash, or rustc-hash for integer keys). No new map type leaks into your public API.
  • Keys come from untrusted input (HTTP headers, user-supplied JSON keys): keep std’s default SipHash, or choose a hasher with HashDoS resistance.
  • no_std, or you need HashTable or the extra entry APIs: use hashbrown directly.

© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)