Sub topic 11
Choosing a Growth Factor
This chapter assumes you know how a dynamic array tracks size and capacity, and that a resize allocates a new block and copies the elements. You should also know introductory Big-O notation. Here we ask one question: when the array is full, how much bigger should the new block be?
Growth policies
A growth policy decides the new capacity. There are two common kinds.
- Additive growth adds a fixed number of slots, such as
capacity + 10. - Geometric growth multiplies the capacity by a factor
g, such ascapacity * 2.
Additive growth looks safe because it wastes little memory. It has a cost problem, though. If you add k slots each time, you resize after every k appends. Each resize copies all current elements. For n appends, the total copying grows like n²/k, so the average cost per append is O(n). Geometric growth keeps the total copying proportional to n, which is why append is amortized O(1).
Measuring the cost
The program below counts element copies for both policies. It starts from capacity 1 and appends 100,000 items.
def count_copies(n, next_capacity):
capacity = 1
size = 0
copies = 0
for _ in range(n):
if size == capacity:
copies += size
capacity = next_capacity(capacity)
size += 1
return copies, capacity
n = 100_000
print("add 10: ", count_copies(n, lambda c: c + 10))
print("double: ", count_copies(n, lambda c: c * 2))
print("grow 1.5:", count_copies(n, lambda c: max(c + 1, int(c * 1.5))))Additive growth makes hundreds of millions of copies. Doubling makes fewer than n, and a factor of 1.5 makes about 2n. The max(c + 1, ...) guard keeps capacity growing when int(c * 1.5) would round down to the same value at very small sizes.
The trade-off
A larger factor means fewer resizes but more unused slots. A smaller factor wastes less memory but copies more often.
| Factor | Copies per append (approx.) | Worst-case unused space |
|---|---|---|
| 2 | about 1 | up to 50% of capacity |
| 1.5 | about 2 | up to 33% of capacity |
| 1.25 | about 4 | up to 20% of capacity |
The copy figure comes from the geometric series. With factor g, the total copies over n appends is about n / (g - 1).
Why some libraries avoid 2
With factor 2, each new block is larger than the sum of all earlier freed blocks (1 + 2 + 4 + ... + 2^(k-1) is less than 2^k). An allocator therefore cannot reuse the freed space for the next block, so the array keeps moving to fresh memory. A factor below about 1.618 (the golden ratio) lets freed blocks eventually combine to fit a new one. This argument is often cited for factors such as 1.5. It depends on the allocator, so treat it as a tuning consideration, not a rule.
Choosing in practice
- Use 2 when speed of append matters most and extra memory is cheap.
- Use 1.5 or less when memory is tight or arrays are very large.
- If you know the final size, reserve it up front and avoid resizing.
- Never use a fixed additive step for arrays that can grow large.
Exercise
An array grows by a factor of 1.25. Using the approximation n / (g - 1), about how many element copies happen over 1,000,000 appends, and what does that mean per append?
Show answer
g - 1 = 0.25, so the total is about 1,000,000 / 0.25 = 4,000,000 copies. That is about 4 copies per append, which is still O(1) amortized, but four times the copying of factor 2.