Computer Science
Why Hash Tables Resize
Load factor, chaining, open addressing, and why a hash table that never grows eventually stops being fast.
Introduction
Section titled “Introduction”A hash table promises average constant-time lookup, insertion, and deletion. That promise quietly depends on one thing: the table must stay mostly empty. As entries accumulate, collisions pile up and performance degrades. Resizing — growing the table and rehashing everything — is how hash tables keep their promise.
The core concept
Section titled “The core concept”A hash table maps keys to buckets using a hash function. The load factor is the ratio of entries to buckets: n / capacity. With a good hash function, the expected number of entries per bucket is roughly the load factor, so lookup cost grows with it. A load factor of 0.75 means lookups inspect about one entry on average; a load factor of 10 means about ten.
Two strategies handle collisions:
- Chaining stores a list (or tree) of entries in each bucket. Lookups degrade gracefully, but long chains cost cache misses and pointer chasing.
- Open addressing probes for the next free slot when a bucket is occupied. It is cache-friendly at low load factors, but as the table fills, probes cluster and performance falls off a cliff — probing cost grows sharply once the load factor passes roughly 0.7.
How resizing works
Section titled “How resizing works”When the load factor crosses a threshold, the table allocates a larger array — usually double the size — and rehashes every entry into it. Because bucket indices depend on capacity (typically hash % capacity), every entry may move. This is an O(n) operation, but it happens rarely: doubling means the table must double in size again before the next resize, so the cost amortises to O(1) per insertion on average.
The threshold is a trade-off. A higher threshold saves memory but costs speed; a lower threshold wastes space. Java’s HashMap defaults to a load factor of 0.75, and CPython’s dict grows when it is roughly two-thirds full — both land in the same practical sweet spot.
Why it matters
Section titled “Why it matters”Understanding resizing explains several real behaviours. A hash map that grows one entry at a time can stall on a single insertion while it rehashes millions of entries — a latency spike that shows up in production profiles. Pre-sizing a map when the final size is known (Java’s HashMap(int initialCapacity), or building a dict from a known-size iterable) avoids repeated resizes entirely. And a hash function that clusters keys defeats resizing altogether: no amount of growth helps if most keys land in the same few buckets, which is why hash flooding — deliberately crafting colliding keys — is a real denial-of-service vector that modern hash functions defend against with randomised seeding.
Misconceptions
Section titled “Misconceptions”Resizing does not make a hash table “sorted” or ordered — iteration order after a resize is effectively arbitrary and can change between runs. And a hash table is not a cache: growing it without bound to avoid resizes trades one problem (rehash cost) for another (memory pressure and garbage-collection pauses).
Related reading
Section titled “Related reading”References
Section titled “References”- Java HashMap documentation (Oracle)
- CPython dict implementation notes
- Cormen, Leiserson, Rivest, Stein, Introduction to Algorithms, 3rd ed., Chapter 11