[antlr-interest] Performance and LA(int)
Terence Parr
parrt at cs.usfca.edu
Wed Feb 23 10:26:49 PST 2005
On Feb 23, 2005, at 9:18 AM, Paul J. Lucas wrote:
> In recent profiling, the ANTLR lexer method LA(int) is called a
> *lot*. Why does the ANTLR-generated code repeatedly call it?
Hi Paul,
Because it's easier to generate code that way. I was kinda hoping the
compiler would see the obvious common subexpression. Apparently not,
eh? Are you sure it actually does call it repeatedly?
> Why not copy the current value into a local variable?
>
> final int c1 = LA(1);
> // ... do whatever with 'c1' rather than repeatedly
> // calling LA(1) ...
>
> In the case of multiple lookahead, simply use multiple
> temporaries:
>
> final int c1 = LA(1);
> final int c2 = LA(2);
> // ...
"a kingdom for a decent compiler optimizer" ;)
Ter
--
CS Professor & Grad Director, University of San Francisco
Creator, ANTLR Parser Generator, http://www.antlr.org
Cofounder, http://www.jguru.com
More information about the antlr-interest
mailing list