How to Reduce Compile Time, and Linking Time for Visual C++ Projects (Native C++)

How do YOU reduce compile time, and linking time for Visual C++ projects (native C++)?

It may sound obvious to you, but we try to use forward declarations as much as possible, even if it requires to write out long namespace names the type(s) is/are in:

// Forward declaration stuff
namespace plotter { namespace logic { class Plotter; } }

// Real stuff
namespace plotter {
namespace samples {
class Window {
logic::Plotter * mPlotter;
// ...
};
}
}

It greatly reduces the time for compiling also on others compilers. Indeed it applies to all configurations :)

How to reduce release build time in visual studio unmanaged code?

Based on suggestions received in comments, I started by finding out compilation time taken by each file:

  1. Clean project to ensure all *.obj files are removed
  2. Build the project again
  3. Noticed the time stamp for each file and I found a file which was
    taking almost two hours to compile.

When I open the source file I see something terribly wrong in the code in contrast to my observation. It is a huge monster file of 27KLOC (Opps!, of course I didn't write this file).
There are 739 instances of a class created dynamically and assigned to an array. Each instance in turn dynamically creates some of its members as well. In shorts thousands of objects are being created in this file.

To ensure that this file is the culprit and VS studio is taking way too much optimizing this file. I disabled the optimization
in this file as proposed by @Predelnik in comments. Voilà! program compiles within couple of minutes now. This source code needs a serious re-factoring.

If someone is facing such problems, I would go as following -

  1. Enable Build-And-Run option and /MP flag. As discussed Here. If there is some problem with the code the parallel projects and file compilations would not help.

  2. Find out if any source file is the culprit as above. I believe the link I found Here is a way to calculate the build time not compile time of each file.

product to decrease c++ compile time?

If it has to be a product, look at Xoreax IncrediBuild, which distributes the build to machines on the network.

Other than that:

  • solid build machines. RAM as it fits, use fast separate disks.
  • Splitting into separate projects (DLLs, Libraries). They can build in parallel, too
    (use dual quad/core, and is easily bottlenecked by disk)
  • Intelligent use of headers, including precompiled headers. That's not easy, and often there are other stakeholders.
    PIMPL helps, too.

How to speed up Compile Time of my CMake enabled C++ Project?

Here's what I had good results with using CMake and Visual Studio or GNU toolchains:

  1. Exchange GNU make with Ninja. It's faster, makes use of all available CPU cores automatically and has a good dependency management. Just be aware of

    a.) You need to setup the target dependencies in CMake correctly. If you get to a point where the build has a dependency to another artifact, it has to wait until those are compiled (synchronization points).

    $ time -p cmake -G "Ninja" ..
    -- The CXX compiler identification is GNU 4.8.1
    ...
    real 11.06
    user 0.00
    sys 0.00

    $ time -p ninja
    ...
    [202/202] Linking CXX executable CMakeTest.exe
    real 40.31
    user 0.01
    sys 0.01

    b.) Linking is always such a synchronization point. So you can make more use of CMake's Object Libraries to reduce those, but it makes your CMake code a little bit uglier.

    $ time -p ninja
    ...
    [102/102] Linking CXX executable CMakeTest.exe
    real 27.62
    user 0.00
    sys 0.04
  2. Split less frequently changed or stable code parts into separate CMake projects and use CMake's ExternalProject_Add() or - if you e.g. switch to binary delivery of some libraries - find_library().

  3. Think of a different set of compiler/linker options for your daily work (but only if you also have some test time/experience with the final release build options).

    a.) Skip the optimization parts

    b.) Try incremental linking

  4. If you often do changes to the CMake code itself, think about rebuilding CMake from sources optimized for your machine's architecture. CMake's officially distributed binaries are just a compromise to work on every possible CPU architecture.

    When I use MinGW64/MSYS to rebuild CMake 3.5.2 with e.g.

    cmake -DCMAKE_BUILD_TYPE:STRING="Release"
    -DCMAKE_CXX_FLAGS:STRING="-march=native -m64 -Ofast -flto"
    -DCMAKE_EXE_LINKER_FLAGS:STRING="-Wl,--allow-multiple-definition"
    -G "MSYS Makefiles" ..

    I can accelerate the first part:

    $ time -p [...]/MSYS64/bin/cmake.exe -G "Ninja" ..
    real 6.46
    user 0.03
    sys 0.01
  5. If your file I/O is very slow and since CMake works with dedicated binary output directories, make use of a RAM disk. If you still use a hard drive, consider switching to a solid state disk.

  6. Depending of your final output file, exchange the GNU standard linker with the Gold Linker. Even faster than Gold Linker is lld from the LLVM project. You have to check whether it supports already the needed features on your platform.

  7. Use Clang/c2 instead of Visual C++ compiler. For the Visual C++ compiler performance recommendations are provided from the Visual C++ team, see https://blogs.msdn.microsoft.com/vcblog/2016/10/26/recommendations-to-speed-c-builds-in-visual-studio/

  8. Increadibuild can boost the compilation time.

References

  • CMake: How to setup Source, Library and CMakeLists.txt dependencies?
  • Replacing ld with gold - any experience?
  • Is the lld linker a drop-in replacement for ld and gold?

How to improve link performance for a large C++ application in VS2005

If you're using the /GL flag to enable Whole Program Optimization (WPO) or the /LTCG flag to enable Link Time Code Generation, turning them off will improve link times significantly, at the expense of some optimizations.

Also, if you're using the /Z7 flag to put debug symbols in the .obj files, your static libraries are probably huge. Using /Zi to create separate .pdb files might help if it prevents the linker from reading all of the debug symbols from disk. I'm not sure if it actually does help because I have not benchmarked it.

I want to reduce my VS.NET project's compile time - what are your ideas for how to do this?

I'm surprised that 60k lines of code take 2 minutes to compile. I have an application that is 500,000 lines of code, and it only takes about a minute and a half. Make sure you are not doing full rebuilds each time, and make sure you are not cleaning the solution between builds. A normal build should perform an incremental build, only recompiling code that has changed since the last build (along with anything affected by that change.)

Perhaps some other factors might include heavy use of large resources (images?), broad-sweeping changes in the lowest level libraries (i.e. those used by everything else), etc. Generally speaking, on a relatively modern machine, compiling 60,000 lines of C# code should take less than a minute on average, unless you are rebuilding the entire solution.



Related Topics



Leave a reply



Submit