Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PR #9407/e653b281 backport][3.11] Add __slots__ to stream classes #9408

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -39,6 +39,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 @@ -56,6 +59,9 @@ async def __anext__(self) -> _T:


class ChunkTupleAsyncStreamIterator:

__slots__ = ("_stream",)

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

Expand All @@ -70,6 +76,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 @@ -104,7 +113,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 @@ -131,6 +158,8 @@ def __init__(
self._exception: Optional[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 @@ -517,6 +546,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
8 changes: 2 additions & 6 deletions tests/test_flowcontrol_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,12 @@ def protocol():

@pytest.fixture
def stream(loop, protocol):
out = streams.StreamReader(protocol, limit=1, loop=loop)
out._allow_pause = True
bdraco marked this conversation as resolved.
Show resolved Hide resolved
return out
return streams.StreamReader(protocol, limit=1, loop=loop)


@pytest.fixture
def buffer(loop, protocol):
out = streams.FlowControlDataQueue(protocol, limit=1, loop=loop)
out._allow_pause = True
bdraco marked this conversation as resolved.
Show resolved Hide resolved
return out
return streams.FlowControlDataQueue(protocol, limit=1, loop=loop)


class TestFlowControlStreamReader:
Expand Down
Loading