Repository navigation
fix: FindAll/Count O(n²) for $-anchored patterns - #185
Merged
Merged
Conversation
UseReverseAnchored was missing from findIndicesAtWithState switch, causing FindAll to fall through to PikeVM loop: O(n) calls × O(n) each. Fix: short-circuit in findAllIndicesLoop and Count for UseReverseAnchored. A $-anchored pattern without (?m) matches at most once — call the reverse searcher once and stop. Match/Find were already correct (single reverse scan). Tests: 5 correctness cases vs stdlib, linear-time verification on 2400 lines, multiple-candidate test (3 'ERROR' occurrences, stdlib returns 1 match at end). Fixes #183
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Benchmark ComparisonComparing Summary:
|
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.
Summary
FindAll/Countfor$-anchored patterns were O(n²) — each iteration ran PikeVM from successive positions to end of input.Root cause:
UseReverseAnchoredwas missing fromfindIndicesAtWithStateswitch, falling through to NFA.Match/Findwere correct (single reverse scan viafindIndicesReverseAnchored).Fix: short-circuit in
findAllIndicesLoopandCount— a$-anchored pattern without(?m)matches at most once. One reverse searcher call, at most one result, no loop.ERROR.*$on 2400-line log: 3.3 s → instant.Test plan
Fixes #183