Replace per-byte partial-boundary scan with rfind lookbehind#300
Merged
Conversation
When `find` cannot locate a whole boundary in part data, the tail may hold a partial boundary that completes in the next chunk. The old fallback seeded the per-byte matcher at the tail, which thrashed on CR/LF-dense bodies: `\r\n` matches the boundary prefix, fails at `-`, resets, reconsiders, and re-runs `find` for every `\r\n` pair near the chunk end. Since `self.boundary` is `\r\n--` + boundary and an RFC boundary cannot contain CR, the last CR in the tail is the only candidate prefix start. Anchor on it with `rfind`, confirm with `startswith`, carry the partial via `index`, and let the existing end-of-chunk flush emit the data and re-mark the lookbehind. This mirrors multer's lookbehind strategy. worstcase_crlf goes from ~2700 to ~18500 MB/s (6.9x), reaching parity with the LF case; LF, boundary-char, and file-upload paths are unchanged. Verified behaviorally identical to the previous parser across 82k differential comparisons (every chunk-split strategy incl. byte-by-byte, with the boundary edge landing on every offset).
Owner
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. 👍 ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
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.
What
When
findcannot locate a whole boundary in part data, the tail may hold a partial boundary that completes in the next chunk. The old fallback seeded the per-byte matcher at the tail, which thrashed on CR/LF-dense bodies:\r\nmatches the boundary prefix, fails at-, resets, reconsiders, and re-runsfindfor every\r\npair near the chunk end.Since
self.boundaryis\r\n--+ boundary and an RFC boundary cannot contain CR, the last CR in the tail is the only candidate prefix start. We anchor on it withrfind, confirm withstartswith, carry the partial viaindex, and let the existing end-of-chunk flush emit the data and re-mark the lookbehind. This mirrors multer's lookbehind strategy.Impact
Benchmarked against
multipart_bench(1MB part body, 64KB chunks, sansio variant):worstcase_crlfwas the slowest scenario across all pure-Python parsers; it is now the fastest, reaching parity with the LF case.Correctness
Verified behaviorally identical to the previous parser across 82k differential comparisons of the full callback-event stream and error type/offset: every chunk-split strategy (whole, byte-by-byte, fixed-size, random) plus an exhaustive two-chunk sweep with the boundary edge landing on every byte offset. Zero mismatches.
Adds a CRLF-dense corpus case (
crlf_dense_part_data) to the data-driven test suite, run both whole-write and byte-by-byte. 100% coverage maintained.AI Disclaimer
This PR was developed with the assistance of either Claude or Codex. I've reviewed and verified the changes.