-
Notifications
You must be signed in to change notification settings - Fork 507
Pre-allocate the buffer based on the expected Content-Length with the Rust HTTP client
#19498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MadLittleMods
merged 6 commits into
develop
from
madlittlemods/rust-pre-allocate-reseponse-buffer
Feb 27, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1120e2e
Pre-allocate the buffer based on the expected `Content-Length` with t…
MadLittleMods d61e217
Add changelog
MadLittleMods 2ca3ca3
Fix lints
MadLittleMods 107968e
Merge branch 'develop' into madlittlemods/rust-pre-allocate-reseponse…
MadLittleMods 9ccc7fc
Better comments
MadLittleMods e9e75ba
Use `response.headers().typed_get::<headers::ContentLength>()`
MadLittleMods File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Pre-allocate the buffer based on the expected `Content-Length` with the Rust HTTP client. |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am a bit sceptical of that manual chunk aggregation though.
Fine with making it better in another PR, but we could:
axum::Body::from(response)http_body_util::Limited::new(body, limit)limited_body.collect().awaitbytes::Bytespyo3to support its conversionEach of those steps are supposedly quite efficient: collecting the body in a list of buffers is done by moving references, aggregating that to a contiguous buffer pre-allocates the right size, and the conversion from
bytes::BytestoPyBytesis also done without copy.Another thing we could make better, is that if we know that we're consuming it as a string, we may want to do the bytes->str conversion in Rust-land? Even one step further, what if we did the JSON parsing on the Rust side with something like
pythonize?EDIT: I've done some benchmarking: https://gitlab.element.io/quenting/pyo3-json-benchmark
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previous discussion on using
http_body_util::Limited#18357 (comment)Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds like a good improvement 👍 I've tackled the first part of this in #19510
Only weird thing I noticed in your description was:
As the docs actually say this:
But that sounds just as equivalent to before ⏩
We can explore the JSON stuff further once we have some better traces to nail down whether that's the problem.
For example, if I recall correctly, that trace example in the PR description is from a single event (not big JSON parsing)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's still go for merging this PR even though #19510 supersedes it after