Skip to content

Conversation

@stephentoub
Copy link
Member

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.

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.
Copy link
Contributor

Copilot AI left a 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

@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-text-regularexpressions
See info in area-owners.md if you want to be subscribed.

Copy link
Member

@MihaZupan MihaZupan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

source diffs

@stephentoub
Copy link
Member Author

/ba-g "due to space limitations"

@stephentoub stephentoub merged commit 17365a6 into dotnet:main Jul 21, 2025
79 of 88 checks passed
@stephentoub stephentoub deleted the moreregexsets branch July 21, 2025 12:36
@github-actions github-actions bot locked and limited conversation to collaborators Aug 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants