#pragma once vs #ifndef

Started by
13 comments, last by SeaBourne 13 years, 3 months ago
The idea is that the compiler can load the file into RAM and once it sees #pragma once it can stop there and go check if it has been included already. Whereas for the traditional way it has to parse the file looking for matching compiler directives until it finds the correct terminating #endif at the bottom.
That's wrong. The speed gain attained by the optimization is due to the fact that the file doesn't have to be opened a second time. It's the file access, not the parsing, that is the performance problem. Parsing takes a trivial amount of time compared to file I/O.

The optimization works like this (pseduo code)
[html]
FOR EACH translation unit
FOR EACH #include directive encountered
IF the file is in the set of "#pragma once" files
OR the file is in the set of files with include guards and the relevent preprocessor symbol is still defined:
We're finished processing the include (no need to open the file at all)
ELSE:
Process the file
If the file contained a pragma once, add it to the set of "#pragma once" files
If the file contained an include guard add it to the set of files with include guards,
along with the record of the relevent preprocessor symbol
[/html]

The important things to note are:
* The optimization works just as well with #pragma once as it does with include guards - it's slightly easier to implement with #pragma once, which is why the #pragma once optimization precedes the optimization being applied to normal include guards.
* An #include directive does not require the included file to be opened and read a second time, cutting down on file IO.
Advertisement

The idea is that the compiler can load the file into RAM and once it sees #pragma once it can stop there and go check if it has been included already. Whereas for the traditional way it has to parse the file looking for matching compiler directives until it finds the correct terminating #endif at the bottom.

That's wrong. The speed gain attained by the optimization is due to the fact that the file doesn't have to be opened a second time. It's the file access, not the parsing, that is the performance problem. Parsing takes a trivial amount of time compared to file I/O.

Yes, you're right of course. I blame that brain fart on having just got up! :)

It still doesn't make any difference in my world. In fact, in my standard non-standard ways that Apoqpiq loves so much, I get the most speed boost by breaking ALL the rules.

We have a single header file that includes all the others. This single file is made into a PCH. No other source files include anything other than this one master file. Which sounds about as suboptimal as you can be, right? Especially when you touch a single header and have to suffer a full rebuild for your trouble.

But, I urge anyone to at least try it one wet weekend. If you stick rigidly to the principal of including this ONE file only in all your sources, the speed boost you see will leave you slack jawed and you'll suddenly understand that a full rebuild each time you touch a loose header really isn't a problem when it only takes several seconds.

EDIT: Dunno if this works on gcc too well, depends on the version. DevStudio fair zings along, but the biggest win we ever had was switching someone elses project (done in the usual way) to our uber-header method when porting to DS. The PCH speed-up in CodeWarrior is ridiculous. A full rebuild of defcon took a couple of seconds, I shit you not.
------------------------------Great Little War Game
My pattern looks like this:

#ifndef FOO
#define FOO
#pragma once
// ... header stuff ...
#endif

Compiler can choose which one it likes best :) The big downside to #pragma is that you can't include the same header more than once in a translation unit, which I've seen used in some hacky situations:

#include "Foo.h"

#include "Bar.h"

// Hacky fun time
#undef FOO
#include "Foo.h"

But this is an exception to the rule and certainly should be handled on a case-by-case basis for any header that needs to do this.
Compiler can choose which one it likes best The big downside to #pragma is that you can't include the same header more than once in a translation unit, which I've seen used in some hacky situations:

I wouldn't call this a downside. #pragma once is designed specifically to call a header once, without the rather inelegant method of 3 separate # directives and having to make sure you're using a unique symbol.
If you need to call a header twice in the same translation unit then by all means use something other than the compiler directive designed specifically not to call the same header twice.


I use it in MSVC and it apparently works on the other major compilers too. If a feature is supported on the compilers you're using then I don't see a reason not to use it. While it's not supported on *every* compiler, the same could be said about several other c++ features which are standard compliant and *still* have poor compiler support.

#pragma once is pretty much safe to use cross compiler nowadays. If you want to use it you can. I think it was add in GCC 4.0.0 maybe earlier. The new compiler Clang supports it also afaik for Mac.

This topic is closed to new replies.

Advertisement