-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Eliminate backtracking in the interpreter for patterns with .* #51508
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
2511bfa
First cut of look up table for speeding up Go()
781541c
More efficient .* in RegexInterpreter
1c7faa0
sq
7ea6425
Get more debug info
9c234d4
Remove assert and add unit test
85b6897
Potential unit test
7a404e9
temp
1949445
Fix a bug
1564d7a
sq
7f76e5f
Add extra protection to the backtracking optimization
ac838ad
Add unit test
262d5d5
Revert
aacb00e
RegexCompiler changes
4f6bdd4
sq
d8e73cc
Remove debug unit tests
cb3f3f2
Add a length to the AsSpan call
3ba7f45
Address RegexCompiler comments and add unit tests
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ internal sealed class RegexInterpreter : RegexRunner | |
| private int _codepos; | ||
| private bool _rightToLeft; | ||
| private bool _caseInsensitive; | ||
| private int _maxBacktrackPosition = -1; | ||
|
|
||
| public RegexInterpreter(RegexCode code, CultureInfo culture) | ||
| { | ||
|
|
@@ -223,6 +224,20 @@ private bool MatchString(string str) | |
| { | ||
| if (runtextend - runtextpos < c) | ||
| { | ||
| // If MatchString was called after a greedy op such as a .*, we would have zipped runtextpos to the end without really examining any characters. Reset to maxBacktrackPos here as an optimization | ||
| if (!_caseInsensitive && _maxBacktrackPosition != -1 && runtextpos > _maxBacktrackPosition) | ||
stephentoub marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| // If lastIndexOf is -1, we backtrack to the max extent possible. | ||
| runtextpos = _maxBacktrackPosition; | ||
| ReadOnlySpan<char> runtextSpan = runtext.AsSpan(_maxBacktrackPosition, runtextend - _maxBacktrackPosition); | ||
| int lastIndexOf = runtextSpan.LastIndexOf(str); | ||
stephentoub marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (lastIndexOf > -1) | ||
| { | ||
| // Found the next position to match. Move runtextpos here | ||
| runtextpos = _maxBacktrackPosition + lastIndexOf; | ||
stephentoub marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
|
|
@@ -1185,6 +1200,7 @@ protected override void Go() | |
| int len = Math.Min(Operand(1), Forwardchars()); | ||
| char ch = (char)Operand(0); | ||
| int i; | ||
| int tempMaxBacktrackPosition = runtextpos; | ||
|
|
||
| if (!_rightToLeft && !_caseInsensitive) | ||
| { | ||
|
|
@@ -1217,6 +1233,7 @@ protected override void Go() | |
| if (len > i && _operator == RegexCode.Notoneloop) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder whether this should also happen for |
||
| { | ||
| TrackPush(len - i - 1, runtextpos - Bump()); | ||
| _maxBacktrackPosition = tempMaxBacktrackPosition; | ||
stephentoub marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| advance = 2; | ||
|
|
@@ -1261,6 +1278,16 @@ protected override void Go() | |
| { | ||
| int i = TrackPeek(); | ||
| int pos = TrackPeek(1); | ||
| if (!_caseInsensitive && _maxBacktrackPosition != -1 && pos > _maxBacktrackPosition && runtextpos < pos && _operator == (RegexCode.Notoneloop | RegexCode.Back) && !_rightToLeft) | ||
| { | ||
| // The Multi node has bumped us along already | ||
| int difference = pos - _maxBacktrackPosition; | ||
| Debug.Assert(difference > 0); | ||
| pos = runtextpos; | ||
| i -= difference; | ||
| // We shouldn't be backtracking anymore. | ||
| _maxBacktrackPosition = -1; | ||
| } | ||
| runtextpos = pos; | ||
| if (i > 0) | ||
| { | ||
|
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.