You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
_SelectorTransport.get_write_buffer_size() is O(n), causing quadratic behavior under write pressure #146507
This method is called from _maybe_pause_protocol() after every write() / writelines(), and from _maybe_resume_protocol() during _write_ready(). When the transport cannot drain fast enough and chunks accumulate in the buffer, this creates O(n²) behavior — each new write iterates over all previously buffered chunks.
In our production system (aiohttp WebSocket server sending many small binary messages), this caused get_write_buffer_size to consume ~90% CPU under load.
Other transports already use O(1) tracking
_SelectorTransport is the only transport with this problem. Even within the same module, _SelectorDatagramTransport already maintains a running self._buffer_size counter — incrementing on write, decrementing on drain — and returns it directly from get_write_buffer_size() in O(1).
Activity
Suor commentedon Mar 27, 2026
Here is a workaoround we are using for now. In case anyone interested.
NOTE: it will break if you pass a mutable something to
.send_bytes()and when actually mutate it before it was sent. Don't do that ;)Details
pythongh-146507: Make _SelectorTransport.get_write_buffer_size() O(1)
Add NEWS entry for pythongh-146507
gh-146507: cache the buffer size for `SelectorTransport.get_write_buf…
pythongh-146507: cache the buffer size for `SelectorTransport.get_wri…
pythongh-146507: cache the buffer size for `SelectorTransport.get_wri…