Avoid repeated parser table decode and cut parse setup overhead (issue 630)#631
Merged
ratmice merged 2 commits intosoftdevteam:masterfrom Apr 30, 2026
Merged
Conversation
ltratt
reviewed
Apr 28, 2026
Member
|
I have one easy comment: @ratmice does this look OK to you? |
Collaborator
|
I haven't yet had a chance to look, but I will try and have a gander this evening, in a couple of hours. |
Collaborator
|
Looks like what it says on the tin, so this looks OK to me, couldn't help but join the bikeshed a little though. |
dcdfda0 to
bdd2053
Compare
bdd2053 to
bf1870b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
While profiling a workload that parses many small inputs in a tight loop, I found two sources of avoidable per-parse overhead in
lrpar.First, generated
parse()functions fromlrpar_modwere calling_reconstitute(__GRM_DATA, __STABLE_DATA)on every invocation, which meant re-decoding the grammar and state table every time even though RTParserBuilder::new only borrows them. This change caches the reconstituted tables in generated code and reuses them across calls.Second, there were a couple of smaller setup costs in lrpar::parser:
Box<&dyn Fn(...)>, introducing a heap allocation around an already-borrowed callback on every parse.Vec<Result<...>>and then walked it again to build the lexeme vector.This PR removes those extra costs by:
OnceLock.lrpar::ParserTableswrapper so generated code does not need to name lrtable types directlySince the time spent in _reconstitute is proportional to grammar size, this change is particularly impactful there.
However, even on a very small grammar, such as calc_actions example, these changes brought a tight-loop parse benchmark down from roughly ~2.06 µs/parse to ~0.80 µs/parse.