Repository navigation
Serialize Lark.grammar (fixes issue #1472) - #1506
Conversation
|
How much does this change add to the cache size? (in rough terms) |
In my scenario with a file.lark of 4kb: cache without grammar: 63,994 bytes making an increase of ~52kb As a quick and dirty performance test, I ran my script which uses lark to parse a file and produce a cpp (with jinja) multiple times under different regimes. Without caching: 440ms This shows that there is indeed a noticeable speed benefit even if the cache has become larger, despite the overhead of jinja and the rest of my script. |
|
How does serializing the grammar make it run faster? It only adds operations, and doesn't remove any. |
According to the above tests, deserialising the grammar from the cache is faster than reproducing it from the source .lark file each time. |
|
I think you're mistaken. |
If you would like to avoid the cost of de/serialising the grammar by default, I can make its serialisation optional instead, so you must specify cacheWithGrammar=True. |
|
I prefer Also consider adding text about this in the reconstructor error. |
ed834e6 to
b20cf8b
Compare
b20cf8b to
f9adb01
Compare
|
Sorry, was away on holiday after your last comment. I have committed further changes taking it into account. Any further feedback? |
|
@NasalDaemon Can you explain the code in tree_matcher? Specfically when postlex is None? |
I was attempting avoid needing to use parser.grammar altogether, as it isn't loaded from the cache if cache_grammar=False. It looks like the grammar never actually needs to be compiled from the parser, as the rules and terminals of a parser are always created during its construction (including from a cache), so I've now completely removed the call to compile and just copy the stored rules and tokens from parser. When not loading from cache (or compiling for the first time), the results are stored in parser.rules and parser.terminals: Line 407 in 87bb8ef When loading from cache, they are deserialised: Lines 540 to 544 in 87bb8ef |
|
Hmm no, I get it now. You had it right before. When there is a postlex, the |
1a53fba to
414da9c
Compare
Ah, indeed! I've reverted to the old behaviour again |
|
@NasalDaemon I made some fixes in a new PR. Feel free to review. The only thing that's missing is tests. I'll do it some day, unless you want to do it. |
Thank you for this! Lgtm so I have approved. |
Fixes for PR #1506, which adds the option to cache the grammar definition
|
Thanks for the contribution! |
Fixes #1472