Repository navigation
Conversation
Demonstrates that the http crate is viable for allocation-sensitive clients: a mocked HTTP/1.1 round trip with 10 headers and 5 KiB bodies each way, received in 3-byte chunks, costs 2 heap allocations versus 89 for a naive client. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…lient example Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #788 +/- ##
==========================================
Coverage 100.0% 100.0%
==========================================
Files 738 85 -653
Lines 98623 5446 -93177
==========================================
- Hits 98623 5446 -93177
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Adds
examples/efficient_client.rstohttp_extensions. It shows that thehttpcrate is viable for allocation-sensitive clients: used deliberately, a full request/response round trip costs one small heap allocation.Scenario
A mocked HTTP/1.1 client sends a request and receives a response. Each carries 10 headers and a 5 KiB body. The mocked connection delivers the response in 3-byte chunks. All byte data lives in
GlobalPoolmemory managed throughBytesBuf/BytesView: the payload, the outgoing request, the receive buffer and the canned response.Techniques
BytesBuf, and the request is written into pooled memory. The request bodyBytesViewis appended without copying.Bytes, and each header value is a slice of it created withHeaderValue::from_maybe_shared, so nothing is copied. Long-lived request values (authorization, correlation ID) are shared the same way. Static values usefrom_static.HeaderMapreuse. The client splits the request into parts withinto_parts()and clears the request'sHeaderMapto hold the response headers. The caller then passes that map into the next request, so the map is allocated once.BytesViewover the receive buffer.Report
Measured with
alloc_trackerover 1,000 requests after a 100-request warm-up (cargo run -p http_extensions --example efficient_client --release):The one remaining allocation (40 bytes) is the shared owner that
BytesView::to_bytes()creates to wrap the pooled response head asBytes. That's the only storageHeaderValueaccepts. All 10 response header values reference this one owner.The naive client uses the common convenience APIs (
Request::builder()with string headers,format!,Vec<u8>buffers,HeaderValue::from_str), for comparison.