TheYahya.com

TheYahya.com

Share this post

TheYahya.com
TheYahya.com
A Deep Look Into Golang Profile-Guided Optimization (PGO)
Copy link
Facebook
Email
Notes
More

A Deep Look Into Golang Profile-Guided Optimization (PGO)

Yahya's avatar
Yahya
Aug 25, 2023
1

Share this post

TheYahya.com
TheYahya.com
A Deep Look Into Golang Profile-Guided Optimization (PGO)
Copy link
Facebook
Email
Notes
More
Share

Intro

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:

  1. A pprof CPU profile, which can be collect with runtime/pprof or net/http/pprof

  2. Renaming it to default.pgo and move it to same directory as main.go or pass it to pgo flag in go build command

Let’s take a look at the following code and see how it works…

Continue reading

Thanks for reading! Subscribe for free to receive new posts and support my work.

1

Share this post

TheYahya.com
TheYahya.com
A Deep Look Into Golang Profile-Guided Optimization (PGO)
Copy link
Facebook
Email
Notes
More
Share

Ready for more?

© 2025 Yahya SayadArbabi
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture

Share

Copy link
Facebook
Email
Notes
More