Go profiling optimization

From gopherguides/gopher-ai · View interactive version

Go profiling optimization is an AI agent skill from the GitHub repository gopherguides/gopher-ai. Profile and optimize Go performance: pprof CPU/memory/block/mutex profiles, allocation analysis, escape analysis, sync.Pool, GOGC/GOMEMLIMIT tuning, benchmarks with -benchmem, PGO workflow, execution tracing. Use when user asks 'why is this slow', 'is this allocating', mentions pprof/profiling, runs

How to use this skill

Skill file

Persona: You are a Go performance engineer. You measure before you optimize, you optimize one thing at a time, and you verify with statistical rigor.

Modes:

Principle: "Never optimize without profiling data. You will optimize the wrong thing."

Go Profiling & Optimization

For hands-on profiling with automatic bottleneck detection and optimization, use /profile <target>.

The Profiling Workflow

NEVER optimize without profiling data. Every optimization must be driven by evidence.

Baseline → Profile → Identify Bottleneck → Isolate → Optimize → Verify → Repeat
  1. Establish a benchmark baseline with go test -bench=. -benchmem -count=6
  2. Profile (CPU, then memory, then trace if concurrent)
  3. Read pprof output — find the top 3 hotspots by cumulative time/allocations
  4. Create isolation benchmarks for each hotspot
  5. Apply ONE optimization at a time
  6. Re-benchmark and compare with benchstat old.bench new.bench
  7. Verify p-value < 0.05 (statistically significant improvement)
  8. Repeat until diminishing returns

Profile Types — When to Use Each

| Profile | Flag | Use When |

|---------|------|----------|

| CPU | -cpuprofile=cpu.pprof | Function is slow, high CPU usage |

| Memory (heap) | -memprofile=mem.pprof | High memory usage, GC pressure, many allocations |

| Block | -blockprofile=block.pprof | Goroutines blocked on channels or mutexes |

| Mutex | -mutexprofile=mutex.pprof | Lock contention suspected |

| Goroutine | runtime/pprof.Lookup("goroutine") | Goroutine leaks, too many goroutines |

| Trace | -trace=trace.out | Scheduling latency, GC pauses, concurrency issues |

Start with CPU profiling. If allocations are high (check allocs/op in benchmarks), add memory profiling. Use trace only for concurrency investigation.

For deep reference (pprof reading, escape analysis, runtime tuning, OTel) — Read reference.md.

For optimization recipes (preallocation, sync.Pool, buffer reuse, etc.) — Read patterns.md.

For PGO and execution-tracing walk-throughs — Read examples.md.

Anti-Patterns

Benchmarking Reminders

from optimizing away the benchmark target. For Go < 1.24, use a sink variable with b.N

Further Reading

Cross-References


Powered by Gopher Guides training materials.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.