Skip to content

gh-146507: Make _SelectorTransport.get_write_buffer_size() O(1)#146518

Merged
kumaraditya303 merged 5 commits intopython:mainfrom
moktamd:fix/selector-transport-buffer-size
Mar 27, 2026
Merged

gh-146507: Make _SelectorTransport.get_write_buffer_size() O(1)#146518
kumaraditya303 merged 5 commits intopython:mainfrom
moktamd:fix/selector-transport-buffer-size

Conversation

@moktamd
Copy link
Copy Markdown
Contributor

@moktamd moktamd commented Mar 27, 2026

Closes #146507

_SelectorTransport.get_write_buffer_size() recalculates the total buffer size by iterating the entire _buffer deque on every call. Since this is called from _maybe_pause_protocol() after every write(), it creates O(n²) behavior when chunks accumulate faster than they drain.

This replaces the O(n) sum with a running _buffer_size counter, matching the pattern already used by _SelectorDatagramTransport and _ProactorBaseWritePipeTransport.

Also fixes a pre-existing issue in _write_send where BlockingIOError after popleft() would silently drop the buffer entry — the data is now put back.

Maintain a running _buffer_size counter instead of recalculating
with sum(map(len, _buffer)) on every call. This matches the O(1)
pattern already used by _SelectorDatagramTransport and
_ProactorBaseWritePipeTransport in the same codebase.
@python-cla-bot
Copy link
Copy Markdown

python-cla-bot bot commented Mar 27, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link
Copy Markdown

bedevere-app bot commented Mar 27, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@kumaraditya303 kumaraditya303 merged commit 1417737 into python:main Mar 27, 2026
52 checks passed
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.

_SelectorTransport.get_write_buffer_size() is O(n), causing quadratic behavior under write pressure

2 participants