Make(map) With Capacity Hint — What Does
Fast recall
Quick revision
Core answer
Practical understanding of Maps Make And Literal.
Repeating a definition without saying when to use it.
Give one trade-off and say how you would test it.
Interview-ready guide
Interview answer
The size argument in make(map[K]V, n) is a capacity hint, not a limit. It tells the runtime 'I expect about n entries — size the initial bucket structure for that,' avoiding the early growth-and-rehash cycles as the map fills from zero.
The hint is purely advisory. Exceed it and the map grows normally — no error, no truncation, no visible difference except allocation timing. Undershoot it (hint 1000, use 10) and you've wasted some memory up front; the runtime doesn't refund it.
When it matters: maps that will definitely hold a known-count set — building a lookup from a slice of length n (hint n), a count-by-key aggregation over known input. For small maps or unknown sizes, the hint buys nothing measurable; the default growth handles small maps in one allocation anyway.
And note the contrast with slices: make([]T, n) on a slice makes n ELEMENTS (zeroed, len n). make(map, n) makes capacity for n entries but zero entries exist. The same function name, two different size semantics — the slice's number is a length, the map's is a hint.
m := make(map[string]int, 1000) // hint: ~1000 entries, grows past it fine
// vs make(map[string]int) — same results, one up-front allocationMake(map) With Capacity Hint — What Does
Direct Answer
make(map[K]V, n) pre-allocates roughly n entries' worth of buckets. A performance hint only — the map grows past n without error.
Deep Dive & Architecture
What the hint controls is the initial bucket count. A Go map is a hash table: an array of buckets, each holding ~8 entries, with overflow chaining and incremental rehashing on growth. From empty, the first inserts trigger several grow-rehash steps as the entry count crosses thresholds — cheap individually, but a loop inserting 100k entries into an unhinted map pays for every growth along the way.
make(map[string]int, 100_000) pre-sizes the bucket array for ~100k entries — the growth steps happen once at allocation instead of during the fill loop. For big, size-knowable builds that's a real win; benchmarks show it clearly at six-figure sizes.
The '~' in 'roughly n buckets' matters: the runtime computes the bucket count from n via its own formula (n/8 buckets, rounded per growth class), so hint 100 doesn't allocate exactly 100 slots' worth — it allocates the size class that fits. That's fine; the hint is an input to a heuristic, not an exact reservation.
The debugging lesson it encodes: map performance issues are usually allocation/rehash churn, visible in profiling as runtime.makemap and growth allocations. When a profile shows that, the hint is the fix; when it doesn't, adding hints is noise.
Common Mistakes & Pitfalls
Repeating a definition without saying when to use it.
Check your understanding
Answer these without looking back at the article.
- What is Maps Make And Literal, and when would you use it?
- How would you compare Maps Make And Literal with an alternative?
- What is a common mistake when using Maps Make And Literal?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.