Skip to content

go.map_without_size_hint

Performance Low

Detects map initialization without capacity hint when the expected size is known.

Maps without size hints:

  • Repeated rehashing — Map grows and rehashes as items added
  • Memory copying — Each grow copies all entries
  • Fragmented memory — Multiple allocations instead of one
// ❌ Before (no hint)
result := make(map[string]int)
for _, item := range items {
result[item.Key] = item.Value
}
// ✅ After (with hint)
result := make(map[string]int, len(items))
for _, item := range items {
result[item.Key] = item.Value
}
  • make(map[K]V) in loops where size is known
  • Map initialization followed by loop filling it

Unfault adds size hints:

result := make(map[string]int, len(items))