Improving compilation performance in C++.
My project is slow to build
Often we develop our projects without worrying about standards or optimization issues, ehen the project is small this is not a problem, but if the project is growing we start to have compile time problem.
So what can we do to improve the project build time. Below some of the most critical points:
- Get a faster computer (of course);
- Only if needed: Forward Declarations;
- Add guard conditions;
- Split the build into pieces;
- Use parallelism and precompiled headers;
- Use shared libraries.
This has to do with the physical structure of C++ and how the compiler works, this is very important in large projects. I will detail each point quoted and then we will test with a project.
1. Get a faster computer
This is quite obvious, a software developer primarily working with desktop application needs to have great hardware. If you do not have one, buy one.
2. Only if needed: Forward Declarations
In C++, when anything in a header file changes, all code that includes that header either directly or indirectly must be recompiled. So it is important to minimize these dependencies. How?
- Do not include “#include xxx.h” unnecessarily in the header class;
- Avoid unnecessary membership.
See the example below in the unnecessary class:
3. Add guard conditions
It is very important to add the guards to the files and also add #pragma once to avoid recompiling to that file. See the class file definition example:
See the class implementation example:
4. Split the build into pieces
Split the build into pieces, create libraries that can be reused and shared with other projects. For example, create a library of generic methods, a library that communicates with the database, another library that sends data, and receives data from a server.
See the solution diagram below:
5. Use parallelism and precompiled headers
In the case of Unix environment we use the command make (maintain program dependencies) to build the project, by default make does not use parallelism, so we have to enable it. To know how many cores my processor uses the lscpu command in FreeBSD use sysctl hw.model hw.machine hw.ncpu, in my case I have 4.
So to build my project I will use the command make -j4. There are other build system like ninja or bjam, I like to use ninja with cmake for small and medium projects.
Use shared libraries
Use whenever possible shared library on Unix/Linux systems are the .so files on windows .dll and on Mac .dylib. Shared libraries reduce the amount of code that is duplicated in each program that makes use of the library, keeping the binaries small. It also allows you to replace the shared object with one that is functionally equivalent, but may have added performance benefits without needing to recompile the program that makes use of it. Shared libraries will, however have a small additional cost for the execution of the functions as well as a run-time loading cost as all the symbols in the library need to be connected to the things they use. Additionally, shared libraries can be loaded into an application at run-time, which is the general mechanism for implementing binary plug-in systems.
I made the modifications I mentioned above in a library that I use for generic methods and the speed gain for compiling was 20/30% for fast. Lets test:
Install clang and ccache, cmake and ninja on your system. Now do the commands below:
The ninja enables by default the parallelism in the case of make you need to pass -j with number of cores of your processor.
Let’s calculate the build time:
Check the file on my test:
It takes 18 seconds to compile! It is! With some care you make your build faster and improve your productivity.
Programs that were used: