Skip to content

go.cpu_in_hot_path

Performance Medium

Detects CPU-intensive operations (reflection, regex, JSON marshaling) in frequently-called code paths.

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
// ❌ 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
}
}
  • reflect package usage in loops
  • JSON marshal/unmarshal in hot paths
  • Regex operations in loops (use pre-compiled patterns)
  • Hash computations in tight loops

Unfault suggests moving expensive operations out of loops or pre-computing values.