Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANTLR4 - Listeners are faster compared to Visitors?

Tags:

antlr4

I am trying to parse java files using ANTLR4 and walk the parse tree searching for specific function calls.

While I am able to achieve this using both Visitor and Listener approach, but stress tests reveals that Listeners are faster compared to Visitors, which goes contrary to popular belief.

Theoretically, Visitors are supposed to be faster as they would only inspect specific node, where as listeners inspect all. Anyone know why this would be the case?

like image 868
ramos Avatar asked Oct 12 '25 09:10

ramos


1 Answers

In ANTLR, listeners should be faster than visitors, though the performance difference will be less than easily measurable, if at all.

Listeners use the walker algorithm in ParseTreeWalker. Visitors use the algorithm in AbstractParseTreeVisitor. Both 'consider' all nodes.

Beyond the slight implementation differences, the one quantitative difference is that visitor calls involve the overhead of generic return type processing. Still, should be a negligible impact on performance in any modern JVM.

like image 157
GRosenberg Avatar answered Oct 16 '25 07:10

GRosenberg