Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Replace per-byte partial-boundary scan with rfind lookbehind#300

Merged
Kludex merged 1 commit into
mainfrom
speed-up-crlf-dense-part-data
Jun 4, 2026
Merged

Replace per-byte partial-boundary scan with rfind lookbehind#300
Kludex merged 1 commit into
mainfrom
speed-up-crlf-dense-part-data

Conversation

@Kludex

@Kludex Kludex commented Jun 4, 2026

Copy link
Copy Markdown
Owner

What

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. We 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.

Impact

Benchmarked against multipart_bench (1MB part body, 64KB chunks, sansio variant):

Scenario before after
worstcase_crlf ~2700 MB/s ~18500 MB/s (6.9x)
worstcase_lf ~21500 MB/s unchanged
worstcase_bchar ~16400 MB/s unchanged
file upload unchanged unchanged

worstcase_crlf was 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.

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).
@codspeed-hq

codspeed-hq Bot commented Jun 4, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 5 untouched benchmarks


Comparing speed-up-crlf-dense-part-data (12cbd2b) with main (4cffc68)

Open in CodSpeed

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 4 files

Re-trigger cubic

@Kludex

Kludex commented Jun 4, 2026

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@Kludex Kludex merged commit 8672979 into main Jun 4, 2026
15 checks passed
@Kludex Kludex deleted the speed-up-crlf-dense-part-data branch June 4, 2026 15:42
@Kludex Kludex mentioned this pull request Jun 4, 2026
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