Go Map Buckets, Tophash, and Incremental Growth
Go maps store entries in fixed-size buckets, use a one-byte tophash value to reject most non-matches quickly, and move entries incrementally when the table grows. During that move, lookups know whether to search the old bucket or its new destination, so growth does not require a long stop-the-world reorganization.
This describes the classic bucket-based map implementation used by Go through Go 1.23. Go 1.24 introduced a different Swiss Table-based implementation, so internal details can vary by toolchain. The terms buckets, tophash, and overflow refer to the classic implementation.
The problem a map has to solve
A map must turn a key into a location quickly. A hash function converts the key into a number that appears random; the map then uses part of that number to choose a bucket. But many keys can produce the same bucket index. The map therefore needs a compact collision strategy that avoids scanning every key in the map.
It also needs to grow. A fixed array would become crowded as entries were added, causing more collisions and longer searches. Rebuilding the entire table at once would make one insertion or deletion unexpectedly expensive. Go solves this by keeping the old table temporarily and moving its buckets in small pieces.
What a bucket contains
A classic Go map bucket holds up to eight entries. Conceptually, it contains:
- Eight short hash fragments, called tophash values
- Space for up to eight keys
- Space for up to eight values
- A pointer to an overflow bucket, if more than eight entries map there
The name “tophash” means that each byte stores a selected high portion of the key’s full hash. The rest of the hash is used to select the bucket. The key itself is still stored and must be compared before a lookup can succeed; tophash is only a fast preliminary filter.
For example, a lookup can calculate a key’s hash, select one bucket, and compare the stored tophash bytes in that bucket. If a byte differs, the stored key cannot match, so Go skips loading and comparing that key. If the byte matches, Go performs the complete key comparison. This is especially useful when keys are large or when many entries share a bucket.
Some tophash values are reserved to mean states such as “this slot is empty” or “this bucket has been evacuated.” They are metadata, not ordinary hash fragments, and let map operations understand both slot occupancy and growth state.
Overflow buckets handle collisions
Suppose more than eight keys select the same bucket. Go keeps the first eight in the main bucket and links another bucket onto it. That linked bucket is an overflow bucket and also has room for eight entries. Further collisions can create a chain.
This avoids probing through unrelated buckets, but chains are not free: each link can mean another memory access, and long chains make lookups slower. Overflow accumulation is therefore one signal that can cause a map to grow even when its total entry count is not yet high enough to hit the normal load limit.
The map keeps a count of its primary buckets. If there are 2^B primary buckets, Go uses B bits of the hash to select one. The remaining hash bits, including the part represented by tophash, help distinguish entries within that bucket.
Growing the bucket array
Go normally grows a map by allocating twice as many primary buckets. Adding one bucket-index bit means each old bucket divides into two possible new destinations. An old bucket numbered i can therefore become new bucket i or new bucket i + oldBucketCount, depending on the next hash bit.
Growth is triggered mainly by a load-factor threshold: too many entries per bucket would make collisions common. Excessive overflow can also trigger a same-size grow, which keeps roughly the same number of primary buckets but rebuilds the layout to eliminate accumulated overflow chains.
The important point is that Go does not immediately move every entry. The map records both the new bucket array and the old one, then evacuates old buckets gradually. Evacuation means copying the entries from an old bucket and its overflow chain into their correct destination buckets in the new array.
How lookups work during evacuation
While a grow is in progress, a lookup first calculates the bucket it would use in the new layout. It then checks whether the corresponding old bucket has already been evacuated.
- If the old bucket has not been evacuated, the lookup searches the old bucket and its overflow chain.
- If it has been evacuated, the lookup searches the appropriate new bucket instead.
The old bucket’s metadata records this state. During evacuation, each entry is placed in one of the new bucket’s two possible destinations, based on the additional hash bit. Once that bucket is marked evacuated, future operations no longer need its old contents.
A lookup does not need to wait for all other buckets to move. It only needs to follow the map’s current state for the bucket relevant to its key. This is why reads can continue to work while evacuation is incomplete.
How evacuation progresses
Map operations do small amounts of migration work. An insertion or deletion that encounters a growing map evacuates the bucket needed for that operation and usually advances evacuation through another bucket as well. The map tracks the next old bucket that still needs processing.
When an operation reaches an old bucket that has not yet moved, it may first evacuate it, then perform the operation against the new table. Other operations progressively drain the remaining old buckets. Once all old buckets have been evacuated, the map releases the old bucket array and operates only on the new one.
This spreads the cost of resizing across many operations instead of charging one insertion for a complete rebuild. The trade-off is temporary complexity: during the grow, a lookup may need to understand both layouts, and an operation may do migration work in addition to its normal map work.
The practical result is that a map grow is not a single event in which every entry moves at once. It is a transition period in which old and new bucket arrays coexist, with tophash metadata telling each operation which state it is observing.
The same behavior explains runtime profiles or implementation discussions that mention buckets, oldbuckets, overflow, nevacuate, or “evacuation.” These are the internal pieces that let Go preserve ordinary map semantics while controlling the latency cost of resizing.