go.slice_append_in_loop
Performance
Low
Detects slice appends in loops without pre-allocation, causing repeated reallocations.
Why It Matters
Section titled “Why It Matters”Appending without pre-allocation:
- Multiple reallocations — Slice grows exponentially, copying each time
- Memory churn — Old backing arrays become garbage
- GC pressure — More work for garbage collector
Example
Section titled “Example”// ❌ Before (grows slice repeatedly)var result []Itemfor _, id := range ids { result = append(result, fetchItem(id))}// ✅ After (pre-allocated)result := make([]Item, 0, len(ids))for _, id := range ids { result = append(result, fetchItem(id))}What Unfault Detects
Section titled “What Unfault Detects”append()in loops withoutmake([]T, 0, n)initialization- Growing slices without capacity hint when size is known
Auto-Fix
Section titled “Auto-Fix”Unfault adds slice pre-allocation:
result := make([]Item, 0, len(ids))