go.map_without_size_hint
Performance
Low
Detects map initialization without capacity hint when the expected size is known.
Why It Matters
Section titled “Why It Matters”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
Example
Section titled “Example”// ❌ 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}What Unfault Detects
Section titled “What Unfault Detects”make(map[K]V)in loops where size is known- Map initialization followed by loop filling it
Auto-Fix
Section titled “Auto-Fix”Unfault adds size hints:
result := make(map[string]int, len(items))