Why Builds Get Slow
A five-second build is a tool. A twenty-minute build is a tax: every engineer pays it many times a day, CI pays it on every push, and people start batching changes, skipping tests and avoiding refactors just to dodge it. This course is about lowering that tax for C and C++ projects, and about the infrastructure that stores what the builds produce.
Where the time goes
A C++ build spends its time in a few predictable places:
| Phase | What happens | What makes it slow |
|---|---|---|
| Preprocessing | #include text is pasted in, macros expand |
heavy headers included by every file |
| Parsing & templates | the compiler builds the AST and instantiates templates | the same headers parsed again in every translation unit (TU) |
| Code generation | optimisation and machine code | -O2/-O3, LTO, huge functions |
| Linking | objects and libraries combined | many symbols, debug info, LTO |
| Waiting | jobs blocked on other jobs | a long critical path, too few cores, a single serial step |
The first two rows dominate in most C++ code bases. One .cpp file of twenty lines can turn into tens of thousands of lines once its headers are pasted in. Here is the real count for a few standard headers with GCC 13:
for h in vector string map iostream regex; do
printf '%-10s %6d lines\n' "<$h>" "$(echo "#include <$h>" | g++ -std=c++20 -x c++ -E - | wc -l)"
done<vector> 26258 lines
<string> 31825 lines
<map> 25626 lines
<iostream> 43165 lines
<regex> 78111 lines
Including <regex> in 200 files means the compiler reads and parses roughly 15 million lines that are the same every time. Most techniques in this course attack that repetition.
The four levers
- Do less work per build. Precompiled headers, unity builds, C++20 modules, forward declarations and include hygiene shrink what each compile has to parse.
- Never do the same work twice. Compiler caches (ccache, sccache) and build-system caches return stored results for inputs they have seen before, across clean builds, branches and machines.
- Do the work in parallel. More local cores, distributed compilation (distcc, icecream, Incredibuild) and remote execution (Bazel's REAPI) spread compiles over many machines. The critical path and Amdahl's law set the limits.
- Don't rebuild what you can download. Artifact repositories (Artifactory, Nexus, GitHub Packages) store built libraries and releases so that other teams and later pipelines fetch binaries instead of rebuilding them.
Measure deterministically
Timings vary from run to run and from machine to machine, so this course never asks you to trust a stopwatch number printed in a lesson. Every real session measures something that doesn't change between runs:
- how many translation units Ninja compiled (
[3/7]lines,ninja -d explain); - how many preprocessed lines or headers one compile reads (
-E | wc -l,-H); - ccache's hit and miss counters (
ccache --print-stats); - the structure of a
-ftime-traceprofile (which kinds of events, how many); - object and archive sizes.
The speed-ups themselves come from small models (js run and python run blocks) that compute them from fixed inputs: Amdahl's law, critical paths, cache hit rates, queueing for remote execution and proxy repositories.
How this course fits the track
You should already be comfortable with CMake and Ninja (GNU Make & Ninja), and know what a package manager does (C/C++ Packages). Content-addressed caching and hermeticity were introduced in Build Fundamentals. After this course, Bazel takes caching and remote execution to the scale of whole monorepos.
| Part | You will learn |
|---|---|
| Measuring | clean vs incremental vs no-op builds, .ninja_log, critical paths, -ftime-trace, header cost |
| Faster compiles | PCH, unity builds, modules, forward declarations, LTO, linkers and debug info |
| Compiler caches | ccache in depth, sharing caches, sccache |
| Distributed builds | distcc, icecream, Incredibuild, remote execution |
| Artifact management | hosted/proxy/virtual repositories, promotion, retention, checksums |
| Mini Project | take a slow project and prove each improvement with numbers that don't drift |
Test yourself
In a typical C++ code base, what usually dominates compile time?
- Linking
- Parsing the same large headers again in every translation unit
- Writing object files to disk
- Running CMake
Why do the real sessions in this course print hit counts and TU counts instead of seconds?
- Seconds are not supported by Ninja
- Timings vary between runs and machines, while counts are deterministic
- Counts are always smaller
- The normaliser deletes numbers
Which lever does an artifact repository pull?
- Parallelism
- Fewer headers
- Downloading what was already built instead of rebuilding it
- Faster linking