ramos
ramos

Reputation: 98

ANTLR4 - Listeners are faster compared to Visitors?

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?

Upvotes: 4

Views: 1377

Answers (1)

GRosenberg
GRosenberg

Reputation: 5991

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.

Upvotes: 4

Related Questions