pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/python/cpython/commit/b484c32d0ae386df8751aea86b3055813d8f805b

8faa60c69660fa.css" /> gh-141863: use `bytearray.take_bytes` in asyncio streams for better p… · python/cpython@b484c32 · GitHub
Skip to content

Commit b484c32

Browse files
authored
gh-141863: use bytearray.take_bytes in asyncio streams for better performance (#141864)
1 parent fb65552 commit b484c32

2 files changed

Lines changed: 12 additions & 22 deletions

File tree

Lib/asyncio/streams.py

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -667,8 +667,7 @@ async def readuntil(self, separator=b'\n'):
667667
# adds data which makes separator be found. That's why we check for
668668
# EOF *after* inspecting the buffer.
669669
if self._eof:
670-
chunk = bytes(self._buffer)
671-
self._buffer.clear()
670+
chunk = self._buffer.take_bytes()
672671
raise exceptions.IncompleteReadError(chunk, None)
673672

674673
# _wait_for_data() will resume reading if stream was paused.
@@ -678,10 +677,9 @@ async def readuntil(self, separator=b'\n'):
678677
raise exceptions.LimitOverrunError(
679678
'Separator is found, but chunk is longer than limit', match_start)
680679

681-
chunk = self._buffer[:match_end]
682-
del self._buffer[:match_end]
680+
chunk = self._buffer.take_bytes(match_end)
683681
self._maybe_resume_transport()
684-
return bytes(chunk)
682+
return chunk
685683

686684
async def read(self, n=-1):
687685
"""Read up to `n` bytes from the stream.
@@ -716,20 +714,16 @@ async def read(self, n=-1):
716714
# collect everything in self._buffer, but that would
717715
# deadlock if the subprocess sends more than self.limit
718716
# bytes. So just call self.read(self._limit) until EOF.
719-
blocks = []
720-
while True:
721-
block = await self.read(self._limit)
722-
if not block:
723-
break
724-
blocks.append(block)
725-
return b''.join(blocks)
717+
joined = bytearray()
718+
while block := await self.read(self._limit):
719+
joined += block
720+
return joined.take_bytes()
726721

727722
if not self._buffer and not self._eof:
728723
await self._wait_for_data('read')
729724

730725
# This will work right even if buffer is less than n bytes
731-
data = bytes(memoryview(self._buffer)[:n])
732-
del self._buffer[:n]
726+
data = self._buffer.take_bytes(min(len(self._buffer), n))
733727

734728
self._maybe_resume_transport()
735729
return data
@@ -760,18 +754,12 @@ async def readexactly(self, n):
760754

761755
while len(self._buffer) < n:
762756
if self._eof:
763-
incomplete = bytes(self._buffer)
764-
self._buffer.clear()
757+
incomplete = self._buffer.take_bytes()
765758
raise exceptions.IncompleteReadError(incomplete, n)
766759

767760
await self._wait_for_data('readexactly')
768761

769-
if len(self._buffer) == n:
770-
data = bytes(self._buffer)
771-
self._buffer.clear()
772-
else:
773-
data = bytes(memoryview(self._buffer)[:n])
774-
del self._buffer[:n]
762+
data = self._buffer.take_bytes(n)
775763
self._maybe_resume_transport()
776764
return data
777765

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Update :ref:`asyncio-streams` to use :func:`bytearray.take_bytes` for a over
2+
10% performance improvement on pyperformance asyncio_tcp benchmark.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy