Go 1.21 PGO: Feed the Compiler a Production Profile, Not a Guess
Go 1.21 made profile-guided optimization generally available. When a Go module contains a suitable default profile, the Go tool can use it to optimize the main package. The benefit is workload-dependent: the compiler can spend effort improving code that profiles show to be hot rather than optimizing every path as if it were equally important.
The practical loop is straightforward: collect a CPU profile from representative production traffic, review that it contains no sensitive data, place the approved profile in the main package directory, build, and compare the resulting binary against the same source without the profile. Profile data is operational material. Treat it with the same access and retention care as other production diagnostics.
Do not use a short-lived synthetic benchmark as the sole profile source. It can overemphasize one code path and produce an optimization that helps the benchmark but not the service. Use profiles from healthy, representative traffic, then test the exact workload that matters: throughput, tail latency, CPU use, and memory. Keep the non-PGO build available for comparison and rollback.
PGO is not a substitute for algorithmic improvements. If the profile shows repeated database calls or expensive serialization, optimizing machine code around the hot function may not be the highest-value change. Read the profile first, fix the dominant cost, and then see whether PGO offers an additional gain.
The Go 1.21 team reported improvements across a representative benchmark set, but results vary by program. The Go 1.21 release notes and PGO user guide explain how the tool discovers and applies profiles.