EN VI

C++ - Reducing build times on using boost graph library?

2024-03-10 18:00:08
How to C++ - Reducing build times on using boost graph library

I use the following boost libraries:

#include <boost/property_tree/ptree.hpp>

in my user file

gcs.h

and

#include <boost/graph/adjacency_list.hpp>

in my user file

boostgraph.h

Then, I had #included gcs.h and boostgraph.h in a bunch of my other .cpp files. On running Build Insight in Visual Studio, the IDE indicated that a large amount of my build time was being spent in parsing ptree.hpp and adjacency_list.hpp due to their inclusion in multiple translation units. To resolve this, atleast for ptree.hpp, I refactored my code so that my gcs.h file only does:

#include <boost/property_tree/ptree_fwd.hpp>

which seems to be a forward declaration header provided by the boost library itself. Then, in the specific .cpp files where the functionality is used, I had the full header:

#include <boost/property_tree/ptree.hpp>

This did end up reducing my build time atleast as far as the repeated parsing of ptree.hpp went.

I tried to see if there was any equivalent forward declared headers for adjacency_list.hpp but something equivalent like adjacency_list_fwd.hpp does not seem to be provided by the boost graph library.

(1) Is this the right approach to try to reduce build times -- having only minimal header files in user .h files and then having the full header file in the .cpp files where the class is actually needed?

(2) Is there a way to structure the boost graph library code so that build times are reduced? (I realize that this specific question is somewhat open ended, but hope this post is not closed as I am looking for specific inputs that are not opinion based on how to reduce build time for boost graph library, especially given that other boost libraries seem to provide a minimal forward declaration header).

Solution:

It depends on what definitions are necessary in the header.

Basically you may be able to use your own forward declarations of your own types.

Otherwise you should typically use the PIMPL idiom, in this context also known as "compilation firewalling".

Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login