Skip to content

Serialize Lark.grammar (fixes issue #1472) - #1506

Merged
erezsh merged 8 commits into
lark-parser:devfrom
NasalDaemon:serialize-grammar
Jul 13, 2025
Merged

erezsh merged 8 commits into
lark-parser:devfrom
NasalDaemon:serialize-grammar

Conversation

@NasalDaemon

Copy link
Copy Markdown
Contributor

Fixes #1472

@erezsh

erezsh commented Jan 17, 2025

Copy link
Copy Markdown
Member

How much does this change add to the cache size? (in rough terms)

@NasalDaemon

Copy link
Copy Markdown
Contributor Author

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
cache with grammar: 115,970 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
With caching, but using Lark.grammer = load_grammar(...): 370ms
With caching+serializing grammar: 320ms

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.

@erezsh

erezsh commented Jan 18, 2025

Copy link
Copy Markdown
Member

How does serializing the grammar make it run faster? It only adds operations, and doesn't remove any.

@NasalDaemon

Copy link
Copy Markdown
Contributor Author

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.

@erezsh

erezsh commented Jan 19, 2025

Copy link
Copy Markdown
Member

I think you're mistaken.

@NasalDaemon

Copy link
Copy Markdown
Contributor Author

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.

@erezsh

erezsh commented Jan 21, 2025

Copy link
Copy Markdown
Member

I prefer cache_grammar=False.

Also consider adding text about this in the reconstructor error.

@NasalDaemon
NasalDaemon force-pushed the serialize-grammar branch 2 times, most recently from ed834e6 to b20cf8b Compare February 5, 2025 12:54
@NasalDaemon

Copy link
Copy Markdown
Contributor Author

Sorry, was away on holiday after your last comment. I have committed further changes taking it into account. Any further feedback?

@erezsh

erezsh commented Jul 6, 2025

Copy link
Copy Markdown
Member

@NasalDaemon Can you explain the code in tree_matcher? Specfically when postlex is None?

@NasalDaemon

NasalDaemon commented Jul 6, 2025 •

Copy link
Copy Markdown
Contributor Author

@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:

self.terminals, self.rules, self.ignore_tokens = self.grammar.compile(self.options.start, terminals_to_keep)

When loading from cache, they are deserialised:

lark/lark/lark.py

Lines 540 to 544 in 87bb8ef

self.rules = [Rule.deserialize(r, memo) for r in data['rules']]
self.source_path = '<deserialized>'
_validate_frontend_args(self.options.parser, self.options.lexer)
self.lexer_conf = self._deserialize_lexer_conf(data['parser'], memo, self.options)
self.terminals = self.lexer_conf.terminals

@erezsh

erezsh commented Jul 6, 2025 •

Copy link
Copy Markdown
Member

Hmm no, I get it now. You had it right before. When there is a postlex, the always_accepts value can affect the compilation, which will disrupt the reconstructor. So when there is a postlex, we need to recompile the grammar. But otherwise, you're probably right that we don't need to.

@NasalDaemon

Copy link
Copy Markdown
Contributor Author

Hmm no, I get it now. You had it right before. When there is a postlex, the always_accepts value can affect the compilation, which will disrupt the reconstructor. So when there is a postlex, we need to recompile the grammar. But otherwise, you're probably right that we don't need to.

Ah, indeed! I've reverted to the old behaviour again

@erezsh

erezsh commented Jul 6, 2025

Copy link
Copy Markdown
Member

@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.

@NasalDaemon

Copy link
Copy Markdown
Contributor Author

@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.

@erezsh
erezsh changed the base branch from master to dev July 13, 2025 12:00
@erezsh
erezsh merged commit fb00ddf into lark-parser:dev Jul 13, 2025
erezsh added a commit that referenced this pull request Jul 13, 2025
Fixes for PR #1506, which adds the option to cache the grammar definition
@erezsh

erezsh commented Jul 13, 2025

Copy link
Copy Markdown
Member

Thanks for the contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cache option does not work with Reconstructor

2 participants