- Notifications
You must be signed in to change notification settings - Fork1.6k
<regex>: Speed up searches for regexes that start with assertions#5576
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
StephanTLavavej merged 2 commits intomicrosoft:mainfrommuellerj2:regex-speedup-assertion-searchesJun 14, 2025
Merged
<regex>: Speed up searches for regexes that start with assertions#5576
StephanTLavavej merged 2 commits intomicrosoft:mainfrommuellerj2:regex-speedup-assertion-searchesJun 14, 2025
Uh oh!
There was an error while loading.Please reload this page.
Conversation
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
StephanTLavavej approved these changesJun 10, 2025
Member
StephanTLavavej commentedJun 11, 2025
I'm mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed. |
StephanTLavavej approved these changesJun 14, 2025
Member
StephanTLavavej commentedJun 14, 2025
I resolved a trivial adjacent-add conflict with#5535 in |
a4c5e3f intomicrosoft:main 39 checks passed
Uh oh!
There was an error while loading.Please reload this page.
Member
StephanTLavavej commentedJun 14, 2025
Must go faster! 🚗 🦖 😻 |
Sign up for freeto 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.
Towards#5468. This extends the skip heuristic to the remaining unhandled assertions: word boundaries and lookahead assertions.
_Matcher2::_Skip()than in_Matcher2::_Is_wbound(), because we don't have to do any special handling for the start or end of the input string:_Skip()is never called for the start (or as the comment at the top says,--_First_argis valid) and it cannot skip beyond the end of the input string (_Last) anyway. So we only have to handle the middle case and have to keep looking for the first position where the word character property changes (\b) or where it does not change (\B)._Skip()is just a heuristic, so it's fine if we don't exclude some non-matches. There is only one thing we must not do here: Skip something that could match.<regex>: Nonlinear slowdown with increasing string length #5452, this does not suffer from the same problem because we only move forward in the checks: If evaluating the start of the assertion body or the regex after the assertion body tells us that we should skip until position x, then we search next for the first possible position the other part of the regex matches starting from x. No position is evaluated more than once for the assertion and once for the remainder of the regex after the assertion, which implies a running time linear in the input._N_endifnodes. (Nothing relevant happens at such nodes. They don't imply anything about the input string. Their only feature is that all the branches of an_N_ifnode join here again, but the problematic case is when branching happens, not when branches end.)<regex>: Avoid generating unnecessary single-branch_N_ifnodes #5539, this doesn't actually make a difference if the current matcher and parser are combined. It can improve performance, though, when the current matcher evaluates an NFA generated by an old parser.Tests
I added some tests for lookahead assertions. The tests use
regex_replace()because it performs several searches into a single test call.There already was test coverage for skipping of word boundaries in
VSO_0000000_regex_use'stest_DDB_153116_replacements()function. But the test coverage had a gap: It didn't check for correct behavior if two (or more) non-word characters are next to each other. I closed this gap by adding some spaces to the existing tests.Benchmark