Build Performance and Caching for C++

Where C++ build time actually goes and how to get it back — measurement first, then ccache and sccache, distributed compilation, include-graph reduction, and the link step that refuses to parallelize.

Overview

How build performance work goes wrong

Someone reads that unity builds or precompiled headers are fast, enables them globally, breaks incremental builds for the whole team, and cannot say whether it helped because there was never a baseline. The discipline is the same as any performance work: measure, find the dominant cost, attack that, re-measure.

In practice that is a loop of five moves.

  • Take a baseline.
  • Measure where the time goes, with -ftime-trace and ninja -d stats.
  • Form a hypothesis about the dominant cost.
  • Make one targeted change.
  • Re-measure. If the numbers do not move as predicted, go back to step 3 with a new hypothesis.

Key concepts

  • -ftime-trace
  • ClangBuildAnalyzer
  • ccache direct mode
  • CCACHE_BASEDIR and hash_dir
  • depend mode
  • Unity build
  • ThinLTO versus full LTO
  • Link as the serial tail

Interview questions

  • Your clean build takes 45 minutes. Walk me through how you would find out where the time is going and what you would do first.
  • Explain how ccache decides on a hit, and name the things that defeat it along with the fixes.
  • Compare distcc, icecream, sccache distributed mode, and Bazel remote execution. What actually distinguishes them?
  • When would you use unity builds and precompiled headers, and what does each actually cost?
  • How do you choose -j, and why is the link phase special?

Sign up free to read the full topic

More DevOps topics

Start free on Cariara · Coding problems · System design studio