[comp.std.c] Conditional inclusion

bright@Data-IO.COM (Walter Bright) (10/24/89)

In article <14240@well.UUCP> nagle@well.UUCP (John Nagle) writes:
<	#ifndef XXX
<	#define XXX
<	...content...
<	#endif
<This works, but on the second inclusion, the file still has to be read and
<parsed, at least by the level of processing that reads "#" statements.
<With widespread use of this technique within library files, some files may
<be read a large number of times, mostly to be ignored.  This slows compilation.

This problem has been made worse by ANSI C, which now requires that the
text in false conditionals be "tokenized". Tokenizing is a far slower
process than going into a special loop that screams past characters
until a preprocessor line is found ('\n' { whitespace } '#'). In fact, on
most CPUs once a line is determined to not be a preprocessor line, an
extremely fast scan for '\n' can be done. Tokenizing is at least an order
of magnitude slower.

peter@ficc.uu.net (Peter da Silva) (10/25/89)

In article <14240@well.UUCP> nagle@well.UUCP (John Nagle) writes:
<	#ifndef XXX
<	#define XXX
<	...content...
<	#endif
<This works, but on the second inclusion, the file still has to be read and
<parsed, at least by the level of processing that reads "#" statements.
<With widespread use of this technique within library files, some files may
<be read a large number of times, mostly to be ignored.  This slows compilation.

In practice, you can combine this with a second technique:

	#ifndef FROB_H
	#define FROB_H

	#ifndef SYS_FOO_H
	#include <sys/foo.h>
	#endif

	#ifndef SYS_BAR_H
	#include <sys/bar.h>
	#endif

	...

	#endif

This completely resolves the efficiency question.
-- 
Peter da Silva, *NIX support guy @ Ferranti International Controls Corporation.
Biz: peter@ficc.uu.net, +1 713 274 5180. Fun: peter@sugar.hackercorp.com. `-_-'
"That particular mistake will not be repeated.  There are plenty of        'U`
 mistakes left that have not yet been used." -- Andy Tanenbaum (ast@cs.vu.nl)