Skip to content

fix: FindAll/Count O(n²) for $-anchored patterns - #185

Merged
kolkov merged 2 commits into
mainfrom
feature/issue-183-reverse-anchored-findall
Oct 9, 2026
Merged

kolkov merged 2 commits into
mainfrom
feature/issue-183-reverse-anchored-findall

Conversation

@kolkov

@kolkov kolkov commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Summary

FindAll/Count for $-anchored patterns were O(n²) — each iteration ran PikeVM from successive positions to end of input.

Root cause: UseReverseAnchored was missing from findIndicesAtWithState switch, falling through to NFA. Match/Find were correct (single reverse scan via findIndicesReverseAnchored).

Fix: short-circuit in findAllIndicesLoop and Count — 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

  • 5 correctness cases vs stdlib (match, no-match, case-insensitive, single-line)
  • Multiple-candidate test: 3 "ERROR" occurrences, stdlib returns 1 match at end
  • Linear-time verification on 2400 lines (would be 3.3 s if quadratic)
  • Full suite green, lint clean
  • regex-bench CI: 4/4 green

Fixes #183

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

codecov Bot commented Oct 9, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown

Benchmark Comparison

Comparing main → PR #185

Summary: geomean 104.6n 105.1n +0.46%

⚠️ Potential regressions detected:

geomean                    104.6n         105.1n        +0.46%
geomean                               ³                +0.00%               ³
geomean                               ³                +0.00%               ³
geomean                         ³                +0.00%               ³
geomean                         ³                +0.00%               ³
AnchoredLiteralVsStdlib/coregex_long-4                  9.131n ± ∞ ¹    9.343n ± ∞ ¹    +2.32% (p=0.008 n=5)
ASCIIOptimization_Issue79/medium_WithASCII-4            630.6n ± ∞ ¹    644.2n ± ∞ ¹    +2.16% (p=0.008 n=5)
LangArenaLogParser/post_requests-4                      1.181m ± ∞ ¹    1.183m ± ∞ ¹    +0.19% (p=0.016 n=5)
LangArenaLogParser/auth_attempts-4                      808.1µ ± ∞ ¹    812.5µ ± ∞ ¹    +0.55% (p=0.032 n=5)
LangArenaLogParser/sessions-4                           482.1µ ± ∞ ¹    486.8µ ± ∞ ¹    +0.96% (p=0.008 n=5)

Full results available in workflow artifacts. CI runners have ~10-20% variance.
For accurate benchmarks, run locally: ./scripts/bench.sh --compare

@kolkov
kolkov merged commit 94b20cd into main Oct 9, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: FindAll/Count O(n²) for $-anchored patterns (UseReverseAnchored bypassed)

1 participant