Official Kotlin Grammar Performance

I’m using ANTLR4 and the official Kotlin G4 grammar (Grammar) to parse and analyze Kotlin source code.

Unfortunately the performance of this grammar can be fairly awful, even on simple inputs. For example, parsing a 20-arm if-else statement takes around 500ms. I’m currently encountering a situation where parsing successful examples of a simple program we have students write in an introductory course can take several seconds.

I’m not an expert on ANTLR4, but my understanding is that the problem has to do with the structure of the grammar, which has a lot of pass-through rules that result in very deep nesting. I’ve done a bit of work previously to flatten portions to reduce memory usage and improve performance without affecting correctness. However, I’m wondering if anyone knows of or maintains an optimized Kotlin G4 grammar that is both correct and results in improved performance?

FYI, now being discussed in the ANTLR community: [kotlin] Grammar is wrong and very slow · Issue #3580 · antlr/grammars-v4 · GitHub

Based on the comments here (Concurrent Parsing Bottlenecks · Issue #4344 · antlr/antlr4 · GitHub) there are also some obvious flaws with the official Kotlin grammar, as well as places where it apparently doesn’t reflect the parsing strategy used by the compiler.