Issue #28370: Speedup asyncio.StreamReader.readexactly

Patch by Коренберг Марк.
This commit is contained in:
Yury Selivanov 2016-10-05 18:01:12 -04:00
parent 5b8d4f97f8
commit 3e56ff0d08
2 changed files with 18 additions and 17 deletions

View File

@ -448,6 +448,7 @@ class StreamReader:
assert not self._eof, '_wait_for_data after EOF' assert not self._eof, '_wait_for_data after EOF'
# Waiting for data while paused will make deadlock, so prevent it. # Waiting for data while paused will make deadlock, so prevent it.
# This is essential for readexactly(n) for case when n > self._limit.
if self._paused: if self._paused:
self._paused = False self._paused = False
self._transport.resume_reading() self._transport.resume_reading()
@ -658,25 +659,22 @@ class StreamReader:
if n == 0: if n == 0:
return b'' return b''
# There used to be "optimized" code here. It created its own while len(self._buffer) < n:
# Future and waited until self._buffer had at least the n if self._eof:
# bytes, then called read(n). Unfortunately, this could pause incomplete = bytes(self._buffer)
# the transport if the argument was larger than the pause self._buffer.clear()
# limit (which is twice self._limit). So now we just read() raise IncompleteReadError(incomplete, n)
# into a local buffer.
blocks = [] yield from self._wait_for_data('readexactly')
while n > 0:
block = yield from self.read(n)
if not block:
partial = b''.join(blocks)
raise IncompleteReadError(partial, len(partial) + n)
blocks.append(block)
n -= len(block)
assert n == 0 if len(self._buffer) == n:
data = bytes(self._buffer)
return b''.join(blocks) self._buffer.clear()
else:
data = bytes(self._buffer[:n])
del self._buffer[:n]
self._maybe_resume_transport()
return data
if compat.PY35: if compat.PY35:
@coroutine @coroutine

View File

@ -353,6 +353,9 @@ Library
- Issue #28369: Raise RuntimeError when transport's FD is used with - Issue #28369: Raise RuntimeError when transport's FD is used with
add_reader, add_writer, etc. add_reader, add_writer, etc.
- Issue #28370: Speedup asyncio.StreamReader.readexactly.
Patch by Коренберг Марк.
IDLE IDLE
---- ----