0

Having larger compilation units (as opposed to spreading the same code over multiple smaller compilation units) has some advantages and disadvantages:

  • Pro:
    • If two compilation units include the same large header, it will be compiled twice. If both compilation units are merged into one, the header gets compiled only once. This reduces compilation time.
    • Within a compilation unit, more optimizations are possible (such as inlining function calls), so this can result in a more optimized executable. To get similar performance with multiple compilation units, you can use link-time optimization, but that significantly increases compilation time.
  • Con:
    • If you make a small change after your code has been compiled, you will need to recompile a larger compilation unit, which takes more time.
    • Compilation time can be reduced by compiling different compilation units in parallel. If you have many small compilation units, they can be spread optimally across threads. If you have only a few, there is less flexibility and the slowest one may take a lot longer than any of the others, which is suboptimal.

As far as I can see, having larger header files (as opposed to spreading the same code over multiple smaller headers) only has disadvantages:

  • You always need to compile the entire header, even if you use just a small subset of it.
  • If you do need all the small headers, after preprocessing, there will be no difference compared to the single large header: compilation speed is the same and the same optimizations are possible.

The only disadvantage seems to be that there will be more file I/O and a bit more work for the preprocessor.

Nevertheless, many people seem to favor large headers, for example:

  • The STL seems to be grouped in just a few large header files.
  • The SystemC standard explicitly says users should #include <systemc> to get all of the SystemC code, and doesn't specify any partitioning into smaller headers which users might include to get just a subset.

This StackOverflow answer also suggests using few large headers, but that seems to be in the context of precompiled headers. In that case, indeed, it makes sense: If you combine all your headers into one, you only need to precompile that header once and you can use it everywhere. The alternative would produce many precompiled headers containing overlapping code.

But assuming I'm not precompiling my headers, are there other arguments for using large headers instead of many small ones?

PieterNuyts
  • 388
  • 2
  • 13
  • 2
    There is also usage/organization, remember only few headers is simpler than remember huge number of header. – Jarod42 Dec 09 '20 at 10:59
  • True, but in some cases the increase in compile time is a huge price to pay for that. If this were the only reason, I would expect authors to provide multiple headers that users can include, and then one or a few large ones that just include all the others. That way the user has the choice between efficiency and ease of use. – PieterNuyts Dec 11 '20 at 09:20

0 Answers0