Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find compilation bottlenecks?

How do I find which parts of code are taking a long time to compile?

I am already using precompiled headers for all of my headers, and they definitely improve the compilation speed. Nevertheless, whenever I make a change to my C++ source file, compiling it takes a long time (this is CPU/memory-bound, not I/O-bound -- it's all cached). Furthermore, this is not related to the linking portion, just the compilation portion.

I've tried turning on /showIncludes, but of course, since I'm using precompiled headers, nothing is getting included after stdafx.h. So I know it's only the source code that takes a while to compile, but I don't know what part of it.

I've also tried doing a minimal build, but it doesn't help. Neither does /MP, because it's a single source file anyway.

I could try dissecting the source code and figuring out which part is a bottleneck by adding/removing it, but that's a pain and doesn't scale. Furthermore, it's hard to remove something and still let the code compile -- error messages, if any, come back almost immediately.

Is there a better way to figure out what's slowing down the compilation?

Or, if there isn't a way: are there any language constructs (e.g. templates?) that take a lot longer to compile?


What I have in my C++ source code:

  • Three (relatively large) ATL dialog classes (including the definitions/logic).

    They could very well be the cause, but they are the core part of the program anyway, so obviously they need to be recompiled whenever I change them.

  • Random one-line (or similarly small) utility functions, e.g. a byte-array-to-hex converter

  • References to (inline) classes found inside my header files. (One of the header files is gigantic, but it uses templates only minimally, and of course it's precompiled. The other one is the TR1 regex -- it's huge, but it's barely used.)

Note:

I'm looking for techniques that I can apply more generally in figuring out the cause of these issues, not specific recommendations for my very particular situation. Hopefully that would be more useful to other people as well.

like image 848
user541686 Avatar asked Nov 12 '11 19:11

user541686


1 Answers

Two general ways to improve the compilation time :

  • instead of including headers in headers, use forward declare (include headers only in the source files)
  • minimize templated code (if you can avoid using templates)

Only these two rules will greatly improve your build time.

You can find more tricks in "Large-Scale C++ Software Design" by Lakos.

For visual studio (I am not sure if it is too old), take a look into this : How should I detect unnecessary #include files in a large C++ project?

like image 159
BЈовић Avatar answered Oct 15 '22 06:10

BЈовић