Why Growing a Hash Table Repositions Keys

A common table indexing rule depends on capacity. Growing that capacity can change a key's home slot even when its hash stays constant.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Calculate one moved position
  2. Keep the operation atomic at its boundary
  3. Recompute positions for the new capacity
  4. Explain why individual expensive resizes can be affordable

Calculate one moved position

A hash of 13 gives index one at capacity four and index five at capacity eight under a modulo rule. Copying a slot by its old index can break lookup. Quotient and remainder gives both positions.

Keep the operation atomic at its boundary

Build the new placement before treating it as the active table when the implementation can fail midway. Test existing keys after growth, including old collisions. Load factor illustrates the reason such a transfer can be useful.[1]

Recompute positions for the new capacity

With remainder indexing, key nine begins at index four for capacity five. At capacity eleven its home position is nine. Copying the old slot into the larger array without rebuilding would leave it unreachable to the new lookup. See load factor.

Key    index mod 5    index mod 11
9      4              9
14     4              3
20     0              9

Explain why individual expensive resizes can be affordable

Geometric growth avoids rebuilding at every increase in size. Sizes that roughly double give a geometric sum of previously moved entries. A resize can still take substantial time on a single insertion. Test keys that collide before resizing and others that collide afterward.

References

  1. Jeff Erickson, Algorithms ↩