Skip to content

[regexp] Analyze RegExp to determine longest required literal - #501

Merged
Hans-Halverson merged 1 commit into
masterfrom
regexp-literal-filter-analysis
Aug 11, 2026
Merged

[regexp] Analyze RegExp to determine longest required literal#501
Hans-Halverson merged 1 commit into
masterfrom
regexp-literal-filter-analysis

Conversation

@Hans-Halverson

@Hans-Halverson Hans-Halverson commented Aug 11, 2026

Copy link
Copy Markdown
Owner

Summary

Introduce a new analysis pass for RegExps that determines the best required literal that must be present in the input for it to match the RegExp. The required literal must be a sequence of 3-16 Latin1 code points that are search for directly, i.e. we don't support case variants in case insensitive mode. The required literal also tracks its minimum and optional maximum offset from the start of a match, which will be used to narrow down possible match start positions.

This analysis pass is performed in the new RequiredLiteralAnalyzer, which tracks the best (aka longest, bounded) searchable literal found along with its offsets. This analysis must track the min/max widths of every subpattern before the literal in order to determine the literal min/max offsets.

The resulting RequiredLiteralFilter is stored on the compiled RegExp and will be used to optimize matching in an upcoming PR.

Note that we track may_contain_strings on the RegExp AST now and use it during this new analysis. We already had this information during parsing, we just need to save it.

Seeing a ~1.4% regression on the Octane RegExp benchmark due to the added analysis. This is more than made up for by the ~60% increase to this benchmark once the search is added.

Tests

  • Added RegExp bytecode snapshot tests verifying the result of the required literal analysis

@Hans-Halverson
Hans-Halverson force-pushed the regexp-literal-filter-analysis branch 2 times, most recently from 961f09c to 5f88eaf Compare August 11, 2026 21:17
@Hans-Halverson
Hans-Halverson force-pushed the regexp-literal-filter-analysis branch from 5f88eaf to 5f556e3 Compare August 11, 2026 21:27
@Hans-Halverson
Hans-Halverson merged commit bdb1c74 into master Aug 11, 2026
11 checks passed
@Hans-Halverson
Hans-Halverson deleted the regexp-literal-filter-analysis branch August 11, 2026 21:34
Hans-Halverson added a commit that referenced this pull request Aug 12, 2026
## Summary

Use the `RequiredLiteralFilter` introduced in
#501 during matching.
This filter allows us to 1) filter out any inputs that never contain the
required literal and 2) if the literal has a known bound from the start
of the match, restricts match start positions that need to be checked.
Is used in combination with the `MatchStartFilter` in order to further
optimize where the backtracking matcher even needs to be run.

Notes:
- Required literal search is specialized for the "matches can only
appear at the start" case by restricting search to bounded window, if
possible.
- We must conservatively widen the search window in
`HeapTwoByteCodePointLexerStream` to account for code points that take
two code units.

Seeing a massive +62.6% increase to Octane RegExp benchmark score from
this change, an a +3.3% overall improvement to Octane score.

## Tests

- Added LLM-generated integration tests exercising the required literal
filter in a variety of situations
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.

1 participant