Page 1 of 1

Cleaning up `#ifndef xyz_H #include "xyz.h" #endif'

Posted: Sun Apr 03, 2011 7:58 pm
by Angeba
There are several of such conditionals in various
files. The `#ifndef' and `#endif' should be
unnessesary, because they already are in the included
files (where any are missing they should be added).

Should we remove these conditionals to clean up the
code a bit? Or is there any reason to keep them?

Re: Cleaning up `#ifndef xyz_H #include "xyz.h" #endif'

Posted: Sun Apr 24, 2011 10:22 pm
by rotunda
This is put in include files to prevent them from being included more than once.

Re: Cleaning up `#ifndef xyz_H #include "xyz.h" #endif'

Posted: Sun May 01, 2011 10:31 am
by Angeba
I meant for example in `src/client/types.h', there is:

Code: Select all

#ifndef KEYS_H
#include "keys.h"
#endif
Since `src/client/keys.h' reads:

Code: Select all

#ifndef KEYS_H
#define KEYS_H
...
#endif /* ! KEYS_H */
, it should be sufficient to just write:

Code: Select all

#include "keys.h"
in `src/client/types.h'.
There are a lot of such redundant constructs. They don't harm, but probably are
unnessesary bloat. I would remove them, while I am doing any modifications to such files,
if they are not useful for anything.

PS.: Now I got the idea, that it was done like I explained above, for performance reasons.
That construct removes the need for the C-preprocessor to scan the to-be-or-not-to-be-included file, so probably compiling will be somewhat quicker (haven't
done any tests yet).
The result should be the same either way, but without the conditionals around `#include',
the preprocessor needs to scan through the included file (reading the `#ifndef ...' and
scanning upto the matching `#endif').
Perhaps I'll make a comparison to compare compilation time.

Re: Cleaning up `#ifndef xyz_H #include "xyz.h" #endif'

Posted: Sat May 07, 2011 11:21 am
by rotunda
Yes, this is probably an old optimization. I wouldn't expect it to give much gain on modern machines.

Re: Cleaning up `#ifndef xyz_H #include "xyz.h" #endif'

Posted: Sat May 07, 2011 9:29 pm
by Angeba
Having read the related chapter of the GNU C Preprocessor info documentation
(`info cpp "Once-Only Headers"'), I created a simple test program. It did include one big
include file (which was protected for once-only inclusion, standard way), many times.

I made 2 versions of the test program, one with many of these statements:

Code: Select all

#ifndef ...
#include ...
#endif
#ifndef ...
#include ...
#endif
...
, the other version with the same number of just these statements:

Code: Select all

#include ...
#include ...
...
Now I timed compilation of these test "programs". Like you expected, Rotunda, I could not
measure a difference in compilation time between the two versions.
Perhaps other preprocessors/compilers than gcc (GNU cpp) behave different, but at least gcc
does not need such tweaks (for already more than 10 or 15 years probably).

So if no one else has a rational different view, I'll remove the conditionals around the `#includes', when they show up in my editor.