Skip to content

Commit

Permalink
Add __slots__ to stream classes (#9407)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Oct 3, 2024
1 parent b96b01b commit e653b28
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES/9407.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Reduced memory required for stream objects created during the client request lifecycle -- by :user:`bdraco`.
34 changes: 33 additions & 1 deletion aiohttp/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class EofStream(Exception):


class AsyncStreamIterator(Generic[_T]):

__slots__ = ("read_func",)

def __init__(self, read_func: Callable[[], Awaitable[_T]]) -> None:
self.read_func = read_func

Expand All @@ -59,6 +62,9 @@ async def __anext__(self) -> _T:


class ChunkTupleAsyncStreamIterator:

__slots__ = ("_stream",)

def __init__(self, stream: "StreamReader") -> None:
self._stream = stream

Expand All @@ -73,6 +79,9 @@ async def __anext__(self) -> Tuple[bytes, bool]:


class AsyncStreamReaderMixin:

__slots__ = ()

def __aiter__(self) -> AsyncStreamIterator[bytes]:
return AsyncStreamIterator(self.readline) # type: ignore[attr-defined]

Expand Down Expand Up @@ -107,7 +116,25 @@ class StreamReader(AsyncStreamReaderMixin):
"""

total_bytes = 0
__slots__ = (
"_protocol",
"_low_water",
"_high_water",
"_loop",
"_size",
"_cursor",
"_http_chunk_splits",
"_buffer",
"_buffer_offset",
"_eof",
"_waiter",
"_eof_waiter",
"_exception",
"_timer",
"_eof_callbacks",
"_eof_counter",
"total_bytes",
)

def __init__(
self,
Expand All @@ -134,6 +161,8 @@ def __init__(
self._exception: Optional[Union[Type[BaseException], BaseException]] = None
self._timer = TimerNoop() if timer is None else timer
self._eof_callbacks: List[Callable[[], None]] = []
self._eof_counter = 0
self.total_bytes = 0

def __repr__(self) -> str:
info = [self.__class__.__name__]
Expand Down Expand Up @@ -505,6 +534,9 @@ def _read_nowait(self, n: int) -> bytes:


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

__slots__ = ("_read_eof_chunk",)

def __init__(self) -> None:
self._read_eof_chunk = False

Expand Down

0 comments on commit e653b28

Please sign in to comment.