Add Grow to presize an existing map
Go code can presize a new map with make(M, hint), but a map that already exists grows one rehash at a time. Code that merges many entries into a map it doesn't own pays for every intermediate grow. Examples include copying headers into ResponseWriter.Header(). Presizing before a copy measured 53–65% faster.
The new maps.Grow function grows an existing map in place so that at least n more entries fit without further growth, as if the map had been created with make(M, len(m)+n). This is best effort: it cannot guarantee that no more growth will happen.
func Grow[M ~map[K]V, K comparable, V any](m M, n int)
maps.Grow(dst, len(src))
for k, v := range src {
dst[k] = v
}
maps.Copy(dst, src) will also pre-grow dst to fit len(src) new entries, so existing callers of Copy get the speedup without code changes.
- Proposal
- #81698 by lizthegrey
- Status
- Implemented on Nov 7, 2026
- Release
- Go 1.28
- Impact
- medium
- Area
- maps