NewsCrawler.org

Understanding Go: how profile-guided optimization works

This article continues a series on making Go code faster. The previous article used SIMD[1] instructions, through the simd/archsimd and simd packages, to make a dot product about 9 times faster, but the code became much harder to read. This article covers Profile-Guided Optimization (PGO)[2], which speeds up a program without rewriting it: you record a CPU profile[3] of where the program spends its time and pass it to the compiler, which optimizes the hot code harder.

The go command gives the compiler a trimmed version of the profile that keeps only call graph edges: which function calls which, and how often. Line numbers are stored as offsets from the start of each function, so edits higher up in a file do not break the match. The trimmed profile goes to every package being compiled, including the standard library. By default, -pgo is set to auto and uses a file named default.pgo in the main package's directory, while -pgo=off turns PGO off.

PGO runs three optimizations in a fixed order. Devirtualization[4] turns an interface call into a direct call, with a type check and the interface call kept as a fallback. Inlining copies the body of a called function into the caller, and at hot call sites the compiler accepts functions up to 25 times bigger than it does without a profile. Loop alignment adds padding so that hot loops start at the beginning of a CPU cache line, and it works only on x86 (amd64 and 386).

Read the original internals-for-interns.comDiscussion

Sources

Published: First seen: Читать по-русски

Open as a page