Skip to content

Simplify: bound repeat expansion by the program-size limit - #696

Open
Mamat9q wants to merge 1 commit into
google:mainfrom
Mamat9q:simplify-expansion-budget
Open

Mamat9q wants to merge 1 commit into
google:mainfrom
Mamat9q:simplify-expansion-budget

Conversation

@Mamat9q

@Mamat9q Mamat9q commented Nov 19, 2026

Copy link
Copy Markdown

Expanding a counted repetition materialises one copy of the subexpression per repetition before any program-size limit is consulted. A pattern such as a{1000} repeated 30000 times coalesces into a single {30000000,30000000} repeat, and Simplify then allocates ~500 MB expanding it, even though the pattern can never compile: the program-size limit only applies later, in the compiler. max_mem does not help either, for the same reason.

Give Simplify() an optional expansion budget, counted in copies materialised while expanding repetitions, and fail the simplification (returning NULL, as the compiler already handles) once it would be exceeded. The compiler passes its own program-size limit, so patterns that could never compile are now rejected before the expansion allocates, while patterns that compile today keep compiling: each copy compiles to roughly one instruction, so the budget matches the limit it derives from.

Before/after (pattern a{0,1000} repeated N times, peak RSS):
N=600: 38.0 MB -> 37.9 MB (still compiles, unchanged)
N=700: 26.9 MB -> 3.8 KB (rejected fast)
N=10000: 162 MB -> 5.6 KB
N=30000: 478 MB, 2.21 s -> 9.0 KB, 0.01 s
A general-case variant (a{0,1000} x30000) went from ~3.8 GB peak RSS to 9.0 KB. All re2 tests pass, including under ASan.

@google-cla

google-cla Bot commented Nov 19, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

This branch has not been deployed

No deployments
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