Skip to content

re2: fast path for trailing match-all in Match#607

Open
alkis wants to merge 1 commit intogoogle:mainfrom
alkis:strip-trailing-matchall
Open

re2: fast path for trailing match-all in Match#607
alkis wants to merge 1 commit intogoogle:mainfrom
alkis:strip-trailing-matchall

Conversation

@alkis
Copy link
Copy Markdown

@alkis alkis commented Mar 13, 2026

When a pattern ends with greedy .* DotNL in one line mode, compile
a second RE2 with the .* stripped. Match delegates to the stripped
RE2 and extends the results to cover all remaining text, avoiding O(n)
scanning of the trailing input.

The prefix RE2 is a completely independent object with its own Prog,
forward DFA, and reverse DFA. This avoids changing the engines as
it keeps the optimization at high level.

Fixes #605.

@alkis alkis force-pushed the strip-trailing-matchall branch 3 times, most recently from 0d1eab6 to 3193e98 Compare March 13, 2026 16:42
@alkis alkis changed the title re2: strip trailing match-all before compilation re2: fast path for trailing match-all in Match Mar 13, 2026
@alkis alkis force-pushed the strip-trailing-matchall branch 4 times, most recently from bc7239d to 51d4819 Compare March 14, 2026 11:01
When a pattern ends with greedy `.*` DotNL in one line mode, compile
a second RE2 with the `.*` stripped. `Match` delegates to the stripped
RE2 and extends the results to cover all remaining text, avoiding O(n)
scanning of the trailing input.

The prefix RE2 is a completely independent object with its own Prog,
forward DFA, and reverse DFA. This avoids changing the engines as
it keeps the optimization at high level.

Fixes google#605.
@alkis alkis force-pushed the strip-trailing-matchall branch from 51d4819 to e10d5f3 Compare March 14, 2026 11:06
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.

GlobalReplace with patterns ending in .*$ scans entire input through BitState unnecessarily

1 participant