Field Notes: Measure First, Keep No-op at Zero
Advice from teams that have spent years shaving minutes off C++ builds. The common thread: most build-speed work is wasted when it isn't driven by measurements, and the cheapest wins are usually hygiene, not new infrastructure.
1. Measure before optimising, and keep the numbers
- Record three numbers for every change to the build: clean-build command count (or time on a fixed CI machine), no-op command count, and the command count after touching your most-included header (Clean, Incremental & No-op).
- Use deterministic measures to prove a change (TU counts, header opens, preprocessed lines, cache hit rates) and timings on a quiet, fixed machine to size it. Laptop timings with a browser open are noise.
- Profile the top of the list. Sort
.ninja_logby duration, look at the slowest 20 TUs and the critical path; fix those before touching global settings. - Keep a build-performance dashboard: clean time, CI wall time, ccache hit rate per job, the slowest TUs. Regressions creep in one innocent
#includeat a time.
2. The no-op build must run zero commands
A no-op build that does work is a bug. The most common cause is a generator that rewrites its output every time. This session shows a version-header generator written the naive way and then the correct way:
mkdir -p gen/src && cd gen
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Gen CXX)
option(FIXED "write the header only when its content changes" OFF)
set(hdr ${CMAKE_BINARY_DIR}/version.hpp)
if(FIXED)
set(write ${CMAKE_COMMAND} -E copy_if_different version.hpp.tmp ${hdr})
else()
set(write ${CMAKE_COMMAND} -E copy version.hpp.tmp ${hdr})
endif()
add_custom_target(gen_version ALL
COMMAND sh ${CMAKE_SOURCE_DIR}/gen.sh version.hpp.tmp
COMMAND ${write}
BYPRODUCTS ${hdr})
add_executable(app src/main.cpp src/about.cpp src/net.cpp)
target_include_directories(app PRIVATE ${CMAKE_BINARY_DIR})
add_dependencies(app gen_version)
CM
printf 'printf "#define APP_VERSION 140\\n" > "$1"\n' > gen.sh # stands in for a real version generator
printf '#include "version.hpp"\nint about() { return APP_VERSION; }\n' > src/about.cpp
printf '#include "version.hpp"\nint net() { return APP_VERSION / 10; }\n' > src/net.cpp
printf 'int about(); int net();\nint main() { return about() + net() - 154; }\n' > src/main.cpp
for fixed in OFF ON; do
cmake -S . -B b-$fixed -G Ninja -DFIXED=$fixed > /dev/null && cmake --build b-$fixed > /dev/null && ./b-$fixed/app
echo "FIXED=$fixed, no-op build:"
cmake --build b-$fixed | sed -E 's#^(\[[0-9]+/[0-9]+\]) cd .*(copy[_a-z]*) .*# \1 regenerate version.hpp (\2)#; s#^(\[[0-9]+/[0-9]+\] [A-Za-z]+ [A-Za-z]+ [A-Za-z]+).*# \1 ...#'
doneFIXED=OFF, no-op build:
[1/4] regenerate version.hpp (copy)
[2/4] Building CXX object ...
[3/4] Building CXX object ...
[4/4] Linking CXX executable ...
FIXED=ON, no-op build:
[1/4] regenerate version.hpp (copy_if_different)
The naive version recompiles every file that includes version.hpp and relinks, on every build, forever. With copy_if_different Ninja still plans four steps ([1/4]), but after the generator runs it sees, thanks to BYPRODUCTS (which turns on restat), that version.hpp didn't change, and drops the other three: only the cheap generator step remains. Better still is a real add_custom_command(OUTPUT …) with DEPENDS, which runs nothing at all when the inputs are unchanged. Version stamps that must change per commit belong in a single tiny .cpp file, not in a header everyone includes.
Put the no-op check in CI: build, build again, and fail if the second build's output isn't ninja: no work to do.
3. Header hygiene beats hardware
- One "everything" header included by every file costs more than any other single problem. Split it; give each subsystem a small public header.
- Include in
.cppfiles, forward-declare in headers, and use Pimpl for heavy classes with stable interfaces (lesson). - Move rarely changing, heavy third-party headers (Boost, Qt, fmt, nlohmann/json) into a PCH, not into your own common header.
- Watch for headers that include implementation headers of libraries (
<boost/asio.hpp>in a public header pulls in hundreds of files). - Use
extern templatefor templates instantiated with the same arguments in many TUs, and explicitly instantiate them once. - Re-measure fan-out after each refactor with
ninja -t deps(Header Cost).
4. Pick defaults per workflow
| Workflow | Good defaults |
|---|---|
| Developer inner loop | Debug or RelWithDebInfo -O1, no LTO, lld/mold, -gsplit-dwarf, shared libraries, ccache, PCH |
| CI pull-request builds | ccache (read from main's cache), unity builds, one job without unity/PCH to catch missing includes |
| Release builds | LTO/ThinLTO, static libraries, full debug info stored separately, reproducible flags |
Make them presets (CMakePresets.json), so nobody has to remember flags.
5. Don't optimise the wrong layer
- If the build is path-bound, a bigger machine won't help; break the chain.
- If the cache hit rate is low, fix keys (
base_dir,compiler_check, unstable generated headers) before adding remote storage. - If configure takes a minute, profile CMake (
--profiling-format=google-trace) before touching compile flags: hundreds oftry_compilechecks andfind_packagecalls add up. - If tests dominate CI time, build speed isn't the problem; shard the tests.
Test yourself
A generator rewrites version.hpp on every build. What is the cheapest correct fix?
- Disable Ninja
- Write the file only when its content changes (copy_if_different), with BYPRODUCTS
- Use a unity build
- Add more cores
Where should a per-commit version stamp live?
- In a header included everywhere
- In one small .cpp file (or a function), so only it recompiles
- In the PCH
- In every CMakeLists.txt
The build is path-bound and the team proposes a 128-core build server. What do the notes suggest?
- Buy it
- Shorten the critical path first; more cores don't help a path-bound build
- Use -j1
- Disable ccache