Profile-guided optimizations in Go.
When Go builds a binary, the compiler will optimize it by default (e.g. by inlining code). But here the compiler will make a “best effort guess” by using static heuristics based on (un)common paths in functions.
Starting from Go 1.21
, the compiler supports profile-guided optimizations (PGO) to better optimize built binaries by using collected CPU pprof profiles.
Providing profiles to the compiler gives it more information about how code behaves in a “real” production environment, and it can better optimize the built binary. For example, by more aggressively optimizing the most frequently used functions, or by more accurately selecting common cases.
Setting expectations
As of Go 1.21, benchmarks for a representative set of Go programs show that building with PGO improves performance by around 2-7%.
As of Go 1.22, benchmarks for a representative set of Go programs show that building with PGO improves performance by around 2-14%.
The Go team expects performance gains to keep increasing over time as more optimizations take advantage of PGO in future Go versions.
How to use PGO
- Add a profile named
default.pgo
in the main package directory.
- Use the
-pgo
flag to provide the path to a profile when using go build
.
Combining profiles
To use a more representative profile for PGO, it’s possible to combine multiple profiles:
Resources
Thanks for reading!
If you have ideas how to improve this post, let me know on GitHub.