-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Improve Regex performance (mainly interpreted) #449
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
Reduce the checks needed and elimiate unnecessary layers of function calls.
This doesn't show up in real regexes and is just adding unnecessary complication to the code. No one writes `[a-b]`... they just write `a`. SingletonInverse is more useful, as you can search for any character except for a specific one, e.g. find the first character that's not a dash.
It's small but isn't getting inlined; it's only called in 4 places, but on hot paths, and inlininig it nets around an ~8% throughput win.
lpereira
reviewed
Dec 2, 2019
...ibraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexCharClass.cs
Show resolved
Hide resolved
...ibraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexCharClass.cs
Show resolved
Hide resolved
eerhardt
reviewed
Dec 2, 2019
src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/Match.cs
Show resolved
Hide resolved
eerhardt
reviewed
Dec 2, 2019
...raries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexInterpreter.cs
Show resolved
Hide resolved
eerhardt
approved these changes
Dec 2, 2019
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. Just a couple minor nits/questions.
Thanks for reviewing, @eerhardt. |
This is awesome, thanks @stephentoub. Do you think there may much more that can be achieved incrementally? |
I do, e.g. #496 |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
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.
This is a follow-on to #271, which improved the performance of Regex, mainly for compiled regular expressions. This PR improves the throughput of interpreted Regexes, those without the RegexOptions.Compiled option.
The primary change here is similar in nature to the primary change for Compiled: generating a fast lookup for ASCII values rather than having to parse/analyze the character class description for each character matched against it. In the Compiled case, we took the time to analyze the first 128 characters and emit a static lookup to make it fast to find the answer for any character < 128. But generating such a lookup table takes time. For Compiled, we're willing to pay that time, because signing up for Compiled is done when you want to maximize throughput at the expense of such upfront costs. We don't have that luxury for non-compiled. Instead, we throw a small additional amount of memory at the problem. Instead of storing 128 bits per character class (one for each character), we store 256 bits per character class: for each character a bit indicating whether we've evaluated it and a bit indicating the result. The first time we encounter a character, we do the more costly evaluation, and then we store back both that we evaluated it and the result; subsequent accesses will see that it was already evaluated and just use the cached result.
A few other, smaller opportunistic changes were also made here based on profiling, including improving some of the codegen around repeated array accesses and inlining a function on a hot path where doing so made an impactful difference. Also a tiny bit of code cleanup as I was touching the relevant functions.
cc: @danmosemsft, @ViktorHofer, @davidwrighton, @eerhardt