profile
- clang -ftime-trace would output one json file, that can generate flame graph
- gcc -ftime-report would output the phase time and percent
only Ninja
Ninja would output .ninja_log, use ninjatracing tool to convert the log files to Chrome (about:tracing), but actually it’s not very useful.
Clang + Ninja
cmake -G Ninja -DCMAKE_CXX_FLAGS="-ftime-trace" ..
Clang would output the compile time detail.
ninjatracing -e $BUILD_DIR/.ninja_log > trace.jsonwould report more detail of compile time
Clang + Clang Build Analyzer
ClangBuildAnalyzer is very useful tool with Clang + -ftime-trace. It can directly output
Time summary: parsing (frontend) : time Codegen & opts (backend) Files that took longest to parse (compiler backend) … Files that took longest to codegen (compiler backend) … Templates that took longest to instantiate: … Template sets that took longest to instantiate: … Functions that took longest to compile: … Function sets that took longest to compile / optimize: … Expensive headers: …
speed up
do not change the code
- faster linker lld faster than gnu bsd linker
- PCH(pre-compiled header)
target_precompile_headers(<target> PUBLIC <headers>)add_library(mylib_pch INTERFACE) add_library(my_lib::pch ALIAS mylib_pch) target_precompile_headers(mylib_pch INTERFACE # C++ headers <algorithm> <chrono> <functional> # third party libraries <Eigen/Core> # your own library headers <mylib/common.h> <mylib/Mesh.h> <mylib/logger.h> ) target_link_libraries(mylib PRIVATE mylib::pch) target_link_libraries(myexecutable PRIVATE mylib::pch) - cache compile result
- ccache (macos or linux)
find_program(CCACHE_PROGRAM ccache) if (CCACHE_PROGRAM) set(CMAKE_C_COMPILER_LAUNCHER ${CCACHE_PROGRAM}) set(CMAKE_CXX_COMPILER_LAUNCHER ${CCACHE_PROGRAM}) endif() - sccache(window)
- ccache (macos or linux)
- unity builds
cmake -DCMAKE_UNITY_BUILD=ONwhen cmake 3.16+ - LTO (link time optimization)
cmake -DLLVM_ENABLE_LTO=Thinfor clang - PGO(profile guided optimization)
build clang with cmake -DLLVM_BUILD_INSTRUMENTED=IR use this to train the compiler - we build some application - generate a profraw file merge all profraw files with llvm-prodata feed output to clang cmake with -DLLVM_PROFDATA_FILE=<path> combine with LTO for best results - Post link optimization
- LLVM-BOLT
- LLVM-Propeller
Grab bag
- -fvisibility=hidden
- -fexperimental-new-pass-manager
- distcc
- LTO on your code
- -ftime-trace
bloaty a size profiler for binaries
change the code
- split one large file into many small file , so that is benefit from parallel compiling
- PIMPL
- fwd class instead of include header
- explicit template instantiation