Fixed some cases of quadratic worst-case runtime #2922
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This fixes a few of the cases I described in #2597.
Basically, the regex engine moves the regexes over the string from left to right and is typically forced to check each of the n suffixes for a string of length n. There are various optimizations employed by the regex engine that reduces the number of suffixes that have to be tested but it's usually still linear, O(n). If the regex engine then also takes O(n) steps to reject each of those suffixes, we get a worst-case runtime of O(n^2).
The fixes in this PR also have the same strategy: reject suffixes in O(1). So all rejecting suffixes will be rejected in (on average) O(1).
I have to note that this PR fixes not all of these cases, by far. I only fixed the cases that were easy to fix.
All of the changed regexes still behave the same in the context of Prism's matching algorithm.
The only exception is JS stack trace where I added an additional lookbehind group as part of the fix. Hence the changed test case.