Hash tables

Hand-drawn grid of cross-hatched cream, sand, umber and moss squares with six scattered terracotta squares and five short chains of linked terracotta squares, some upright and some level.

A hash table is a data structure that stores keys, usually with values, in an array. It finds each key by computing a hash, a number derived from the key that says where in the array to look. It answers lookups, insertions and deletions by exact key; ordered questions, such as the next key or all keys in a range, belong to search trees and ordered indexes. In an independent benchmark of C and C++ tables, flat tables that store entries in the array itself usually beat tables that chain entries in separate nodes (Allan, 2024). The ranking among flat designs, though, shifts with key distribution, load, key type and memory hardware (Richter et al., 2015; Debnath et al., 2015).

The main disputes concern which probing scheme and hash function are fastest, how deletions wear a table down, and when a plain scan beats hashing. A separate concern is security: an attacker who chooses the keys can force a table into its worst case (Crosby & Wallach, 2003).

How collisions are handled

Two keys can hash to the same slot, a collision, and hash table designs differ mainly in what they do next. In chaining, each slot holds a list of entries; GCC’s C++ library implements std::unordered_map with linked nodes indexed by a bucket array (Free Software Foundation, 2025). In open addressing, also called flat storage, entries sit in the array itself, and a collision sends the table to probe other slots in a fixed order.

The probe order defines the design: linear probing tries the next slots in turn, and quadratic probing jumps by growing steps. Robin Hood hashing is linear probing that moves entries so probe lengths stay even. Hopscotch hashing keeps each entry within a short distance of its home slot. Cuckoo hashing gives each key a few candidate positions and moves existing keys aside to make room. The load factor is the share of slots in use.

Swiss tables, from the Abseil C++ library, add a metadata byte per slot that holds a 7-bit fragment of the hash and the slot’s state (Abseil, n.d.c). A lookup uses SIMD instructions, which compare many bytes in one step, to pick out matching fragments among 16 candidates before comparing any full key (Abseil, n.d.c).

Which designs are fastest

Benchmarks agree that flat tables usually beat chained ones, but they disagree on the order among flat designs. In Allan’s benchmark, the node-based std::unordered_map was slower than most flat tables in most tests. Only on lookups of keys that were present did it match the slowest flat tables (Allan, 2024). The same benchmark found SIMD-filtered and hybrid tables less sensitive to high load than conventional linear, quadratic and Robin Hood tables (Allan, 2024).

The distribution of keys can reverse the ranking of probing schemes. In Richter and colleagues’ tests at higher loads, linear probing inserted 45 million dense integer keys per second against 35 million for quadratic probing, while quadratic probing led on every other key distribution tested (Richter et al., 2015). Dense keys here are integers packed into a narrow range, such as consecutive IDs.

Cuckoo hashing trades insertion speed for lookup speed in crowded tables. In the same study, a four-table cuckoo variant inserted at a relatively low but stable rate across loads. It pulled ahead of the other flat designs on lookups once load reached 80% and failed lookups became common (Richter et al., 2015).

Key type and memory hardware also move results. One tested table, Bytell, ranked better on lookups with integer keys than with string keys (Allan, 2024). For write-heavy work at high load, cuckoo beat hopscotch on ordinary memory, but hopscotch led on phase-change memory, where cuckoo’s chains of moved keys cost extra writes (Debnath et al., 2015).

Memory use and pointer stability

Flat tables save memory when entries are small, but they move entries when they grow, while node-based tables keep each entry at a fixed address. The C++ standard requires its unordered containers to keep pointers and references to elements valid across a rehash, the rebuild into a larger array, though iterators become invalid (ISO/IEC JTC1/SC22/WG21, n.d.). Abseil offers both kinds: its flat containers store keys and values inline and invalidate pointers on rehash, while its node containers keep node addresses stable (Abseil, n.d.a).

Which kind uses less memory depends on the size of the value. In Ankerl’s C++ test, Abseil’s flat map used 26.4 bytes per entry against 46.2 for its node map with 8-byte values. With 64-byte values it used 113.3 bytes against 94.2 (Ankerl, 2026).

