Repository navigation
Make the regexp analyzer's POSIX named class lookup linear - #128394
Open
TheNetherWatcher wants to merge 2 commits into
Open
TheNetherWatcher wants to merge 2 commits into
TheNetherWatcher wants to merge 2 commits into
Conversation
Inside a character class, every `[:` made skipPosixNamedClass scan the rest of the pattern for `:]`. With no `:]` left, each scan ran to the end, so analysis was quadratic. Find the last `:]` once and skip the scan for any `[:` after it.
Contributor
|
Workflow [PR], commit [a957efa] Summary: ❌
AI ReviewSummaryThis PR removes quadratic searches for a missing Final VerdictStatus: ✅ Approve |
clang-tidy flagged regexp_.data() once the call was split over several lines, because the NOLINT stayed on the first one. Take the pointer once and keep the call on one line. The valid-class queries timed out in the fast test: re2 accepts those patterns and its own MaybeParsePosixClass also scans for :] at every [:, so building the class is quadratic in re2 regardless of the analyzer. Use 1000 [: there; the 250000 run stays in the first query, which re2 rejects at the leading *.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Inside a character class, every
[:makesskipPosixNamedClasslook for a closing:], and that scan runs over the whole rest of the pattern. When no:]is left, it always runs to the end, so a pattern with many[:and no:]made the analysis quadratic:SELECT match(materialize('x'), concat('*[', repeat('[:', 250000), 'x]'))spends ~34 s before re2 is even built.A failed scan stays failed for every later
[:, so this finds the last:]once withrfindand passes it down. A[:after it returnspos + 1right away, which is what the scan would have returned. When the scan does run, it is guaranteed to find a:], and the analyzer then continues after it, so each character is scanned at most once.Timing of the analyzer's walk over the issue's pattern (old vs new function,
-O2):[:countTesting:
05473_regexp_posix_class_scan_linearwith the query from the issue, a valid class with the same[:run, a named class before the run, and a:]after it. I checked the expected answers against re2 directly.[of 300,000 random patterns over[,:,],a,^: no differences.Closes: #119581
Related: #118342
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed quadratic time spent analyzing a regular expression whose character class contains many
[:and no closing:]. For example,matchwith 250,000 of them took about 34 seconds before reporting the pattern as invalid.Workflow [PR]
Sync PR [sync-upstream/pr/128394]