Fundamentals 15 min read

Profile‑Guided Optimization (PGO) in Go and C++: Principles, Practices, and Performance Results

This article explains the principles of Profile‑Guided Optimization, outlines the three‑step workflow, demonstrates practical PGO implementations for Go and C++ with code examples and devirtualization techniques, and presents benchmark results showing significant latency reduction and throughput improvement.

DevOps
DevOps
DevOps
Profile‑Guided Optimization (PGO) in Go and C++: Principles, Practices, and Performance Results

Profile‑Guided Optimization (PGO), also known as feedback‑directed optimization, uses runtime profiling data to guide recompilation for better performance, and is applicable to many languages.

The article first explains the PGO principle, describing how profiling data is collected, and outlines the three typical steps: instrumented build to generate profile data, running the instrumented binary to collect the .prof file, and recompiling with the profile to produce an optimized binary.

For Go, PGO support started in Go 1.20 (disabled by default) and became enabled by default in Go 1.21. The author shows how to collect a CPU pprof file, merge multiple profiles, and build with go build -pgo=/pprof/main.pprof , discussing source‑stability and iterative robustness, and reports a 5 %‑7 % performance gain in a sidecar service.

For C++, the article demonstrates how virtual function calls can hinder performance and how devirtualization, including basic devirtualization with final and speculative devirtualization, can turn indirect calls into direct calls. Sample C++ code and the resulting assembly before and after optimization are presented.

Finally, a practical benchmark on an Envoy service shows that enabling PGO reduces average latency by 14 %‑18 % and increases QPS by roughly 15 %‑18 %.

performance optimizationcompilerGoCprofilingPGO
DevOps
Written by

DevOps

Share premium content and events on trends, applications, and practices in development efficiency, AI and related technologies. The IDCF International DevOps Coach Federation trains end‑to‑end development‑efficiency talent, linking high‑performance organizations and individuals to achieve excellence.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.