-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Optimize small HTTP requests/responses by coalescing headers and body into a single packet #10991
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
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #10991 +/- ##
==========================================
+ Coverage 98.77% 98.78% +0.01%
==========================================
Files 129 129
Lines 39624 40078 +454
Branches 2187 2204 +17
==========================================
+ Hits 39137 39591 +454
Misses 339 339
Partials 148 148
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
CodSpeed Performance ReportMerging #10991 will improve performances by 8.62%Comparing Summary
Benchmarks breakdown
|
Backport to 3.12: 💔 cherry-picking failed — conflicts found❌ Failed to cleanly apply 452458a on top of patchback/backports/3.12/452458a959814ef7bf67860f49606e5ed4649af9/pr-10991 Backporting merged PR #10991 into master
🤖 @patchback |
…nses by coalescing headers and body into a single packet (#10992)
continued from #10976 (comment)
Notes
This won't show up any different on codspeed since it excludes syscalls.
Summary
This PR optimizes network efficiency for small HTTP requests and responses by coalescing headers and body data into a single TCP packet when the combined size is small. This reduces the number of packets sent over the network, improving latency and reducing overhead.
Most importantly, this fixes compatibility with memory-constrained IoT devices that can only perform a single read operation and expect HTTP payloads in one packet. This aligns aiohttp with other popular HTTP clients that already coalesce small requests.
Additionally, when client middleware communicates with an aiohttp server, connection reuse is more likely to occur since complete responses arrive in a single packet for small payloads, further improving performance in aiohttp-to-aiohttp communication.
Changes
Core Implementation
Modified
StreamWriter
inhttp_writer.py
:_headers_buf
and_headers_written
attributeswrite_headers()
to buffer headers instead of sending immediatelysend_headers()
method to force sending buffered headers when neededwrite()
andwrite_eof()
methods_writelines()
for zero-copy writes when coalescing dataset_eof()
to use single write for headers + chunked EOF markerdrain
before buffer size comparisonUpdated
ClientRequest
inclient_reqrep.py
:writer.send_headers()
before waitingAdded Header Buffering Control in
web_response.py
:_send_headers_immediately
class attribute (defaults toTrue
for backward compatibility)StreamResponse
sends headers immediately (for subclasses like FileResponse that use sendfile)Response
class sets_send_headers_immediately = False
to opt into header buffering_write_headers()
checks this attribute to decide whether to send headers immediatelyBenefits
_writelines()
to avoid memory copiesTechnical Details
_send_headers_immediately
attribute)_writelines()
Backward Compatibility
StreamResponse
and its subclasses maintain existing behavior (headers sent immediately)Response
class opts into header buffering for packet coalescingStreamWriter
subclasses continue to work as before