Shark Compiler Control by Sören Kewenig speeds up C++ compilation in Visual Studio 2010, 2008 and 2005 using multiple processor cores. It works similar to the /MP switch, but with less restrictions and more convenient control.
Visual Studio has built-in capabilities to speed up compilation using multiple cores. First of all it the maximum number of parallel project builds options:
When you are building a solution with several projects, independent projects will be built on separate cores. When you have many independent projects, it gives you the best speed up as many .cpp files can be compiled in parallel plus linking of different projects can be run in parallel. On the other hand, if you are building just one project (common with usual incremental code changes) this option doesn’t help at all. To speed up compilation of files within one project you can add Visual Studio /MP C++ compiler option in project properties:
“The /MP option causes the compiler to create one or more copies of itself, each in a separate process. These copies simultaneously compile the source files. Consequently, the total time to build the source files can be significantly reduced.” /MP is incompatible with several other options including /Gm that enables an incremental rebuild.
Shark Compiler Control also enables parallel compilation within a single project. After the installation, you can enable and configure it in the main Visual Studio menu Tools – Shark Compiler Control Configuration:
To test compilation speed up, I used a quad core machine with Windows 7 and Visual Studio 2008 SP1. As a test project I used cryptlib from Crypto++ Library – it consists of 125 .cpp files and doesn’t depend on external components.
With default settings this project compiles in 52 seconds. With the /MP option it compiles in 28 seconds. With Shark Compiler Control it compiles in 29 seconds. Almost 2x speed up from parallel compilation.
To test parallel projects compilation I created a solution with completely independent 4 copies of the cryptlib project. The Maximum number of parallel project builds parameter was set to 4. Solution rebuild with default settings took 62 seconds. With the /MP option – 76 seconds. With Shark Compiler Control – 70 seconds. In this case built-in parallel project compilation already improved performance by significant 3.35x and additional in-project parallelization led to overload.
To better understand how parallel compilation works we can use famous Process Monitor and its Process Tree tool. Process Tree shows when each process started, who started it and when the process ended all in combined timeline. For example, this is how default project rebuild looks like:
On this timeline we see a very short custom prebuild event involving cmd.exe, then precompiled headers compilation by first cl.exe, then 3 batches of other .cpp files compilation and finally linking. Note how everything is performed sequentially – no processes overlap (except devenv.exe and mspdbsrv.exe which are not interesting to us). It is not visible on this diagram, but each compilation and linking process is single threaded. Only one processor core is used and more cores don’t give any speed up in this case.
With the /MP option enabled, 3 batches of .cpp files compilation are now performed in parallel in 4 additional instances of cl.exe (giving 1.86x speed up):
Shark Compiler Control substitutes cl.exe with its own module that calls the original compiler renamed to cl_msvcc.exe. Each cl_msvcc.exe compiles only 3 files before exiting as specified in default Shark Compiler Control options. This results in much more cl_msvcc.exe instances, but only 4 of them run in parallel, according to Shark Compiler Control options used:
When rebuilding the test solution consisting of 4 independent projects, we see perfect parallelization of each project build step. All 4 prebuild tasks run in parallel, 4 precompiled header compilations run in parallel and other files compilation in each project run in parallel. Almost perfectly utilizing 4 available cores:
With the /MP option enabled, within each project 4 parallel compiler instances result in 16 parallel compilations overall, overloading 4 available cores:
Same overload with Shark Compiler Control:
With the /MP option and Shark Compiler Control you can manually specify number of processes run in parallel within each project. Plus for Shark Compiler Control you can specify number of files to compile in one compiler run. I experimented with different values, but didn’t get a noticeable performance improvement. It will be much better if these parameters were automatically and dynamically adjusted by the tools themselves, depending on current load and available resources.
Shark Compiler Control enables you to quickly turn on parallel in-project compilation for your whole solution and compatible with all compiler switches. The /MP option is built-in and lets you configure each project individually, for example, you can enable it only for projects that don’t compile in parallel on solution rebuild. Either way you can’t ignore the opportunity to significantly speed up your C++ compilation utilizing multiple cores.
Shark Compiler Control is currently free to use with Visual Studio 2010, 2008 and 2005. You can download it from the official website.