Deletions, churn and growth

Deleted slots under churn

Under sustained deletion and reinsertion, deleted-slot markers can slow or exhaust a flat table unless it cleans them up, though this rests on a few measurements. In open addressing, a deleted slot cannot simply be emptied, since that would break probe sequences that pass through it; tables mark it with a tombstone instead. At 95% live load with equal numbers of updates and lookups, a simple linear-probing table with tombstones ran out of free slots after about 150 cycles of churn (Chesetti et al., 2025). In Ankerl’s erase-and-insert test, lookups of absent keys slowed from 3.91 to 5.71 ns in Boost’s table at 81% load. In Abseil’s table at 76% load they slowed from 6.26 to 14.62 ns (Ankerl, 2026).

In theory, tombstones need not wear linear probing down if the table rebuilds itself from time to time. In Bender and colleagues’ graveyard hashing, the expected cost per operation is proportional to x when the table is a fraction 1 − 1/x full. At 90% full, x is 10. Rebuilds add a cost of the same order, spread over operations (Bender et al., 2021). The proof assumes idealised random hashing, with extensions to practical hash families (Bender et al., 2021).

Spreading that rebuild cost over operations does not remove pauses. A full rebuild can stop the table and worsen tail latency even when the average cost stays low, so Zombie hashing redistributes tombstones locally during inserts and deletes (Chesetti et al., 2025). In its authors’ 95% churn test, the vectorized version reached 6.91 million operations per second and a modified Abseil table 9.81 million, which the authors describe as similar throughput (Chesetti et al., 2025).

Growth

Growing a hash table means rebuilding it into a larger array, and libraries differ in how much they rebuild at once. Go’s map, since version 1.24, splits its contents into subtables and grows one subtable at a time, leaving the others alone (Pratt, 2025). A directory selects the subtable from the hash, a scheme called extendible hashing, and each subtable holds at most 1,024 slots before it splits (The Go Authors, 2025c).

The 1,024-slot cap bounds the entries copied, not all the work of a split. When a split needs a deeper directory, Go allocates a directory twice the size and copies every old pointer, both in Go 1.24.0 and still in 1.27.4 (The Go Authors, 2025a; The Go Authors, 2026a). Replacing one subtable also rewrites every directory entry that points to it, and that count doubles each time the directory deepens without that subtable splitting (The Go Authors, 2026a).

Entry size matters too. During a split, Go 1.27.4 copies elements stored inline but keeps the existing pointers for elements stored indirectly. Its source notes that a limit in bytes rather than slots might better address copying costs for large objects (The Go Authors, 2026c).

What standard libraries ship

Rust and Go ship flat Swiss-style tables and Python a compact dictionary, while the C++ standard container and Java’s HashMap keep entries in buckets (The Rust Project Developers, 2025; Pratt, 2025; Python Software Foundation, 2025a; Free Software Foundation, 2025; Oracle and/or its affiliates, 2025).

LibraryDesign
C++ standard library (GCC 15.1)std::unordered_map: linked nodes indexed by a bucket array (Free Software Foundation, 2025); the standard requires pointers to stay valid across rehashing (ISO/IEC JTC1/SC22/WG21, n.d.)
Abseil (C++)Swiss tables (Abseil, n.d.c), in flat and stable-node versions (Abseil, n.d.a)
Rust 1.85HashMap is a port of SwissTable with quadratic probing and SIMD lookups (The Rust Project Developers, 2025)
Go 1.24 onwardBuilt-in map based on Swiss tables, split into subtables (Pratt, 2025; The Go Authors, 2025c); small maps match hash fragments before comparing keys (1.25) (The Go Authors, 2025b)
Python (CPython 3.14)Compact, ordered dict with hash indices stored apart from the entries (Python Software Foundation, 2025a); insertion order guaranteed by the language since 3.7 (Python Software Foundation, n.d.e)
Java (OpenJDK 25)HashMap uses ordinary buckets and switches large buckets to balanced trees (Oracle and/or its affiliates, 2025), a change made in JDK 8 (Oracle and/or its affiliates, 2014); iteration takes time proportional to buckets plus entries (Oracle and/or its affiliates, 2025)

