17. Profile-guided optimization
Date: 2023-02-15
Status
Accepted
Context
Profile-guided optimization (PGO), also known as feedback-directed optimization (FDO), is a compiler optimization technique that feeds information (a profile) from representative runs of the application back into to the compiler for the next build of the application, which uses that information to make more informed optimization decisions. For example, the compiler may decide to more aggressively inline functions which the profile indicates are called frequently.
In Go, the compiler uses CPU pprof profiles as the input profile, such as from runtime/pprof or net/http/pprof.
As of Go 1.20, benchmarks for a representative set of Go programs show that building with PGO improves performance by around 2-4%. We expect performance gains to generally increase over time as additional optimizations take advantage of PGO in future versions of Go.
Additional tutorials and docs
- Profile-guided optimization
- What is continuous profiling?
- What happens when you profile a continuous profiler?
Decision
We will use Pyroscope for continuous profiling.
We will use PGO for our application.
Pipeline
- Run application and connect continues profiling
- Use continues profiling by Pyroscope
- Load profile
- Merge profile for application
- Build docker image with new PGO profile
- Push to registry
- Deploy application
Consequences
We will get a little faster application.
The standard approach is to store a pprof CPU profile with filename default.pgo
in
the main package directory of the profiled binary, and build with go build -pgo=auto,
which will pick up default.pgo files automatically.
Example work with pgo
# Get profile
$> curl -s http://localhost:7071/debug/pprof/profile\?seconds\=30 > ./cpu.out
# Merging profiles
$> go tool pprof -proto a.pprof b.pprof > default.pgo
# Build
$> go build -o app -pgo=auto ./boundaries/api/api-gateway/cmd/api.go