go.reflect_in_hot_path
Performance
Medium
Detects reflect package usage in frequently-called code paths.
Why It Matters
Section titled “Why It Matters”Reflection in hot paths:
- 10-100x slower — Reflection bypasses compile-time optimizations
- More allocations — Reflect creates temporary objects
- No inlining — Defeats compiler optimizations
Example
Section titled “Example”// ❌ Before (reflection in loop)func processItems(items []interface{}) { for _, item := range items { v := reflect.ValueOf(item) // Reflection on every iteration }}// ✅ After (type assertion)func processItems(items []Item) { for _, item := range items { // Direct field access, no reflection process(item.Name) }}What Unfault Detects
Section titled “What Unfault Detects”reflect.ValueOf()in loopsreflect.TypeOf()in handlers- Reflection in hot code paths
Auto-Fix
Section titled “Auto-Fix”Unfault suggests type assertions or interfaces:
// Use type switch instead of reflectionswitch v := item.(type) {case string: processString(v)case int: processInt(v)}