Change empty-match exit from break 2 to break#46
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds a regression test for the fail-fast behavior of PatternIterator when encountering an empty regex match. The test verifies that the break 2 statement (which exits both inner and outer loops) prevents unnecessary stream consumption when an empty match occurs mid-buffer, since loading more data cannot help resolve the situation.
Changes:
- Adds
testEmptyMatchFailsEarlyWithoutConsumingEntireStream()to verify that only 2 chunks are touched (1 processed + 1 prefetched) when an empty match triggers early exit, rather than draining all 4 chunks.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
The break 2 was harder to reason about and could incorrectly abort parsing when an empty match occurred mid-buffer with more stream data available. With break (single), the iterator loads the next chunk, which can resolve the empty match into a positive-length one (e.g. when a fixed-width alternative needs more characters than remain in the current buffer).
f744c98 to
1f1dc07
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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
break 2tobreakon empty match inPatternIterator::getIterator()for simpler control flow that is easier to reason about.testZeroLengthMatchMidBufferLoadsMoreData) demonstrating a case wherebreak 2would incorrectly abort: when an empty match occurs mid-buffer because a fixed-width alternative needs more characters than remain, loading the next chunk resolves it into a positive-length match.Context
With
break 2, an empty match mid-buffer with the stream still active would immediately exit both loops and throw. Withbreak, the iterator returns to the outer loop, loads the next chunk, and retries — which can turn the empty match into a successful one when more data is available.Since the library's SQL tokenizer patterns never produce empty matches in practice, the
break 2fail-fast offered no real benefit while being harder to reason about.Test plan
vendor/bin/tester tests/— all 65 tests pass