[regexp] Analyze RegExp to determine longest required literal - #501
Merged
Conversation
Hans-Halverson
force-pushed
the
regexp-literal-filter-analysis
branch
2 times, most recently
from
August 11, 2026 21:17
961f09c to
5f88eaf
Compare
Hans-Halverson
force-pushed
the
regexp-literal-filter-analysis
branch
from
August 11, 2026 21:27
5f88eaf to
5f556e3
Compare
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
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
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
RequiredLiteralFilteris stored on the compiled RegExp and will be used to optimize matching in an upcoming PR.Note that we track
may_contain_stringson 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