Discover more from TheYahya.com
A Deep Look Into Golang Profile-Guided Optimization (PGO)
Profile-Guided Optimization (PGO) is an optimization method which improves final compiled binary by using profile data hints and compiling the code based on those profiles.
There are several optimization mechanisms that compilers usually get into account when compiling your code to binary. Like dead code elimination, Register allocation, Constant folding or function inlining; you may split your code into smaller and smaller functions and different level of abstractions to ease up future changes and modifications, but from compiler point of view multiple calls to different functions may not be very optimize and sometimes compiler decides to inline your functions!
Although there’s a limit to these optimizations. Compiler can’t inline everything! this will result in bloating binary files, l-cache overhead and performance degradation. But with providing information about how the program is being run and which functions are being constantly called, the compiler can inline those hot functions and reduce function call and return overhead. It’s the same with register allocation and other methods.
How does it work?
From go version
v1.21, you only need to provide:
A pprof CPU profile, which can be collect with
Renaming it to
default.pgoand move it to same directory as
main.goor pass it to
Let’s take a look at the following code and see how it works…
Thanks for reading! Subscribe for free to receive new posts and support my work.