Improve performance of card rendering parser #3886
Merged
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.
Looked into why this was happening, seems to be the
recognize(manyX(pair(not(<parser>)), anychar)))pattern, which calls<parser>on every single character. This was fine in 2021, but after #1968 and #2141, cards with deeply nested clozes commonly result in extremely bloated* inputs that it can't handle fast enoughThe speedup is mainly realised by making use of memchr to skip ahead to occurences of
[, a prefix of tags. Also, when nested withinrecognize,manyXneedlessly allocates a vec, populates it per character and then throws it away. This pr removes that fromtext_nodeandclosing_parseras wellBenchmarking with
rslib/bench.sh, using the deeply nested malformed cloze example**, before and after:Where this appears to shine: was able to raise the nested cloze limit from 8 to 10 and open up the preview of the abovementioned malformed example without anki complaining of a blocked main thread (pre-pr: ~3.5s stall on my machine, a >94% speedup)
I can't see any breakage resulting from this. Only the internal representation was changed, i.e. consecutive text nodes are now possible, which shouldn't result in changes to the syntax this parser admits. Tested with decks that use sound and tts tags
*the mentioned malformed example gets blown up to 3mb when passed to the parser
**before (nested) cloze metadata gets added to it