go.cpu_in_hot_path
Performance
Medium
Detects CPU-intensive operations (reflection, regex, JSON marshaling) in frequently-called code paths.
Why It Matters
Section titled “Why It Matters”CPU work in hot paths causes:
- Increased latency — P99 latency spikes
- Reduced throughput — Less requests per second
- Higher costs — Need more instances to handle load
- Poor scaling — Performance degrades under load
Example
Section titled “Example”// ❌ Before (expensive in loop)func processItems(items []Item) { for _, item := range items { json.Marshal(item) // Reflection on every iteration reflect.TypeOf(item) // Even more expensive }}// ✅ After (optimized)func processItems(items []Item) { // Batch marshal if needed data, _ := json.Marshal(items)
// Or pre-compute type information itemType := reflect.TypeOf(Item{}) for _, item := range items { // Use cached type info }}What Unfault Detects
Section titled “What Unfault Detects”reflectpackage usage in loops- JSON marshal/unmarshal in hot paths
- Regex operations in loops (use pre-compiled patterns)
- Hash computations in tight loops
Auto-Fix
Section titled “Auto-Fix”Unfault suggests moving expensive operations out of loops or pre-computing values.