2

I am currently working on a C++ project and am quite often using templates. Now I wonder if I should start worrying more about / cleaning up deep #include trees.

After removing not-needed includes, the code size after running the C preprocessor gcc -E on my .cpp files is:

  • 50% of the files: ~40k lines,
  • 30% of the files: between 40k and 80k lines,
  • 20% of the files: between 80k and 180k lines.

Is there some standard if these are large/small line counts? At what point does it become worth being more aggressive in reducing the #includes?

Community
  • 1
  • 1
hrr
  • 1,807
  • 2
  • 21
  • 35

2 Answers2

6

It doesn't matter how many lines of code there are. What matters is whether you feel build times are tolerable. If it takes too long to compile, then you need to speed it up, for example by eliminating unneeded includes.

But as long as you don't have a problem with build times, why bother worrying about whether you include too much?

jalf
  • 243,077
  • 51
  • 345
  • 550
  • And with templates, compile time isn't closely related to number of lines of code. – Ben Voigt Jul 11 '11 at 16:39
  • @Matthieu: complex macros are reflected in size of preprocessor output – Ben Voigt Jul 11 '11 at 17:16
  • @Ben Voigt: in size, yes, in the number of lines, not necessarily... the lines can get really long though :/ – Matthieu M. Jul 11 '11 at 17:55
  • @Ben Voigt: by the way, if you are interested in preprocessor trickery, Chandler Carruth (Google) is trying to get CLang to output the various stages of macro expansions when an error is reported there. This may, finally, makes Boost.Preprocessor easier to use! – Matthieu M. Jul 11 '11 at 18:11
  • @Ben Voigt: good point, there are definitely plenty of template instantiations. – hrr Jul 11 '11 at 20:24
  • The larger files take about 20 seconds to compile on a 3.3 GHz machine, 24 GB RAM. So compilation still feels a bit slow, yet with make -j it is not prohibitive. – hrr Jul 11 '11 at 20:25
  • @hrr: well, again, it's up to you to decide. It's worth trying to improve on the compilation speed if you feel the productivity boost from the faster compile times would offset the time you'd have to invest in trying to improve compile times. We can't say if 20 seconds is acceptable or not. – jalf Jul 11 '11 at 20:35
2

Size after running the preprocessor is truly unimportant; be concerned with the size BEFORE running the preprocessor. And only if your inclusion tree is very confusing should you be particularly concerned; if it's of a reasonable depth and complexity, you should spend your time working on something that has more of an impact on your code.

Paul Sonier
  • 38,903
  • 3
  • 77
  • 117