go-profiling-optimization
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 benchmarks, or needs to reduce p99 latency. SKIP for general Go idioms — use `go` skill.
What this skill does
<!-- cache:start -->
**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:**
- **Coding mode** — optimizing code. Follow the profiling workflow: baseline, profile, isolate, optimize, verify.
- **Review mode** — reviewing a PR for performance. Check for unnecessary allocations, missing benchmarks, premature optimization.
- **Audit mode** — auditing performance across a codebase. Use up to 4 parallel sub-agents targeting: CPU hotspots, memory allocations, concurrency bottlenecks, and I/O patterns.
> **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
- Optimizing without profiling data — you WILL optimize the wrong thing
- Premature micro-optimization (removing interfaces, using `unsafe`) without measurement
- Ignoring allocations in hot loops — allocations are often the #1 bottleneck
- Using `interface{}` / `any` in performance-critical paths without benchmarking the cost
- Forgetting `-count` flag — a single benchmark run has no statistical validity
- Profiling debug builds instead of release builds
- Using synthetic workloads for PGO instead of production profiles
- Calling `runtime.ReadMemStats()` frequently — it triggers a stop-the-world pause
## Benchmarking Reminders
- Use `b.Loop()` (Go 1.24+) instead of `for i := 0; i < b.N; i++` — prevents compiler
from optimizing away the benchmark target. For Go < 1.24, use a sink variable with `b.N`
- Use `b.ReportAllocs()` or `-benchmem` to track allocations per operation
- Use `-count=6` minimum for benchstat comparisons (more runs = better statistics)
- Use `b.StopTimer()`/`b.StartTimer()` to exclude setup from timing
- Use `benchstat old.bench new.bench` — check p-value < 0.05 for significance
## Further Reading
- `reference.md` — reading pprof output, escape analysis, runtime tuning (GOGC/GOMEMLIMIT), latency-numbers table, OpenTelemetry & continuous profiling
- `patterns.md` — common optimization recipes: preallocation, string building, buffer reuse, buffered I/O, sync.Pool, struct alignment, avoiding interface boxing
- `examples.md` — PGO workflow walk-through, execution-tracing recipes
## Cross-References
- → `go` skill (concurrency.md) for sync.Pool hot-path patterns and goroutine scheduling
- → `go` skill (errors.md) for error-path performance considerations
- → `go` skill (debugging.md) for diagnosing performance regressions
- → `go` skill (testing.md) for benchmark test patterns
<!-- cache:end -->
---
*Powered by [Gopher Guides](https://gopherguides.com) training materials.*
Related in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.