[antlr-interest] Performance and LA(int)

Micheal J open.zone at virgin.net
Wed Feb 23 16:08:09 PST 2005


 
> 	In recent profiling, the ANTLR lexer method LA(int) is called a
> 	*lot*.  Why does the ANTLR-generated code repeatedly call it?
> 	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);
> 		// ...
> 

For C# code generation, LA(1) and LA(2) are cached in *lexers*. This
resulted in noticeably better performance. Our basic performance tests
suggested that caching LA1/LA2 in parsers (in addition to lexer caching)
didn't always result in better performance. Depends on the grammar it seems.
Weird or what?

Micheal



More information about the antlr-interest mailing list