Skip to content

fixes in transition blowup and removing recursion from RegexNode conversion - #68307

Merged
veanes merged 6 commits into
dotnet:mainfrom
veanes:fixIssue60645
Apr 22, 2022
Merged

veanes merged 6 commits into
dotnet:mainfrom
veanes:fixIssue60645

Conversation

@veanes

@veanes veanes commented Apr 21, 2022

Copy link
Copy Markdown
Contributor

Partially addressing active issue #60645.
The issue is not fully fixed yet after this pr since more related performance issues are still present.
But at least three key fixes are made, so the PR made sense at this time.

  1. Removing quadratic increase in transition creation, through TransitionList data structure in AddTransitions method.

  2. Removing deep recursion in ConvertToSymbolicRegexNode(RegexNode) that turned out to be a bottleneck in many stress tests leading to calls of StackHelper.CallOnEmptyStack and construction times in the order of 10sec that was reduced 100x, by rewriting the method with a while loop and local cache of intermediate results.

  3. Added DoublyLinkedList data structure in order to construct lists of symbolic regex nodes that can be extended both at the start and at the end, as well as append to in O(1) cost, and allows walking the list forwards and backwards linearly in the length of the list. It avoids locally creating multiple creations of intermediate transition arrays and mainly avoids deep nesting of concatenations involving captures.

The use of DoublyLinkedList in ConvertToSymbolicRegexNode relies on the property that
no RegexNode object is repeated in the AST
which must be true, since any two different occurrences of RegexNode object must also be distinct references. Corresponding Debug.Assert calls are also made to make sure that this is indeed the case.

@ghost

ghost commented Apr 21, 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

Partially addressing active issue #60645.
The issue is not fully fixed yet after this pr since more related performance issues are still present.
But at least three key fixes are made, so the PR made sense at this time.

  1. Removing quadratic increase in transition creation, through TransitionList data structure in AddTransitions method.

  2. Removing deep recursion in ConvertToSymbolicRegexNode(RegexNode) that turned out to be a bottleneck in many stress tests leading to calls of StackHelper.CallOnEmptyStack and construction times in the order of 10sec that was reduced 100x, by rewriting the method with a while loop and local cache of intermediate results.

  3. Added DoublyLinkedList data structure in order to construct lists of symbolic regex nodes that can be extended both at the start and at the end, as well as append to in O(1) cost, and allows walking the list forwards and backwards linearly in the length of the list. It avoids locally creating multiple creations of intermediate transition arrays and mainly avoids deep nesting of concatenations involving captures.

The use of DoublyLinkedList in ConvertToSymbolicRegexNode relies on the property that
no RegexNode object is repeated in the AST
which must be true, since any two different occurrences of RegexNode object must also be distinct references. Corresponding Debug.Assert calls are also made to make sure that this is indeed the case.

Author: veanes
Assignees: veanes
Labels:

area-System.Text.RegularExpressions

Milestone: -

@veanes

veanes commented Apr 22, 2022

Copy link
Copy Markdown
Contributor Author

The last commit fixes the above comments.
(Recall also that the original comment above concerning uniqueness of RegexNode objects in the original parse tree is no longer relevant or required.)

@veanes

veanes commented Apr 22, 2022

Copy link
Copy Markdown
Contributor Author

I think this is good point in time to merge this PR into main, if the fixes I did look fine, and no more comments remain.
Since @olsaarik is working on adding caching to derivatives, that should then hopefully fix issue #60645, he will need these fixes to avoid running into the prior bottlenecks, so this is on a critical path for him.

@olsaarik olsaarik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good to me!

@veanes
veanes merged commit 047b72f into dotnet:main Apr 22, 2022
@veanes
veanes deleted the fixIssue60645 branch April 22, 2022 21:54
}
}

public override string ToString()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can this ToString be debug only?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes

public Node(T elem, Node? prev, Node? next) { _elem = elem; _prev = prev; _next = next; }
}

public override string ToString()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can this ToSTring be debug only?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Indeed, both ToString()'s are used for debugging purposes only, forgot to mark them as such.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I could do a quick follow-up fix PR, unless someone else takes care of this inside another PR, as this is a trivial change that perhaps does not warrant a separate PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Well, I'll remember to do it in my upcoming PR (regarding upper bound measuring of NFA states).

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