Skip to content

parsers: limit char-class repetition scanning to max - #28

Merged
rameel merged 1 commit into
mainfrom
fix-repeat-max-scan
Sep 22, 2026
Merged

rameel merged 1 commit into
mainfrom
fix-repeat-max-scan

Conversation

@rameel

@rameel rameel commented Sep 22, 2026 •

Copy link
Copy Markdown
Owner

Char-class repetition scanned the entire matching prefix before clamping the consumed count to max. With a small limit and a long matching input, this performed O(n) scanning instead of O(min(n, max)).

For example, Set("a").Repeat(1, 1) scanned all one million characters in new string('a', 1_000_000) despite consuming only one.

Fix:

Input length Before After
1,000 21.274 ns 8.6 ns
1,000,000 21.187 ns 8.9 ns

Parsing time no longer grows with the matching input beyond max.

Limit the input span before scanning repeated character classes.

Previously the entire matching prefix was scanned before the count was
clamped to max, so even a single repetition could scan the whole input.
@rameel
rameel merged commit eb37711 into main Sep 22, 2026
2 checks passed
@rameel
rameel deleted the fix-repeat-max-scan branch September 22, 2026 21:25
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