Switching a whole language to a faster table gave large gains in isolated tests but small ones in real programs. Go’s maintainers measured map operations in 1.24 up to 60% faster than in 1.23 in microbenchmarks, with some regressions. Across full applications, CPU time fell by about 1.5% as a geometric mean (an average of ratios) (Pratt, 2025).

Python’s compact dictionary shows how layout alone can save memory. The design, first proposed by Hettinger in 2012, replaces a sparse array of full entries with a sparse array of small indices pointing into a dense array of entries (Hettinger, 2012). PyPy shipped a version in 2015 (Fijalkowski, 2015), and the CPython 3.6 release notes report 20–25% less dictionary memory than in 3.5 (Pranskevichus & Selivanov, 2016). The cost falls on churn: keeping insertion order can require repacking deleted entries, which made workloads that add and remove keys in equal numbers a bit slower (Fijalkowski, 2015).

Hash functions and hash flooding

Defending against hash flooding

When an attacker can choose the keys, a hash table’s worst case becomes a denial-of-service risk, and libraries defend with secret-keyed hash functions, tree buckets and input limits. Crosby and Wallach described these algorithmic-complexity attacks as deliberately provoking a data structure’s worst-case behaviour; capping how large a table untrusted input can make limits the damage (Crosby & Wallach, 2003).

Against hash tables, the attack is called hash flooding, a concern of web security. The common defence is a hash function seeded with a random secret. SipHash is a family of such functions designed for short inputs, with hash-table lookups protected against flooding among its targets (Aumasson & Bernstein, 2012). Python made SipHash its default string and bytes hash in 3.4 (Heimes, 2013), has used the variant SipHash-1-3 since 3.11 (Python Software Foundation, n.d.d), and seeds it randomly by default (Python Software Foundation, n.d.c). Rust’s HashMap defaults to a randomly seeded SipHash 1-3 chosen for this resistance, and documents a possible throughput cost on integer and long string keys (The Rust Project Developers, 2025).

Each defence has limits. Rust’s seed is only as random as the host’s entropy pool, which can be abnormally low (The Rust Project Developers, 2025). Java’s tree buckets bound the worst case to logarithmic time only when colliding keys have distinct hashes or can be ordered (Oracle and/or its affiliates, 2025). Input budgets are a separate layer: ASP.NET Core 10 limits parsed forms to 1,024 entries and 2,048-byte keys by default (Microsoft, n.d.e).

Fast non-cryptographic hashes are a different tool. The xxHash maintainers state that it is unsuitable for adversarial input where collisions benefit an attacker (xxHash, n.d.).

Hash functions for trusted keys

For keys an attacker cannot choose, studies disagree on how much speed a robust hash function costs. In Richter and colleagues’ integer-key tables at 50–90% load, the simple multiply-shift hash usually gave the highest throughput. Murmur varied less across key distributions, and tabulation hashing was the most robust but slowest (Richter et al., 2015). Pătraşcu and Thorup, by contrast, found simple tabulation competitive with multiply-shift in their linear-probing implementation on random input (Pătraşcu & Thorup, 2010).

Pătraşcu and Thorup also proved a bound for linear probing with simple tabulation. When the table has (1 + ε) times as many slots as keys, each operation takes expected time on the order of 1/ε² (Pătraşcu & Thorup, 2010). Here ε is the spare room: halving it multiplies the bound by four.

The raw speed of a hash function measures a different cost from the table it serves. On 100 KB inputs held in cache, xxHash’s maintainers report 59.4 GB/s for its AVX2 version against 31.5 GB/s for SSE2 (xxHash, n.d.).

Hash tables on graphics processors

On graphics processors (GPUs), bucketed cuckoo hashing can fill a table almost completely. Awad and colleagues’ best static GPU table used three-choice cuckoo hashing with 16-slot buckets and averaged 1.43 probes per insertion at up to 99% load (Awad et al., 2023). They recommend 98% load for at least 99% successful builds, against up to 91% for a 32-slot iceberg design. Their tests used uniform 32-bit keys on an NVIDIA Titan V (Awad et al., 2023).

