Suggestion for improving C++ parser memory usage

Mark Mitchell mark@codesourcery.com
Thu Dec 30 18:45:00 GMT 2004


Roger Sayle wrote:

> The first observation is that cp_parser_implicitly_scoped_statement
> can be tweaked such that the return type is void rather than tree,
> as its result is never used.

Yes.

 > Next a call to cp_lexer_peek_token can be
> used to enter a switch statement that determines if the following keyword
> or token can't possibly need its own scope.  For example, ";" but
> hopefully as RID_IF and many of the remaining C++ constructs.
> 
> From my limited understanding of C++ parsing, statements such as
> RID_IF, RID_WHILE, RID_DO and RID_FOR don't/can't use an immediately
> enclosing scope, as they open new scopes for their conditions and
> bodies.

Yes, what you say is correct.  I am not sure how common that kind of 
nesting is, but if it turns out to be common your optimization might 
indeed be a win.

Another approach would be to try to be lazier; set a flag that said that 
we are in an implicit scope, and then create the actual scoping 
statement only when it was needed.  That would be a much more invasive 
change; you would need to find all places where an object with a 
destructor (including temporary objects) is created, and create the 
scope at that point.  I'm not suggesting trying to do this, but it would 
handle things like:

   if (x)
     f(3);

which your approach would not.

You should of course verify that for:

   if (X x)
     ;

the destructor for "x" is run at the end of the "if", and not at the end 
of the enclosing scope.

-- 
Mark Mitchell
CodeSourcery, LLC
mark@codesourcery.com
(916) 791-8304



More information about the Gcc mailing list