This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

cpplib: Drastically cut memory usage


It's brown paper bag time.

There's been a bug in cpplib since about November.  It doesn't affect
the output of cpplib, just memory usage.  Roughly, when expanding
macros, cpplib moves round a circular chain of memory chunks, and
locks the current one when an expansion is started, so that any nested
expansions don't circle round and overwrite the outer expansion.
Ordinarily it just cycles round the chain, using the next chunk when
the previous one is full, but if it encounters the locked "head"
because of heavy (nested) macro expansion it allocates and inserts a
new chunk in the cycle.

Except... I had a thinko that didn't release the lock in one
circumstance, so that we just keep allocating new chunks when the
previous one was full.  Oops.

[I used to think I couldn't achieve what I wanted with an obstack.  I
now think I probably can, so I may revert to obstacks in the near
future.  There are a couple of complications with token lookahead, but
it should be possible.]

The cause was an "optimisation" where we didn't push a macro expansion
if it was empty.  The problem was we would only release the macro
expansion lock when popping the outermost expansion, and I forgot to
release the lock when doing the no-push optimisation.  Rather than
adding it now, I've just removed the optimisation as I'm not convinced
it really saves us much, if anything, overall.

With the patch below, peak memory usage for stand-alone CPP is as
follows.

File		Old Peak	New Peak	Saving

cppmain.c	1.28MB		1.23MB		4%
c-decl.c	6.31MB		2.36MB		63% (!)
insn-recog.c	9.67MB		1.65MB		83% (!!!)

For the curious, most of the memory used by cpplib above is to store
the tokenized expansions of macros, and random text like identifiers
and numbers.  Memory used during macro expansion never goes above 96K
for any of the above files.

Mark, I think this should go in 3.0.1 or 3.0.2.  Do you want it to
wait for 3.0.2?

Bootstrapping and making check x86 Linux.  C has made check so far
with no regressions.

Neil.

	* cppmacro.c (enter_macro_context): Push macro expansions even
	if empty.

Index: cppmacro.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cppmacro.c,v
retrieving revision 1.60
diff -u -p -r1.60 cppmacro.c
--- cppmacro.c	2001/08/11 07:33:38	1.60
+++ cppmacro.c	2001/08/11 21:28:24
@@ -669,16 +669,12 @@ enter_macro_context (pfile, node)
       list.limit = macro->expansion + macro->count;
     }
 
-  /* Only push a macro context for non-empty replacement lists.  */
-  if (list.first != list.limit)
-    {
-      context = next_context (pfile);
-      context->list = list;
-      context->macro = macro;
+  context = next_context (pfile);
+  context->list = list;
+  context->macro = macro;
       
-      /* Disable the macro within its expansion.  */
-      macro->disabled = 1;
-    }
+  /* Disable the macro within its expansion.  */
+  macro->disabled = 1;
 
   return 1;
 }


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]