-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Eliminate backtracking in the One node for some regex patterns
#51883
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
62eb983
First cut of look up table for speeding up Go()
8e09128
More efficient .* in RegexInterpreter
c8f3778
sq
f783ccf
Get more debug info
77787bc
Remove assert and add unit test
2a5df2c
Backtracking optimization for OneNode
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
| 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) | ||
| { | ||
|
|
@@ -214,6 +215,23 @@ private char Forwardcharnext() | |
| return _caseInsensitive ? _textInfo.ToLower(ch) : ch; | ||
| } | ||
|
|
||
| private void OptimizeRuntextposBacktracking(string str) | ||
| { | ||
| // If 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 (_maxBacktrackPosition != -1 && runtextpos > _maxBacktrackPosition) | ||
| { | ||
| // If lastIndexOf is -1, we backtrack to the max extent possible. | ||
| runtextpos = _maxBacktrackPosition; | ||
| ReadOnlySpan<char> runtextSpan = runtext.AsSpan(_maxBacktrackPosition); | ||
| int lastIndexOf = runtextSpan.LastIndexOf(str); | ||
| if (lastIndexOf > -1) | ||
| { | ||
| // Found the next position to match. Move runtextpos here | ||
| runtextpos = _maxBacktrackPosition + lastIndexOf; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private bool MatchString(string str) | ||
| { | ||
| int c = str.Length; | ||
|
|
@@ -223,6 +241,7 @@ private bool MatchString(string str) | |
| { | ||
| if (runtextend - runtextpos < c) | ||
| { | ||
| OptimizeRuntextposBacktracking(str); | ||
| return false; | ||
| } | ||
|
|
||
|
|
@@ -1026,8 +1045,10 @@ protected override void Go() | |
| continue; | ||
|
|
||
| case RegexCode.One: | ||
| if (Forwardchars() < 1 || Forwardcharnext() != (char)Operand(0)) | ||
| char chOne = (char)Operand(0); | ||
| if (Forwardchars() < 1 || Forwardcharnext() != chOne) | ||
| { | ||
| OptimizeRuntextposBacktracking(chOne.ToString()); | ||
| break; | ||
| } | ||
| advance = 1; | ||
|
|
@@ -1185,6 +1206,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 +1239,8 @@ protected override void Go() | |
| if (len > i && _operator == RegexCode.Notoneloop) | ||
| { | ||
| TrackPush(len - i - 1, runtextpos - Bump()); | ||
| Debug.Assert(_maxBacktrackPosition == -1, $"maxBacktrackPosition = {_maxBacktrackPosition}, runtext = {runtext}, runtextpos = {runtextpos}, ch = {ch}, code = {_code}, runregex = {runregex}"); | ||
|
Author
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. Any assert failures stemming from this line are potential optimizations |
||
| _maxBacktrackPosition = tempMaxBacktrackPosition; | ||
| } | ||
| } | ||
| advance = 2; | ||
|
|
@@ -1261,6 +1285,16 @@ protected override void Go() | |
| { | ||
| int i = TrackPeek(); | ||
| int pos = TrackPeek(1); | ||
| if (_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) | ||
| { | ||
|
|
||
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
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.
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.
Same questions as I had on the previous PR.