-
Couldn't load subscription status.
- Fork 5.2k
Teach regex about more disjoint sets #117869
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
Conversation
The regex optimizer tries to make loops atomic whenever it's safe to do so, as doing so removes possible backtracking. When it finds a set loop followed by another set, to be able to make the loop atomic, it needs to prove that the sets are disjoint, that there's nothing that can match both sets. It already does so for a wide variety of cases, but it's missing logic today that helps it to determine that for sets composed only of Unicode categories, e.g. \d, \w, p{C}, etc. This adds that in.
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.
Pull Request Overview
This PR enhances the regex optimizer to better identify disjoint sets when dealing with Unicode categories, enabling more atomic loops and reducing backtracking. The key improvement is adding logic to recognize when sets composed of Unicode categories (like \d, \w, \p{C}) don't overlap, allowing the optimizer to make loops atomic in more cases.
Key changes:
- Added Unicode category analysis to determine set disjointness
- Enhanced known distinct sets detection with more comprehensive pattern matching
- Added comprehensive test coverage for the new optimization scenarios
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| RegexCharClass.cs | Added TryGetOnlyCategories logic to analyze Unicode category composition and enhanced KnownDistinctSets method |
| RegexReductionTests.cs | Added test cases for atomic loop optimization with Unicode category patterns |
...ibraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexCharClass.cs
Show resolved
Hide resolved
...ibraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexCharClass.cs
Show resolved
Hide resolved
|
Tagging subscribers to this area: @dotnet/area-system-text-regularexpressions |
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.
Nice!
|
/ba-g "due to space limitations" |
The regex optimizer tries to make loops atomic whenever it's safe to do so, as doing so removes possible backtracking. When it finds a set loop followed by another set, to be able to make the loop atomic, it needs to prove that the sets are disjoint, that there's nothing that can match both sets. It already does so for a wide variety of cases, but it's missing logic today that helps it to determine that for sets composed only of Unicode categories, e.g. \d, \w, p{C}, etc. This adds that in.