Skip to content

Improve regex derivative construction - #69530

Merged
stephentoub merged 33 commits into
dotnet:mainfrom
veanes:derivUpdates
May 25, 2022
Merged

stephentoub merged 33 commits into
dotnet:mainfrom
veanes:derivUpdates

Conversation

@veanes

@veanes veanes commented May 19, 2022

Copy link
Copy Markdown
Contributor

The initial trigger for this PR was to fix issue #60645.
There were several root causes behind the issue that surfaced through that stress test that are being fixed by this PR.

  1. The derivative computation was simplified to a more transparent algebraic recursive formulation that captures the backtracking semantics by maintaining correct order of alternatives when OrderedOr is constructed. This needed a separate flag IsHighPriorityNullable to be maintained and a method PruneLowerPriorityThanNullability that extracts a backtracking derivative from a full derivative when transitions are computed in Backtracking-semantics-preserving mode. This implied a reduction of several quadratic behaviors that were caused by the earlier explicit expansion of transitions. Also caching became both easier to maintain and understand in this setting.
  2. The mechanism of computing Effects was reformulated to use the updated derivative computation. This affected how the effects are propagated in the symbolic regex node AST and thus also affected how some of the simplification rules are triggered. Some work is remaining here to improve the way those rules are written, essentially to try to capture the algebraic laws more elegantly and to better separate the concern of checking if a subsumption holds while also simplifying the regex accordingly to avoid double work.
  3. More subsumption rules were added to reduce common cases of expansion that in some cases triggered quadratic (in the size of the regex) behavior and slow performance in the concatenation stress test in the 3rd phase when subcaptures are computed. The main culprit in these tests is deep nesting of sub-captures in combination with nullable patterns such as (a?(a?(a?(a?....)))) that create a lot of possible paths in the underlying NFA.
  4. Concatenation nodes are no longer normalized into right-associative form. This avoided a quadratic behavior in the length of the regex in certain cases, when the right-associative form was being recomputed every time a concatenation happened, e.g. avoiding turning (a(b(cd))))(e(fg)) into (a(b(c(d(e(fg))))))

Concretely, a typical subsumption rule (avoiding alternation) is that Y|XY reduces to X??Y if X is a nullable regex.
This PR contains commits from me as well as @olsaarik.

@ghost

ghost commented May 19, 2022

Copy link
Copy Markdown

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

Issue Details

The initial trigger for this PR was to fix issue #60645.
There were several root causes behind the issue that surfaced through that stress test that are being fixed by this PR.

  1. The derivative computation was simplified to a more transparent algebraic recursive formulation that captures the backtracking semantics by maintaining correct order of alternatives when OrderedOr is constructed. This needed a separate flag IsHighPriorityNullable to be maintained and a method PruneLowerPriorityThanNullability that extracts a backtracking derivative from a full derivative when transitions are computed in Backtracking-semantics-preserving mode. This implied a reduction of several quadratic behaviors that were caused by the earlier explicit expansion of transitions. Also caching became both easier to maintain and understand in this setting.
  2. The mechanism of computing Effects was reformulated to use the updated derivative computation. This affected how the effects are propagated in the symbolic regex node AST and thus also affected how some of the simplification rules are triggered. Some work is remaining here to improve the way those rules are written, essentially to try to capture the algebraic laws more elegantly and to better separate the concern of checking if a subsumption holds while also simplifying the regex accordingly to avoid double work.
  3. More subsumption rules were added to reduce common cases of expansion that in some cases triggered quadratic (in the size of the regex) behavior and slow performance in the concatenation stress test in the 3rd phase when subcaptures are computed. The main culprit in these tests is deep nesting of sub-captures in combination with nullable patterns such as (a?(a?(a?(a?....)))) that create a lot of possible paths in the underlying NFA.
  4. Concatenation nodes are no longer normalized into right-associative form. This avoided a quadratic behavior in the length of the regex in certain cases, when the right-associative form was being recomputed every time a concatenation happened, e.g. avoiding turning (a(b(cd))))(e(fg)) into (a(b(c(d(e(fg))))))

Concretely, a typical subsumption rule (avoiding alternation) is that Y|XY reduces to X??Y if X is a nullable regex.
This PR contains commits from me as well as @olsaarik.

Author: veanes
Assignees: -
Labels:

area-System.Text.RegularExpressions

Milestone: -

@olsaarik

Copy link
Copy Markdown
Contributor

I didn't finish commenting everything I should. Will continue tomorrow morning.

@olsaarik

olsaarik commented May 20, 2022 •

Copy link
Copy Markdown
Contributor

I think the state of comments for all the new code is relatively good now. Feel free to review!

@stephentoub stephentoub changed the title Derivative updates Improve regex derivative construction May 20, 2022
@veanes

veanes commented May 20, 2022

Copy link
Copy Markdown
Contributor Author

I'm planning to go through all the comments, want to do it now, ... but have a couple of meetings in the way. Back soon.

@olsaarik

Copy link
Copy Markdown
Contributor

/azp run runtime-libraries-coreclr outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@olsaarik

Copy link
Copy Markdown
Contributor

Outerloop tests look good; the stress tests are indeed passing now. The only regex thing popping up now is System.Text.RegularExpressions.Tests.RegexIgnoreCaseTests.Unicode_IgnoreCase_Tests, which seems to be failing for all engines.

@veanes

veanes commented May 23, 2022

Copy link
Copy Markdown
Contributor Author

All comments seem to have been addressed at this point. I am sure there will be follow-up improvements going forward (e.g. more and improved subsumption checks, triggered by more evaluations and tests). But I think these belong to future PRs.

@joperezr

Copy link
Copy Markdown
Member

erloop tests look good; the stress tests are indeed passing now. The only regex thing popping up now is System.Text.RegularExpressions.Tests.RegexIgnoreCaseTests.Unicode_IgnoreCase_Tests, which seems to be failing for all engines.

Yes, that one is on me. I just opened #69692 to disable those tests for now.

@stephentoub stephentoub left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. There are some minor things we'll want to clean up further, but I'm going to merge to get it in for Preview 5, and to unblock subsequent work.

@stephentoub
stephentoub merged commit e656304 into dotnet:main May 25, 2022
@veanes

veanes commented May 25, 2022

Copy link
Copy Markdown
Contributor Author

Sounds great! I'm continuing with the nfa-size-limit-PR, it will be great to work against the updated main, (getting this into main will have to wait for the next round but I'll push a PR for it soon, it will be accompanied by unit tests, that are important in this case for validation. Jose showed me how to set this up.)

@veanes
veanes deleted the derivUpdates branch May 25, 2022 20:10
@ghost ghost locked as resolved and limited conversation to collaborators Jun 25, 2022
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.

4 participants