lezer-parser/lezer

Speeding up build parser

nchen63 opened this issue · 3 comments

I have a grammar that takes a long time to build. When profiling I see most of the time (160 seconds) is spent in canMergeInner in automaton.ts.

  • Are there general principles when writing the grammar to speed this step up?
  • Are you open to optimizing the code so that instead of a nested for loop over 2 arrays to find the matching term, the code does a lookup in a map object?

There are often small changes you can make to make a grammar generate dramatically fewer states. Sometimes they are straightforward, such as avoiding rule count blowup by refactoring rules to not have too many optionals (see this patch). In other cases, unfortunately, they are really non-obvious, requiring a lot of insight into what the parser generator will do with your grammar.

If you can find an elegant way to optimize that function, I'd be open to that. But the awkward thing is that, if it builds the table itself, it's going to either have to cache those somewhere, or generate it again and again as it is called on different states. Maybe a lazily-computed property on the state objects would work.

Can you tag/release the fix?

I've released a version 1.5.1