Boost application performance with profile guided optimization Michael Pratt, Google

Learn how Profile Guided Optimization (PGO) can boost your Go application's performance by 2-15% using production runtime data to inform compiler optimizations.

Key takeaways
  • Profile Guided Optimization (PGO) is a compiler optimization technique that uses runtime behavior data to improve application performance, typically achieving 2-15% CPU improvements

  • Key optimizations PGO enables:

    • Function inlining decisions
    • Escape analysis for better stack vs heap allocation
    • Constant propagation and folding
    • Focus optimization efforts on hot code paths
  • Best practices for using PGO:

    • Collect profiles from real production environment rather than benchmarks
    • Keep profiles alongside source code in repository
    • Regularly update profiles as application evolves
    • Profile for at least 30 seconds to get representative data
  • PGO is designed to be:

    • Source-stable (works with small code changes)
    • Iteratively stable (can profile PGO-optimized binaries)
    • Safe against profile mismatches (won’t degrade performance)
    • Easy to use with minimal configuration
  • Implementation details:

    • Uses pprof for profile collection and analysis
    • Profiles should be named default.pgo in package directory
    • Works automatically with Go build once profile is in place
    • Can be enabled via -pgo flag for explicit profile path
  • Most impactful optimizations come from:

    • Moving heap allocations to stack
    • Optimizing hot functions identified in profile
    • Improving performance of dependencies and standard library code
    • Focusing on CPU-bound workloads
  • Practical considerations:

    • Typically reduces container CPU time and associated costs
    • Binary size may increase slightly from inlining
    • Works best with continuous profiling in production
    • Effects vary by application (2-15% improvement range)