Repository navigation
gh-159503: Avoid copying the unsent remainder of a bytes write in asyncio selector transports - #159504
Open
Ar-maan05 wants to merge 1 commit into
Open
gh-159503: Avoid copying the unsent remainder of a bytes write in asyncio selector transports#159504Ar-maan05 wants to merge 1 commit into
Ar-maan05 wants to merge 1 commit into
Conversation
…in asyncio selector transports When a large bytes write is queued behind pending data, every partial send re-queued the leftover as b[nbytes:], copying the rest of the object each time. Keep the remainder as a memoryview in _adjust_leftover_buffer() and _write_send(), as write() already does on its first-send path.
Ar-maan05
requested review from
1st1,
asvetlov,
kumaraditya303 and
willingc
as code owners
October 16, 2026 01:58
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.
When a large
byteswrite is queued behind pending data, every partial send re-queued the leftover asb[nbytes:], copying the rest of the object each time, so the write cost O(n**2). One write of 48 MiB behind a 2 MiB prefix on a socketpair (Linux, sendmsg path):The fix wraps the remainder in a
memoryviewin_adjust_leftover_buffer()and_write_send(), the same waywrite()already does on its first-send path. Abytearraypassed towrite()now stays exported while its remainder is pending on these paths too, which the first-send path already does today.Tested with two new tests checking that the leftover is a view on the original object,
test_asynciopassing, and a differential harness that drives the old and new methods with a scripted socket (partial sends,BlockingIOError, send and sendmsg, bytes/bytearray/memoryview input): identical output over 1200 runs.