Skip to content

Send multipart form bodies in fewer chunks - #1322

Open
patrick-immometrica wants to merge 1 commit into
pydantic:mainfrom
patrick-immometrica:multipart-fewer-writes
Open

patrick-immometrica wants to merge 1 commit into
pydantic:mainfrom
patrick-immometrica:multipart-fewer-writes

Conversation

@patrick-immometrica

@patrick-immometrica patrick-immometrica commented Nov 8, 2026 •

Copy link
Copy Markdown

Summary

MultipartStream.iter_chunks() yields four small chunks per field (boundary, headers, value, \r\n). Each chunk turns into its own h11.Data event and its own send() call, so a form with 10 fields and one file makes ~46 writes per request.

This joins everything except file data into one chunk, so the file chunks still pass through without copies. The two render() methods aren't used anymore, so I removed them.

A POST with 10 short fields and a 2 KB file against benchmark/server.py on loopback (sync client, best of 5):

µs/req
main 790 - 1170
this PR 390 - 410

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

View guided diff

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No issues found across 2 files

View guided diff | Re-trigger cubic

@codspeed

codspeed Bot commented Nov 8, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 17 untouched benchmarks
⏩ 8 skipped benchmarks1


Comparing patrick-immometrica:multipart-fewer-writes (64c593a) with main (f4f65c1)

Open in CodSpeed

Footnotes

  1. 8 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

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