Skip to content

Commit

Permalink
Avoid bytes join and memory copy on single chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Nov 12, 2024
1 parent 0f18900 commit ae39de1
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions aiohttp/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ async def read(self, n: int = -1) -> bytes:
if not block:
break
blocks.append(block)
return b"".join(blocks)
return b"".join(blocks) if len(blocks) > 1 else blocks[0] if blocks else b""

# TODO: should be `if` instead of `while`
# because waiter maybe triggered on chunk end,
Expand Down Expand Up @@ -470,7 +470,7 @@ async def readexactly(self, n: int) -> bytes:
blocks.append(block)
n -= len(block)

return b"".join(blocks)
return b"".join(blocks) if len(blocks) > 1 else blocks[0] if blocks else b""

def read_nowait(self, n: int = -1) -> bytes:
# default was changed to be consistent with .read(-1)
Expand Down Expand Up @@ -527,7 +527,7 @@ def _read_nowait(self, n: int) -> bytes:
if n == 0:
break

return b"".join(chunks) if chunks else b""
return b"".join(chunks) if len(chunks) > 1 else chunks[0] if chunks else b""


class EmptyStreamReader(StreamReader): # lgtm [py/missing-call-to-init]
Expand Down

0 comments on commit ae39de1

Please sign in to comment.