Sub topic 9
Growth factor and capacity choice
This chapter assumes you know how a resize is triggered and how rehashing moves entries into a new array. It covers how much to grow the array and what capacities to allow.
The growth factor
The growth factor g is the ratio of new capacity to old capacity. Doubling means g = 2. After a resize, the load factor drops from just above the threshold to about threshold / g. With a threshold of 0.75 and g = 2, the table restarts at a load near 0.375. Inserts then raise the load back toward 0.75 before the next resize.
That fixes the range the load factor moves through:
- Largest value: the threshold, reached just before a resize.
- Smallest value: about
threshold / g, reached just after a resize.
Trade-offs of the factor
A larger g means fewer resizes, but the table can sit mostly empty. A smaller g wastes less memory, but each resize happens sooner and the total rehash work rises.
| Growth factor | Resizes for n inserts | Memory just after a resize | Typical use |
|---|---|---|---|
| 1.5 | More | Tighter | Memory-sensitive tables |
| 2 | Fewer | Looser | Common default |
| 4 | Fewest | Much looser | Small tables, rarely at scale |
You can measure this directly. The function below counts resizes and entries moved for several factors.
def simulate(n, growth, start=8, max_load=0.75):
capacity, resizes, moves = start, 0, 0
for size in range(1, n + 1):
if size / capacity > max_load:
moves += size
capacity = max(capacity + 1, int(capacity * growth))
resizes += 1
return capacity, resizes, moves
for growth in (1.5, 2, 4):
capacity, resizes, moves = simulate(100_000, growth)
print(growth, capacity, resizes, round(moves / 100_000, 2))Run it and compare the columns. Larger factors end with larger final capacities and fewer resizes. The moves-per-insert ratio stays a small constant for every factor, which the amortized-cost chapter explains.
Growth must be geometric, meaning multiplicative. Adding a fixed number of slots per resize (say, 100) makes the number of resizes proportional to n, and total rehash work becomes O(n²).
Choosing the capacity itself
The index is hash(key) % capacity, so the capacity affects how evenly keys spread. Two common choices exist.
Power of two. The modulo becomes a bitmask, which is faster than division:
capacity = 16
for h in (5, 21, 37):
print(h % capacity, h & (capacity - 1))Both columns match for non-negative integers. The cost is that only the low bits of the hash matter. Hashes that differ only in their high bits all collide:
capacity = 16
keys = [0, 16, 32, 48, 64]
print([k & (capacity - 1) for k in keys])Every key lands in bucket 0. Implementations that use power-of-two sizes protect against this by mixing the high bits into the low bits first, or by using a probe sequence that depends on the full hash.
Prime number. A prime capacity makes the modulo depend on all bits of the hash, so patterned keys such as multiples of 16 spread well. The cost is a slower division and the need to find a suitable prime at each resize, often from a precomputed list, so the growth factor is only approximate.
Practical guidance
- Pick the threshold first, based on the collision strategy. Open addressing needs a lower one than chaining.
- Pick
g = 2unless memory is tight. Use something near 1.5 if it is. - Choose power-of-two capacities when you control the hash function. Choose primes when you do not.
- If you know the final size ahead of time, allocate for it up front with
n / thresholdcapacity. This avoids every resize.
Exercise
A table has capacity 1000, a threshold of 0.75, and a growth factor of 1.5. The 751st entry triggers a resize. What is the load factor right after the resize completes?
Show answer
The new capacity is 1500. The load factor is 751 / 1500, which is about 0.50. This matches the rule of thumb threshold / g = 0.75 / 1.5 = 0.5.