Letting threads probe together helps at high load. In NVIDIA’s own A100 test, groups of four threads probing neighbouring slots gave up to 13% more insertion and 40% more lookup throughput at high load than single-thread probing (Juenger et al., 2023).

Compact layouts can save memory without losing speed. Hegeman and colleagues report 10–20% faster synthetic lookups and insertions from compact cuckoo and iceberg layouts, some needing half or nearly a quarter of the memory (Hegeman et al., 2024). Their compact iceberg table approached compact cuckoo’s speed and added a correct concurrent find-or-insert operation, but reached a lower maximum load (Hegeman et al., 2024).

When a linear scan beats a hash table Weak evidence: few or small studies support this

For very small sets, scanning an array can beat a hash table, but the size where hashing takes over rests on a few microbenchmarks that disagree widely. It ranges from about five entries for strings in Go to between 64 and 128 for integers in C++ (King, 2015; Nanavati, 2018).

Key type moves the crossover. In Nanavati’s C++ test, where lookups almost always missed, scanning 64-bit integers beat the hash set at 64 entries and lost at 128 (Nanavati, 2018). With 64-byte strings, the scan was already slower at 32, the smallest size tested (Nanavati, 2018). In King’s 2015 Go test, a string map tied the scan at five entries and won at ten, while an integer scan was faster at ten and slower at twenty (King, 2015).

Changes to the runtime move the crossover too. Go 1.25’s small maps compare a hash fragment against group metadata before checking any key (The Go Authors, 2025b). In the change’s benchmark, that made successful lookups in maps of 2–8 entries about 1.7 times faster, from about 25 ns to 14 ns (thepudds, 2025).

Other Go measurements also put the map ahead by ten string entries. One benchmark found the map about 9% faster at five string entries and 5.27 against 8.83 ns at ten, cycling through keys that were present (Wendt, n.d.). Another, on an Apple M3 Pro, measured 15.82 ns for the map against 45.3 ns for the scan at ten entries (Vas, 2025).

Further reading

  • Richter, Alvarez and Dittrich, “A seven-dimensional analysis of hashing methods” (Richter et al., 2015): a controlled comparison of probing schemes, hash functions, loads and key distributions on integer keys.
  • Allan, “An extensive benchmark of C and C++ hash tables” (Allan, 2024): an independent benchmark of many current open-source tables, with results by load factor and key type.
  • Abseil, “Swiss Tables design notes” (Abseil, n.d.c): how metadata bytes and SIMD filtering work in the design Rust’s and Go’s tables follow.
  • Pratt, “Faster Go maps with Swiss Tables” (Pratt, 2025): a maintainer’s account of moving a language’s built-in map to Swiss tables, with incremental growth and measured gains.
  • Crosby and Wallach, “Denial of service via algorithmic complexity attacks” (Crosby & Wallach, 2003): describes algorithmic-complexity attacks, which deliberately provoke a data structure’s worst case, and how input limits contain them.
  • Bender, Kuszmaul and Kuszmaul, “Linear probing revisited” (Bender et al., 2021): the theory of how tombstones affect linear probing under deletions.

Known gaps

What this article cannot tell you yet.

  1. The pause caused when Go’s map doubles its directory of subtables is not timed in the measurements available.
  2. How fast tables degrade under sustained deletes is known only at a few occupancies, and no study pairs counts of deleted-slot markers per cycle with latency.
  3. The size at which Go’s Swiss-table map overtakes a slice scan is not measured for the current release.
  4. Hash functions for trusted keys are compared mainly in integer-key tables.
  5. Hash tables on persistent memory are barely covered.
  6. The design histories behind Rust’s table, Java’s tree bins, libstdc++’s chaining and the C++ pointer-stability rule are not covered.

References

Solid source
Rated highly trustworthy by the research, and every passage taken from it was found in it.
Weak source
Rated low, or a passage taken from it was not found in it.
Medium trust
Rated medium by the research.
Not checked
Rated high, but its passages could not all be checked against it.
Show all 40 references