Comparing C/C++ unity build with regular build on a large codebase (2024)
12 hours ago
- #Build Systems
- #Performance
- #C++ Compilation
- Unity build merges multiple translation units into a single compilation unit using #include directives, potentially speeding up full rebuilds.
- Inkscape, a large C++ codebase with ~453k lines of C++ code across 1317 files, was used to compare regular builds against unity builds.
- Regular CMake build took ~35 minutes, while a custom non-unity script build took ~27 minutes, and a unity build script reduced time to ~3 minutes (9–12x speedup).
- Unity builds created one merged file per library (e.g., all.cpp) with preprocessing expanding to over 1 million lines, but reduced compiler calls significantly.
- Measured compilation speeds showed C files compiled faster per line than C++ files, attributed to less generated code and fewer templates.
- Incremental builds with tools like ninja or make -j can be faster (seconds to minutes) for small changes, while unity builds require full rebuilds (~2+ minutes).
- Optimization levels (-O0, -O2, -O3) affected build times: unity build with -O0 was fastest (3 min), while -O2/-O3 increased to ~6 minutes.
- Experiment concluded unity builds offer significant full rebuild speedups but aren't ideal for incremental development due to lack of partial compilation.