Sub topic 16
How Standard Libraries Implement Hash Tables
This chapter assumes you have read the earlier chapters on chaining, open addressing, load factor, and hash flooding. It shows how those ideas appear in widely used languages. The details below describe common current implementations and can change between versions.
Same ideas, different choices
Every standard library table makes four decisions: how to resolve collisions, when to resize, which hash function to use, and whether to keep a useful iteration order.
| Library | Collision strategy | Resize trigger | Notable detail |
|---|---|---|---|
| Python `dict` | Open addressing with a perturbed probe sequence | About 2/3 full | Insertion order is kept |
| Java `HashMap` | Separate chaining | Load factor 0.75 | Long chains become balanced trees |
| C++ `std::unordered_map` | Chaining (the bucket interface requires it) | Default max load factor 1.0 | Pointer-based nodes are not cache friendly |
| Rust `HashMap` | Open addressing (SwissTable design) | Checks groups of slots at once | Randomized SipHash by default |
Python: a compact dict
Since Python 3.6, a dict has two arrays. A small sparse index array holds positions. A dense entries array holds the (hash, key, value) records in insertion order. Probing happens in the index array, which is small and cache friendly. Because entries are appended densely, iteration follows insertion order, and Python 3.7 made this a language guarantee.
You can watch resizing happen. The exact sizes depend on your Python version.
import sys
d = {}
last = sys.getsizeof(d)
for i in range(100):
d[i] = i
size = sys.getsizeof(d)
if size != last:
print(f"resized at {len(d)} entries: {last} -> {size} bytes")
last = sizeThe memory size jumps at only a few points. Between jumps, inserts are cheap. This is the amortized doubling from the load factor chapter.
Java: chaining with a safety net
HashMap starts with 16 buckets and doubles at a load factor of 0.75. Since Java 8, a bucket whose chain reaches 8 entries is converted to a red-black tree, provided the table has at least 64 buckets. This changes the worst case for a single bucket from O(n) to O(log n) when keys are comparable. It is a cheap defense against many keys colliding, whether by accident or by attack.
Rust and others: SwissTable
Rust's HashMap uses the hashbrown implementation, based on Google's SwissTable. It stores a byte of metadata per slot and compares a group of metadata bytes at once using SIMD instructions. This rejects most non-matching slots without touching the keys. Go switched its built-in map to a Swiss table design in version 1.24.
Defaults reflect threats
Python and Rust randomize their string hashing with SipHash and a per-process secret. An attacker cannot predict which keys collide, which blocks hash flooding. Java relies on tree buckets instead. Neither approach makes the worst case impossible. Each makes it hard to trigger on purpose.
Practical consequences
- Pre-sizing helps when you know the count in advance. Java's
new HashMap<>(capacity)and Rust'sHashMap::with_capacityavoid repeated resizes. - Keys must keep equality and hash consistent. Mutating a key after insertion breaks lookups in every library.
- For latency-sensitive code, a resize copies every entry. One insert can be much slower than the average.
Exercise
A Java HashMap has 64 buckets and one bucket holds 9 colliding keys that implement Comparable. Roughly what lookup cost does a key in that bucket have, and why?
Show answer
About O(log n) in the size of that bucket. Because the chain is at least 8 long and the table has at least 64 buckets, Java converts the bucket to a red-black tree, so lookups compare keys down a balanced tree instead of scanning a list.