/performant-golang:go-perf-pgofrom performant-golang
Guides Go Profile-Guided Optimization at depth — a representative production CPU profile (not a microbenchmark) committed as default.pgo, auto-detected by go build (GA Go 1.21), for reproducible builds; what PGO buys (hot-call inlining + interface devirtualization); the 2-14% payoff; refreshing as code drifts; merging profiles. Fires on "set up PGO", "profile-guided optimization", "default.pgo", "make the compiler optimize hot paths", "is PGO worth it". Routes inlining→go-perf-inlining, devirtualization→go-perf-compiler-intrinsics, collection→go-perf-pprof-profiling.
Ships in performant-golang. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
The headline — "feed a real CPU profile back into the compiler; commit default.pgo" — belongs to go-performance §6; read it first. This skill is its deep peer for PGO: where the profile must come from and why, the default.pgo lifecycle, what PGO actually does to hot code, the honest payoff, and the iteration/merge mechanics. The two optimizations PGO unlocks route out — inlining depth to go-perf-inlining, devirtualization to go-perf-compiler-intrinsics — because PGO does not invent them, it just applies them more aggressively where a profile proves a call site is hot.
All Go below builds clean under gofmt, go vet, and go test on Go 1.26.
"Starting in Go 1.20, the Go compiler supports profile-guided optimization (PGO)" (PGO guide), but 1.20 was a preview. PGO is GA from Go 1.21: "PGO support in Go 1.21 is ready for general production use!" (PGO blog). In the 1.21 release notes, "Profile-guide optimization (PGO) … is now ready for general use," and "The -pgo build flag now defaults to -pgo=auto" (Go 1.21).
PGO "enables additional optimizations on code identified as hot by profiles of production workloads" (Go 1.21). It is not a new optimization — it is evidence that tells the compiler which call sites are worth spending its existing budgets on. Gate any PGO idiom on the module's go directive being ≥ 1.21 (see go-version-feature-map).
default.pgo — The One Convention That Matters"The standard approach … is to store a pprof CPU profile with filename default.pgo in the main package directory of the profiled binary. By default, go build will detect default.pgo files automatically and enable PGO" (PGO guide). "The Go toolchain will automatically enable PGO when it finds a profile named default.pgo in the main package directory" (PGO blog).
myapp/
main.go
default.pgo <- CPU profile, here, named exactly this
go build ./... # PGO auto-enabled; no -pgo flag, no code change
Three consequences:
profile endpoint produces (§4).go build -pgo=off disables it; go build -pgo=/path/to/foo.pprof points at a specific file. The default is -pgo=auto (Go 1.21).PGO is not magic; it sharpens two existing optimizations using the profile's hot-site data.
Inlining — more aggressive on proven-hot calls. "The compiler may decide to more aggressively inline functions which the profile indicates are called frequently" (PGO guide). Without a profile the compiler inlines by a static cost budget (~80 nodes); with one, it spends extra budget on the call sites the profile says dominate. The mechanics of that budget, what blocks inlining, and reading -gcflags=-m → go-perf-inlining.
Devirtualization — hot interface calls become direct calls. "PGO builds can now devirtualize some interface method calls, adding a concrete call to the most common callee. This enables further optimization, such as inlining the callee" (Go 1.21). An interface call is an indirect jump the compiler normally cannot inline through; when the profile shows one concrete type dominates a call site, PGO emits a direct call to it (guarded, falling back to the interface call for other types) — and a direct call can then be inlined. The call-overhead/dispatch model → go-perf-compiler-intrinsics.
The takeaway for this skill: you do not write any code to get these. You supply a representative profile, and the compiler routes its budget to where the profile says it pays.
This is the single decision that makes or breaks PGO. "For best results, it is important that profiles are representative of actual behavior in the application's production environment. Using an unrepresentative profile is likely to result in a binary with little to no improvement in production. … collecting profiles directly from the production environment is recommended, and is the primary method that Go's PGO is designed for" (PGO guide).
Why production, not a microbenchmark: PGO only optimizes the call sites the profile flags as hot. A microbenchmark exercises one tiny path under cache-hot, GC-quiet, single-input conditions (see go-perf-methodology §5) — so it flags the wrong sites hot, and the compiler spends its extra budget on code production barely runs. A profile from real traffic flags the sites that actually dominate.
Profiles "from runtime/pprof and net/http/pprof can be used directly as the compiler input" (PGO guide) — the CPU profile you already collect is the PGO input. The how of collecting safely (endpoint exposure, sampling window, labels) lives in go-perf-pprof-profiling; the brief version:
import _ "net/http/pprof" // registers /debug/pprof/ on the default mux
// long-running server: go func() { log.Println(http.ListenAndServe("localhost:6060", nil)) }()
# pull ~30s of CPU profile from a representative production instance
curl -o cpu.pprof "http://localhost:6060/debug/pprof/profile?seconds=30"
cp cpu.pprof myapp/default.pgo
Or in-process with runtime/pprof.StartCPUProfile(w) / StopCPUProfile() (runtime/pprof). Whether the profile is representative enough to act on is a methodology judgment → go-perf-methodology.
The whole first-time setup, end to end:
# 1. capture a representative production CPU profile (~30s of real traffic)
curl -o cpu.pprof "http://prod-instance:6060/debug/pprof/profile?seconds=30"
# 2. drop it in the main package under the exact name
cp cpu.pprof ./cmd/server/default.pgo
# 3. commit it as a versioned build input
git add ./cmd/server/default.pgo && git commit -m "Add CPU profile for PGO"
# 4. build — PGO is auto-enabled, no flag, no code change
go build ./cmd/server
From here it is maintenance: re-pull and re-commit periodically (§6).
default.pgo — Reproducible BuildsThe profile is a build input, so it lives in source control next to the code it optimizes. "Committing profiles directly in the source repository is recommended as profiles are an input to the build important for reproducible (and performant!) builds" (PGO guide). "Storing profiles alongside your source code ensures that users automatically have access to the profile simply by fetching the repository … and that builds remain reproducible" (PGO blog).
Concretely: a default.pgo in .gitignore, or generated only in CI, means two checkouts of the same commit produce different binaries — a reproducibility break, and contributors who go get your module silently lose the optimization. Commit the file. It is typically a few hundred KB; treat it like any other versioned build input.
Refresh as code drifts. A profile captures today's hot paths; refactors move them. "Degradation will slowly accumulate over time since code is rarely refactored back to its old form, so it is important to collect new profiles regularly to limit source skew from production" (PGO guide). Wire profile refresh into a periodic job, not a one-time setup.
The chicken-and-egg worry is unfounded. You might fear a feedback loop: you build with PGO, profile that binary, rebuild with the new profile, and oscillate. Go is designed against this. "Go PGO is generally robust to skew between the profiled version of an application and the version building with the profile, as well as to building with profiles collected from already-optimized binaries" (PGO guide). The property is named iterative stability — "the prevention of cycles of variable performance in successive PGO builds (e.g., build #1 is fast, build #2 is slow, build #3 is fast, etc)" (PGO guide). So profiling a PGO-built production binary and feeding that back is fine — builds converge, they do not oscillate.
Merging multiple profiles. Production is many instances and workload shapes; combine their profiles rather than betting on one. "The pprof tool can merge multiple profiles" (PGO guide):
go tool pprof -proto a.pprof b.pprof c.pprof > default.pgo
This produces one representative default.pgo from several captures — useful when one instance over-represents a single endpoint.
PGO is a free, modest win — not an algorithmic one. "As of Go 1.22, benchmarks for a representative set of Go programs show that building with PGO improves performance by around 2-14%" (PGO guide); the 1.21 announcement measured "between 2% and 7% CPU usage improvements from enabling PGO" (PGO blog), typically a few percent. Frame expectations accordingly:
go-perf-methodology §6).go-perf-benchmarking-statistics). A ~ means this binary didn't benefit measurably; don't claim a number you didn't measure.A few facts that change how you reason about PGO at the program level:
linux/arm64 binary may be used in a windows/amd64 build" (PGO guide). One default.pgo serves every cross-compile target..gitignore default.pgo. It's a build input; omitting it breaks reproducibility and denies go get users the profile (PGO blog).default.pgo, or place it outside the main package — auto-detection keys on exactly that (PGO guide).go-performance §6–7).This plugin (go-perf-* depth):
go-perf-inlining — the inlining budget, mid-stack inlining, what blocks it, reading -gcflags=-m; PGO makes this more aggressive on hot sites.go-perf-compiler-intrinsics — interface call dispatch and the call-overhead model behind PGO devirtualization.go-perf-pprof-profiling — collecting the CPU profile safely (net/http/pprof exposure, sampling, labels) that becomes default.pgo.go-perf-methodology — whether a profile is representative enough to act on, and where PGO sits in the order of operations (last, modest).go-perf-benchmarking-statistics — proving the PGO build is actually faster with benchstat, n≥10.Parent / sibling marketplace (go-* correctness):
go-performance — the measure-first overview and the headline PGO paragraph (§6) this skill deepens.go-version-feature-map — version floors: PGO GA 1.21, -pgo=auto default.PGO anti-patterns in LLM-generated Go, each with wrong/right contrast and citations:
Source provenance for every claim in this skill:
What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
npx claudepluginhub ctoth/golang-skills-plugin --plugin performant-golang34 skills in this plugin
/go-perf-allocator-internalsWhat a Go heap allocation costs — the ~70 size classes and rounding waste (unsafe.Sizeof vs real slot), the tiny allocator packing sub-16B pointer-free objects, the lock-free per-P mcache → mcentral → mheap hierarchy, and scannable vs noscan spans (pointer-free objects cut GC cost; "1 alloc/op" hides different costs). Fires on "still slow after removing allocs", "reduce GC pressure", "size class", "how does Go allocate". Routes to go-perf-escape-analysis, go-perf-sync-pool, go-perf-gc-tuning, go-perf-struct-layout./go-perf-atomics-vs-locksGuides the atomics-vs-locks performance tradeoff in Go — typed sync/atomic ops (atomic.Int64/Pointer/Bool, 1.19) are a single CAS-style instruction, cheaper than a mutex uncontended but still cache-line-bouncing when contended; sync.Mutex's fast path is itself a CAS that only parks the goroutine when contended; RWMutex wins only for long read sections and is slower (scales worse) than a plain Mutex for short ones; the atomic.Pointer copy-on-write config swap; when lock-free isn't worth the correctness risk. Fires on "atomic vs mutex", "is RWMutex faster", "lock-free counter", "reduce locking overhead", "atomic.Pointer config". Routes copylock/atomic-API correctness to go-sync-primitives, memory ordering to go-race-and-memory-model, measuring to go-perf-block-mutex-profiles./go-perf-audience-and-tradeoffsGuides framing a Go performance decision by audience — the same change is right for one and wrong for another: the library author who can't profile callers and must not pessimize the common case (offer AppendXxx/[]byte forms, no unsafe in the API), the app developer who can profile and optimizes the proven hot path, the SRE who needs the code diagnosable (pprof labels, metrics, GOMEMLIMIT), the next reader who pays for clever micro-opts; the clarity-vs-speed through-line. Fires on "should a library do this", "is this worth optimizing for my users", "make this diagnosable", "premature optimization vs pessimization", "who am I optimizing for". Routes the policy root to go-idiomatic-discipline, measure-first to go-perf-methodology./go-perf-benchmarking-statisticsGuides trustworthy Go benchmark measurement at the command level — running each side with -count≥10 into old.txt/new.txt and feeding both to benchstat, reading its ± confidence interval, p-value (Mann–Whitney U), geomean, and the "~" no-significant-difference result, choosing the right unit (b.SetBytes for MB/s, b.ReportMetric for custom throughput), grouping a matrix with -col/-row/-filter, building a reproducible environment (perflock's 90% governor + serialization, frequency scaling. Auto-invokes when comparing benchmark runs, running benchstat, writing a perf A/B, or stabilizing a bench machine, and on "is this benchmark result real", "compare these benchmarks", "did this actually get faster", "my benchmark is noisy", "set up a perf regression test". Routes benchmark loop mechanics (for b.Loop, the sink/DCE trap, ResetTimer, RunParallel) to go-testing-advanced and the why-one-run-lies concept to go-perf-methodology./go-perf-block-mutex-profilesGuides Go contention diagnosis at depth — the block, mutex, and goroutine profiles, why block/mutex are OFF by default and read empty until runtime.SetBlockProfileRate (nanoseconds, 1=everything) / runtime.SetMutexProfileFraction (1/n sampled) are set, the overhead tradeoff, reading delay-vs-contention in pprof, and the goroutine profile for leak and pile-up detection. Auto-invokes on "why is this contended", "diagnose lock contention", "goroutine leak", "my mutex is slow", "profile blocking", "goroutines are piling up". Routes the fix to go-perf-contention-and-sharding / go-perf-atomics-vs-locks, sync correctness to go-sync-primitives, CPU/heap to go-perf-pprof-profiling./go-perf-bounds-check-eliminationGuides bounds-check elimination (BCE) in Go — the compiler checks every slice/array index and the SSA pass removes the ones it proves redundant. Covers seeing surviving checks with go build -gcflags="-d=ssa/check_bce/debug=1", prover-friendly idioms (the _ = b[n-1] / b = b[:n] hint, range loops, constant vs variable offsets), the inlining link, and why -B is a footgun not a fix. Fires on "eliminate bounds checks", "why is this loop slow", "bounds check in hot loop", "optimize this numeric loop". Routes should-I to go-perf-methodology, slices to go-perf-slices.