Loading...
Loading...
Golang performance optimization patterns and methodology - if X bottleneck, then apply Y. Covers allocation reduction, CPU efficiency, memory layout, GC tuning, pooling, caching, and hot-path optimization. Use when profiling or benchmarks have identified a bottleneck and you need the right optimization pattern to fix it. Also use when performing performance code review to suggest improvements or benchmarks that could help identify quick performance gains. Not for measurement methodology (see golang-benchmark skill) or debugging workflow (see golang-troubleshooting skill).
npx skill4agent add samber/cc-skills-golang golang-performanceultrathinksamber/cc-skills-golang@golang-troubleshootingfgprofgo tool pprofnet.(*conn).Readdatabase/sqlsamber/cc-skills-golang@golang-databasesamber/cc-skills-golang@golang-benchmarkgo test -bench=BenchmarkMyFunc -benchmem -count=6 ./pkg/... | tee /tmp/report-1.txtbenchstat /tmp/report-1.txt /tmp/report-2.txt/tmp/report-*.txt| Bottleneck | Signal (from pprof) | Action |
|---|---|---|
| Too many allocations | | Memory optimization |
| CPU-bound hot loop | function dominates CPU profile | CPU optimization |
| GC pauses / OOM | high GC%, container limits | Runtime tuning |
| Network / I/O latency | goroutines blocked on I/O | I/O & networking |
| Repeated expensive work | same computation/fetch multiple times | Caching patterns |
| Wrong algorithm | O(n²) where O(n) exists | Algorithmic complexity |
| Lock contention | mutex/block profile hot | → See |
| Slow queries | DB time dominates traces | → See |
| Mistake | Fix |
|---|---|
| Optimizing without profiling | Profile with pprof first — intuition is wrong ~80% of the time |
Default | |
| Logging in hot loops | Log calls prevent inlining and allocate even when the level is disabled. Use |
| panic allocates a stack trace and unwinds the stack; use error returns |
| Only justified when profiling shows >10% improvement in a verified hot path |
| No GC tuning in containers | Set |
| 50-200x slower than typed comparison; use |
samber/cc-skills-golang@golang-benchmarkbenchdiffcobsamber/cc-skills-golang@golang-benchmarkbenchstatb.Loop()samber/cc-skills-golang@golang-troubleshootingsamber/cc-skills-golang@golang-data-structuresstrings.Buildersamber/cc-skills-golang@golang-concurrencysync.Poolsamber/cc-skills-golang@golang-safetysamber/cc-skills-golang@golang-databasesamber/cc-skills-golang@golang-observability