axhash-map
High-performance HashMap and HashSet collections for Rust.
Powered by hashbrown (SwissTable layout) · Fueled by axhash (AES-NI accelerated hashing)
Why axhash-map?
std::collections::HashMap uses SipHash-1-3 by default — a secure but comparatively slow hash function. For most workloads you don't need cryptographic resistance; you need throughput.
axhash-map swaps the hasher for axhash, which exploits hardware AES instructions (AES-NI on x86-64, AES on ARMv8) to produce hashes at near-memory-bandwidth speed. The underlying table is hashbrown (SwissTable), the same implementation that backs std::collections::HashMap in Rust's standard library — so the table operations are identical; only the hashing step is faster.
Ecosystem
| Crate | Description |
|---|---|
axhash |
High-performance hashing engine |
axhash-map |
Fast HashMap/HashSet powered by hashbrown |
axhash-indexmap |
Ordered maps with AxHash |
axhash-dashmap |
Concurrent DashMap powered by AxHash |
┌──────────────────────────────────────────────────────────┐
│ axhash-map │
│ │
│ Type aliases (Serde-compatible) │
│ HashMap<K, V> HashSet<T> │
│ │
│ Branded newtypes (ergonomic constructors) │
│ AxHashMap<K, V> AxHashSet<T> │
│ │ │ │
│ hashbrown::HashMap hashbrown::HashSet │
│ (SwissTable layout) │
│ │ │ │
│ BuildHasherDefault<AxHasher> │
│ (AES-NI accelerated hash engine) │
└──────────────────────────────────────────────────────────┘
Two usage modes
This crate provides two ways to use the same fast hasher. Pick the one that fits your situation:
Mode 1 — Type alias (HashMap / HashSet)
Plain type aliases over hashbrown with BuildHasherDefault<AxHasher> baked in.
Because there is no wrapper struct, Serde and other #[derive]-based crates work out of the box.
use HashMap; // or HashSet
// Works with serde::Serialize / Deserialize without any extra config.
let mut map: = default;
map.insert;
Mode 2 — Branded newtype (AxHashMap / AxHashSet)
A thin newtype wrapper that adds the familiar ::new() / ::with_capacity() constructors.
Every hashbrown method is accessible transparently via Deref.
use AxHashMap;
let mut map: = new;
map.insert;
| Need | Use |
|---|---|
::new() / ::with_capacity() |
AxHashMap / AxHashSet |
Serde #[derive(Serialize, Deserialize)] |
HashMap / HashSet |
| Custom / seeded hasher | AxHashMap::with_hasher(AxBuildHasher::with_seed(s)) |
Raw hashbrown access |
RawHashMap / RawHashSet |
Benchmark Results
Measured on Apple Silicon (release build, N = 100,000 items).
| Scenario | AxHashMap | std HashMap | Speedup |
|---|---|---|---|
Insert — u64 keys |
379 µs | 1,032 µs | 2.7× |
Insert — String keys |
896 µs | 1,673 µs | 1.9× |
| Lookup — all hits | 200 µs | 748 µs | 3.7× |
| Lookup — 50% hit / 50% miss | 767 µs | 1,994 µs | 2.6× |
| Iteration (full scan) | 130 µs | 124 µs | ~equal |
Iteration performance is effectively identical because iteration does not invoke the hasher.
Run the benchmarks yourself:
# HTML reports → target/criterion/
Installation
[]
= "0.1"
No feature flags required. AES acceleration is detected at runtime; a portable fallback is used automatically on CPUs without AES instructions.
Quick start
Using the type alias (HashMap)
use HashMap;
use BuildHasherDefault;
use AxHasher;
// Construct via Default (zero-cost).
let mut map: = default;
map.insert;
map.insert;
assert_eq!;
assert_eq!;
Using the branded newtype (AxHashMap)
use AxHashMap;
let mut scores: = new;
scores.insert;
scores.insert;
scores.insert;
// Index operator
assert_eq!;
// Safe lookup
assert_eq!;
HashSet
use AxHashSet;
let mut seen: = new;
seen.insert;
seen.insert;
seen.insert; // duplicate — ignored
assert_eq!;
assert!;
let a: = .into_iter.collect;
let b: = .into_iter.collect;
let union: = a.union.copied.collect;
let intersection: = a.intersection.copied.collect;
assert_eq!;
assert_eq!;
Constructors
Branded newtype constructors
use ;
// Default (zero seed)
let map: = new;
let set: = new;
// Pre-allocate to avoid rehashing
let map = with_capacity;
let set = with_capacity;
// Custom seed — use OS entropy for hash-flooding resistance
let seed: u64 = 0xdeadbeef_cafebabe;
let map: =
with_hasher;
// Custom seed + pre-allocated capacity
let map: =
with_capacity_and_hasher;
Type alias constructors
use HashMap;
// Use hashbrown's built-in constructors directly on the alias.
let mut map: = default;
let mut map = with_capacity;
Collecting from iterators
use ;
// FromIterator for AxHashMap
let map: =
.into_iter
.collect;
// FromIterator for AxHashSet
let set: = .into_iter.collect; // len == 3
// Extend
let mut map: = new;
map.extend;
map.extend;
All hashbrown methods are available
AxHashMap and AxHashSet implement Deref / DerefMut to the underlying
hashbrown::HashMap / hashbrown::HashSet, so every method — entry,
retain, drain, reserve, shrink_to_fit, and more — is directly
accessible without any extra imports.
use AxHashMap;
let mut map: = new;
map.entry.and_modify.or_insert;
map.entry.and_modify.or_insert;
assert_eq!;
map.insert;
map.retain;
assert!;
Interoperability with raw hashbrown types
The crate re-exports RawHashMap and RawHashSet (the bare hashbrown types)
and provides From conversions in both directions so you can cross the boundary
without a direct hashbrown dependency in your own Cargo.toml.
use BuildHasherDefault;
use ;
// Wrap a raw hashbrown map.
let raw: =
with_hasher;
let wrapped: = raw.into;
// Unwrap back to hashbrown.
let raw: = wrapped.into_inner;
When to use a custom seed
The default hasher uses a constant seed — the output is deterministic across runs. This is fine for most workloads.
If your map accepts keys from untrusted external input (e.g. HTTP request parameters) and you want to defend against hash-flooding attacks, supply a random seed derived from OS entropy:
use ;
// In production, generate from `rand`, `getrandom`, or similar.
let seed: u64 = 0x1234_5678_9abc_def0;
let mut map: =
with_hasher;
Feature flags
This crate has no feature flags. The AES hardware path is selected at runtime via CPUID — you always ship a single binary.
Dependency footprint
axhash-map
├── axhash-core (AxHasher + AxBuildHasher — AES hash engine)
└── hashbrown (SwissTable, no default features — ahash excluded)
License
MIT — see LICENSE.