Don't Panic! 50% plus performance improvement by jimidle · Pull Request #4192 · antlr/antlr4

added 2 commits

March 17, 2023 11:33
… flowcontrol - 50% performance improvement

  - Prior to this change, a recognition error was tracked by performing a panic(),
    which the generated code for rules would then use recover() to discover. However,
    recover() is not like catch(){} in Java and is expensive to run even if there is
    no panic() to find on the execution stack. Eliminating this and doing a simple check
    at the end of rule execution brings with it a massive performance improvement up to
    50% of CPU execution time. Now that collections and context caching is working correctly
    this is a significant improvement in execution time.

Signed-off-by: Jim.Idle <jimi@idle.ws>
Prior to this change, the runtime and generated code was using panic() and recover()
to perform error checking and reporting. This is extremely expensive and just not the
way to do it.

This change now uses goto, and explicit checking for error state after selected calls
into the runtime. This has greatly improved parser performance. Using the test code
provided by a recent performance issue report, the parse is now twoice as fast as the
issue raised was hoping for.

Signed-off-by: Jim.Idle <jimi@idle.ws>