<regex>
: Speed up searches for regexes that start with assertions
#5576
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_arg
is 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_endif
nodes. (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_if
node join here again, but the problematic case is when branching happens, not when branches end.)<regex>
: Avoid generating unnecessary single-branch_N_if
nodes #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