11

I have a project where each C/C++ file uses a bunch of header files. But about 70-80% of the header files that each C/C++ file uses is the same. So to make my code more readable, I am planning to include all the headers that I will need in the project into a single header file say common_headers.h and include this in all my C/C++ files like this:

#include "common_headers.h"

Now this will include all the necessary headers but also few extra headers that will not be used by an individual file. I want to know if doing this way, would it hit the performance at run time by any chance?

I am fine with a few milliseconds extra delay for compiling the code, but I want to know if this will impact my runtime performance?

Description of headers used:

  1. Most of them are standard C/C++ headers.
  2. The user defined headers have inline template functions in them .
  3. No static functions in user defined headers.

This is my Compiler: g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-3)

Vivek V K
  • 1,100
  • 2
  • 15
  • 33
  • does your compiler support precompiled headers? – M.M Jul 29 '14 at 05:34
  • 6
    If one of the headers included `static Foo foo { call_expensive_function(); };` then it would ! I guess we cannot be too specific without knowing just what's in those headers. – M.M Jul 29 '14 at 05:35
  • Do you mean compilation time performance drop or do you ask whether execution time performance will change? – Eugene Podskal Jul 29 '14 at 05:35
  • @EugenePodskal I am more worried about run time performance than compile time. I am ok with a few extra seconds it takes for compiling – Vivek V K Jul 29 '14 at 05:41
  • 2
    If they are unused, the object files may be bigger, but the linked binary/library should not, so long as you use -flto optimizations or on older versions of gcc -ffunction-sections -fdata-sections and --gc-sections. On other compilers you may see some amount of bloat. Just for reference, gtk does this same thing with and doesn't bother to add these optimizations last I checked, but I'm not saying I would use them as a model for good programming practice. – technosaurus Jul 29 '14 at 05:47

4 Answers4

17

COMPILATION:

If something is included then it has to analyzed even if it will never be actually compiled and linked, so compilation time will increase for sure - Do not include unused headers.

RUNTIME:

It has been already mentioned by @DonReba that unused headers may include some pragma directives that can change the resulting executable, but usually it won't be the case.

Most of the unused functions and declaraions will be optimized out, excluding some specific cases - Do unused functions get optimized out?. The resulting exe may become a bit bigger, but those functions and variables won't be used, so overall impact will be minimal. - - Nonetheless, do not include unused headers.

SUMMARY:

If you can modify your source code to not include anything unneeded - modify it.

Personnaly I prefer to have self-contained modules(headers), that include everything they need - nothing more, nothing less. Such modules can be added and removed without hindsight and possibility that some unneeded dependency has been left. They are still not panacea, but coupled with attentiveness and some code analysis they will keep your program free from deadweight headers.

EDIT:

Precompiled headers:

Precompiled headers are used to reduce compilation time for often used, but rarely changed headers(system headers, huge project headers), so if those unused headers are included in the precompiled header, then the compilation time effect during subsequent compilations will be minimized. Still, all runtime issues, no matter how small they are, stay the same as for simple header includes.

Community
  • 1
  • 1
Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53
  • I cant get why the exe file will become bigger as I will not be including any header that not even one C++ file in my project uses. Even if I add the headers in individual files, it is the same number of headers that is getting added right? – Vivek V K Jul 29 '14 at 06:35
  • http://stackoverflow.com/questions/6215782/do-unused-functions-get-optimized-out describes why and what unused elements will not be optimized out during compilation and linking. Most headers include so-called [include guards](http://stackoverflow.com/questions/8020113/c-include-guards) or `#pragma once` directives so multiple includes of the same header won't really influence anything. But sometimes include guards aren't there due to some reasons, so each include can increase the overall program size - usually it is rarely used in modern C\C++. – Eugene Podskal Jul 29 '14 at 06:55
2

Depends on compiler. Today's most compilers are smart and use precompiled headers to improve performance. I use GCC compiler which supports precompiled header and AFAIK does not affect performance.

Kaidul
  • 15,409
  • 15
  • 81
  • 150
2

Short answer to the question asked: No.

Long answer:

More headers mean marginally more chance of some problem appearing that might manifest as a performance issue, but it's really not a concern.

Many consider it poor style to do as you plan, but there are also those that consider it acceptable.

One of the key reasons for avoiding this style is that it will make it easier to get circular dependencies.

I would discourage it due to the compile time issue where I do not have pre-compiled headers.

Keith
  • 6,756
  • 19
  • 23
0

It is possible that inclusion of an extra header would produce different runtime code due to overriding a preprocessor directive. But this would not be a normal situation.

Visual C++, GCC, and Clang support precompiled headers for improving compilation times for use cases like yours.

Don Reba
  • 13,814
  • 3
  • 48
  • 61