Go

What is Profile-Guided Optimization (PGO) in Go and how do you use it in production?

Question 438HardGo 1.22 to 1.25

PGO became generally available in Go 1.21. The compiler uses a CPU profile from representative production load to make better decisions: inlining hot call sites more aggressively, and devirtualizing hot interface or function-value calls into direct calls guarded by a type check. Typical gains are 2 to 14% CPU.

# 1. collect a CPU profile from prod (net/http/pprof)
curl -o cpu.pprof "http://svc:6060/debug/pprof/profile?seconds=30"

# 2. place it as default.pgo in the main package directory
mv cpu.pprof ./cmd/server/default.pgo

# 3. build: -pgo=auto is the default, picks up default.pgo
go build ./cmd/server
go build -pgo=off ./cmd/server          # disable
go build -pgo=/path/merged.pprof ./...  # explicit
go tool pprof -proto a.pprof b.pprof > merged.pprof   # merge profiles

What interviewers look for:

  • Commit default.pgo so that builds stay reproducible.
  • PGO tolerates stale profiles. Samples are matched by function name and line offset within the function, so edits elsewhere in a file do not invalidate them, and refactors degrade the benefit gradually rather than breaking the build.
  • Building with a profile from the PGO-optimized binary is fine and converges.
  • Expect larger binaries and longer builds.
  • Profile representative traffic, not a benchmark in a loop.
  • The profile applies to the whole build, dependencies included.

Pairs well with: GOAMD64=v3, and checking inlining decisions with -gcflags=-m.

More on Modules, Packages & Tooling

All 36 Modules, Packages & Tooling questions