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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With