-
Notifications
You must be signed in to change notification settings - Fork 300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize block parsing by replacing regexes #137
Merged
Merged
Conversation
This file contains 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
The use of Matcher in there showed up showed up in a profiling session on Android. Using Matcher also means doing more short-lived allocations. Replace it with some simple looping code. This gives a nice speedup for the whole spec benchmark (which is a long document with quite a few fenced code blocks). Before: Benchmark Mode Cnt Score Error Units SpecBenchmark.wholeSpec thrpt 200 104.297 ± 1.124 ops/s After: SpecBenchmark.wholeSpec thrpt 200 124.558 ± 0.485 ops/s
The speedup should be mostly in the common case where there's no match, in that case the new code just checks the first character and then aborts whereas the old code would try a full regex match.
Codecov Report
@@ Coverage Diff @@
## master #137 +/- ##
============================================
+ Coverage 95.18% 95.32% +0.13%
Complexity 161 161
============================================
Files 93 93
Lines 2619 2714 +95
Branches 327 356 +29
============================================
+ Hits 2493 2587 +94
+ Misses 63 62 -1
- Partials 63 65 +2
|
Here's some numbers from Android. Before:
After:
For simple and short only have a single paragraph. Medium and long have multiple. So it looks like with text that has a lot of blocks, this reduces parsing time by about 30%, nice :). |
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.
Block parsers use regex matching to check whether a line matches that block. That means for every line, we try a bunch of regexes. These can add up. The use of
Matcher
showed up in a profiling session on Android.One way to fix that would be to check only one character first, and only if it matches what the block parser expects, run the full regex. But that seems like a bit of a strange mix.
Instead, this PR replaces all the regex use in block parsers with fairly simple looping code.
This gives a nice speedup for the benchmarks. Before:
After:
So that's about a 15% to 18% improvement! 🎉
There's probably some potential for improvement in inline parsing as well, but haven't looked at that yet.