Reduce Compilation Times C++
15 Sep 2024
[
c++
development
performance
]
Build system
Linkers
lld
much faster than the default linker ld
or gold
-fuse-ld=lld
mold
is even faster than lld
Compiler Caching
CCache
ln -s ccache /usr/local/bin/g++
- clang
-ftime-trace
flag creates a flame chart JSON file of each translation unit
- Open tracing output with
chrome://tracing
ClangBuildAnalyzer
parses and summarizes these files
- GCC
-ftime-report
makes the compiler print some statistics to stderr about the time consumed by each pass when it finishes.
Techniques
- Forward Declarations -
<iosfwd>
, dedicated headers with forward declarations
- Removing unused includes -
https://include-what-you-use.org/
, Graphviz
- Splitting Protocol and Implementation - Fundamental Theorem of Software Engineering, PIMPL
- Object Libraries
- Precompiled Headers - since CMake 3.16, headers that are often included bu not changed often, use profiling to determine headers to pre-compile
- Unity Build - since CMake 3.16
Main Takeaways
- Profile your build to determine where to focus your effort
- Forward declarations
- Remove unused includes
- Split Protocol from Implementation
- Merge executables
- Tune precompiled headers
- Unity build and reducing number of files
References