summaryrefslogtreecommitdiffstats
path: root/Lib/asyncio/selector_events.py
diff options
context:
space:
mode:
authormsoxzw <56633971+msoxzw@users.noreply.github.com>2022-04-15 19:59:01 (GMT)
committerGitHub <noreply@github.com>2022-04-15 19:59:01 (GMT)
commit42fabc3ea767f10989363536eaaa9da32616ab57 (patch)
tree6233bfa04fbe4ad6f367d2f4e05d0acb7553c006 /Lib/asyncio/selector_events.py
parentc9e231de8551ab6d06c92dfa95033150e52d7f1f (diff)
downloadcpython-42fabc3ea767f10989363536eaaa9da32616ab57.zip
cpython-42fabc3ea767f10989363536eaaa9da32616ab57.tar.gz
cpython-42fabc3ea767f10989363536eaaa9da32616ab57.tar.bz2
gh-91487: Optimize asyncio UDP speed (GH-91488)
Fix #91487 When transferring a small file, e.g. 256 KiB, the speed of this PR is comparable. However, if a large file, e.g. 65536 KiB, is transferred, asyncio UDP will be over 100 times faster than the original. The speed is presumably significantly faster if a larger file is transferred, e.g. 1048576 KiB. Automerge-Triggered-By: GH:gpshead
Diffstat (limited to 'Lib/asyncio/selector_events.py')
-rw-r--r--Lib/asyncio/selector_events.py6
1 files changed, 5 insertions, 1 deletions
diff --git a/Lib/asyncio/selector_events.py b/Lib/asyncio/selector_events.py
index e99a503..c9bbe2a 100644
--- a/Lib/asyncio/selector_events.py
+++ b/Lib/asyncio/selector_events.py
@@ -1131,6 +1131,7 @@ class _SelectorDatagramTransport(_SelectorTransport):
waiter=None, extra=None):
super().__init__(loop, sock, protocol, extra)
self._address = address
+ self._buffer_size = 0
self._loop.call_soon(self._protocol.connection_made, self)
# only start reading when connection_made() has been called
self._loop.call_soon(self._add_reader,
@@ -1141,7 +1142,7 @@ class _SelectorDatagramTransport(_SelectorTransport):
waiter, None)
def get_write_buffer_size(self):
- return sum(len(data) for data, _ in self._buffer)
+ return self._buffer_size
def _read_ready(self):
if self._conn_lost:
@@ -1200,11 +1201,13 @@ class _SelectorDatagramTransport(_SelectorTransport):
# Ensure that what we buffer is immutable.
self._buffer.append((bytes(data), addr))
+ self._buffer_size += len(data)
self._maybe_pause_protocol()
def _sendto_ready(self):
while self._buffer:
data, addr = self._buffer.popleft()
+ self._buffer_size -= len(data)
try:
if self._extra['peername']:
self._sock.send(data)
@@ -1212,6 +1215,7 @@ class _SelectorDatagramTransport(_SelectorTransport):
self._sock.sendto(data, addr)
except (BlockingIOError, InterruptedError):
self._buffer.appendleft((data, addr)) # Try again later.
+ self._buffer_size += len(data)
break
except OSError as exc:
self._protocol.error_received(exc)