Sub topic 13
What Real Libraries Choose
This chapter assumes you know how separate chaining and open addressing work, what load factor means (entries divided by capacity), and how a resize rehashes every entry. You should also be able to read short Python code.
The settings differ more than you might expect
The textbook rule is to resize when the load factor passes some threshold, and to double the capacity. Real libraries follow that outline, but each one picks its own threshold and growth step. The choices follow from how each table resolves collisions.
| Library | Collision strategy | Max load factor | Growth |
|---|---|---|---|
| Java `HashMap` | Chaining | 0.75 (configurable) | Doubles, capacity stays a power of two |
| C++ `std::unordered_map` | Chaining | 1.0 (adjustable with `max_load_factor`) | Implementation-defined, often about 2x |
| CPython `dict` | Open addressing | 2/3 of slots usable | Based on 3x the live entries, rounded up to a power of two |
| Rust `HashMap` (hashbrown) | Open addressing (Swiss table) | 7/8 | Doubles |
| Go `map` (before 1.24) | Chaining in buckets of 8 slots | 6.5 entries per bucket | Doubles, moved incrementally |
Go 1.24 replaced its map with a Swiss table design, so the last row describes older releases.
Chaining tolerates higher load
A chained table degrades gradually. At load 1.0 the average chain holds about one entry, so lookups stay cheap. That is why C++ defaults to 1.0 and Java accepts 0.75.
Java adds a safeguard. When a single bucket grows past 8 entries and the table has at least 64 buckets, that bucket becomes a balanced tree. A bad hash function then costs O(log n) per lookup instead of O(n).
Open addressing needs lower load
Linear probing cost grows sharply as load approaches 1, as the probe formulas earlier in this tutorial showed. Open-addressing tables therefore resize earlier.
CPython caps its dict at 2/3 full. Swiss tables such as Rust's reach 7/8 because they check a group of slots at once using control bytes, which makes each probe cheaper. The threshold depends on the cost per probe as well as the probe count.
Computing when the first resize happens
You can compute how many entries each default holds before the next insert triggers a resize.
def entries_before_resize(capacity, max_load):
return int(capacity * max_load)
settings = [
("Java HashMap", 16, 0.75),
("CPython dict", 8, 2 / 3),
("Rust hashbrown", 16, 7 / 8),
]
for name, capacity, max_load in settings:
print(name, entries_before_resize(capacity, max_load))This prints 12, 5 and 14. Java starts with 16 buckets and resizes on the 13th insert. CPython starts with 8 slots and resizes on the 6th. In hashbrown, a table with 16 buckets holds 14 entries.
Growth factor and power-of-two sizes
Most of these libraries keep capacity a power of two. Then hash & (capacity - 1) replaces the slower modulo operation. The cost is that low bits of the hash must be well mixed. Java XORs the high bits of the hash into the low bits for this reason. Some C++ implementations use prime bucket counts instead, which tolerates weak hash functions but pays for a real division.
CPython grows by more than 2x because dicts are often built once and then read. A larger step means fewer resizes during construction, at the price of more memory.
Incremental resizing in latency-sensitive systems
A single doubling is O(n) and can stall a request. Redis keeps two tables during a resize. Each later operation moves a few buckets from the old table to the new one, and lookups check both tables until the old one is empty. Older Go maps work the same way, moving old buckets as inserts and deletes touch them. Java and CPython do the whole rehash at once, because their typical workloads tolerate the pause.
Choosing between defaults and tuning
- Keep the defaults unless profiling shows resize time or memory is a problem.
- Lower the threshold when lookup speed matters more than memory.
- Raise it when memory is tight and lookups are cheap, which suits chaining better than probing.
- Pre-size the table when the final count is known.
Exercise
A Java HashMap is created with the default capacity of 16 and load factor 0.75. You want to insert 100 entries without any resize. What is the smallest power-of-two capacity you should request, and why?
Show answer
256. Without a resize you need capacity * 0.75 >= 100, so capacity >= 133.3. The smallest power of two at or above that is 256. Java rounds a requested capacity up to a power of two anyway. A capacity of 128 would hold only 96 entries before the 97th insert triggers a resize.