Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PGO (Profile-Guided Optimization)_ The Secret W...

Avatar for Hanji Chung Hanji Chung
August 03, 2023
220

PGO (Profile-Guided Optimization)_ The Secret Weapon for Accelerating Golang Programs

COSCUP 2023

Avatar for Hanji Chung

Hanji Chung

August 03, 2023
Tweet

Transcript

  1. About me • Freelancer, but open to work • 2+

    backend work experience • 0 golang work experience My LinkedIn
  2. Golang advantages • Flexibility • Easy to use • Concurrency

    • Garbage collection • Safety + Speed • The Cute Mascot
  3. Improving Golang programs performance? • Use goroutines • Parallelize CPU

    work • Make I/O operations asynchronous • Do not allocate memory in hot spots • Favor read-only locks • …
  4. But… • Some optimizations cannot be implemented by analyzing source

    code • Ex. many if/else conditional branches
  5. What is PGO? • Compiler optimization technique • Improve program

    runtime performance • Profile-directed feedback (PDF) • Feedback-directed optimization (FDO)
  6. The basic principles of PGO 1. Profiling is performed on

    the program to collect data about the program's runtime and generate a profiling file. 2. When compiling the program, enable the PGO option, and the compiler will optimize the program's performance based on the content in the .pgo file.
  7. In short… • Giving your compiler some context about what’s

    going on at runtime. • Let your compiler make some optimization.
  8. Collecting profiles 1. Build and release an initial binary (without

    PGO). 2. Collect profiles from production. 3. When it’s time to release an updated binary, build from the latest source and provide the production profile. 4. GOTO 2
  9. Collecting profiles Build and release an initial binary (without PGO).

    Collect profiles from production. When it’s time to release an updated binary, build from the latest source and provide the production profile.
  10. Collecting representative profiles from production • Add net/http/pprof to your

    application. • Fetch /debug/pprof/profile?seconds=30 from an arbitrary instance of your service. • Continuous profiling automatically, which could then be used as a source of profiles for PGO.
  11. Merging profiles • The pprof tool can merge multiple profiles

    like this: $ go tool pprof -proto a.pprof b.pprof > merged.pprof
  12. Source stability and refactoring • Changes within a hot function.

    • Renaming the function. • Moving the function to another package. • Large-scale refactor
  13. Building with PGO • Store a pprof CPU profile with

    filename default.pgo in the main package directory of the profiled binary. • Committing profiles directly in the source repository is recommended. • Before Go 1.21, the default is -pgo=off. PGO must be explicitly enabled. • You may directly pass a path to the profile to use.
  14. Performance of new code • New programs won’t receive PGO

    optimizations until a new profile reflecting the new code is collected.
  15. Q&A

  16. Can I use the same profile for different GOOS/GOARCH builds?

    • You may merge profiles of different GOOS/GOARCH builds.