Reduce unnecessary Regex match attempts for expressions beginning with atomic loops#35824
Reduce unnecessary Regex match attempts for expressions beginning with atomic loops#35824danmoseley merged 2 commits intodotnet:masterfrom
Conversation
|
Tagging subscribers to this area: @eerhardt |
|
Gosh that was a good trick! Should we add a perf test to protect this change? I don't have an intuition how commonly it would apply. BTW this issue jumped out of the debug output when running the benchmark |
...libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexCompiler.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexNode.cs
Outdated
Show resolved
Hide resolved
|
What is the state of the regex perf tests? Do you run them/ track them, or should we? Is there a test eg that would show if this change regressed? I recall I added a very ad-hoc test some time ago. |
https://github.com/dotnet/performance/blob/master/src/benchmarks/micro/libraries/System.Text.RegularExpressions/Perf.Regex.Common.cs
When they're relevant.
There are two tests that start with a relevant loop. One completes the loop frequently enough that it helps:
The other does not:
|
Optimize Regex expressions that begin with atomic unbounded loops (either as written by the dev or because the system detected the loop could be made atomic and did it implicitly) by updating the starting position in the scan loop for the next iteration to be where the loop ended rather than where it started.
Running the examples from https://github.com/mariomka/regex-benchmark/blob/652d55810691ad88e1c2292a2646d301d3928903/csharp/Benchmark.cs#L20-L26:
Before (RegexOptions.None):
After (RegexOptions.None):
Before (RegexOptions.ECMAScript):
After (RegexOptions.ECMAScript):
Before (RegexOptions.Compiled):
After (RegexOptions.Compiled):
Before (RegexOptions.Compiled | RegexOptions.ECMAScript):
After (RegexOptions.Compiled | RegexOptions.ECMAScript):
@danmosemsft, this is the optimization you and I discussed offline.
cc: @eerhardt, @pgovind