From fbbcaedb844e828f41e61028c182c4e70b4c89e1 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 3 Oct 2024 16:54:34 -0500 Subject: [PATCH] Add __slots__ to timer helpers (#9406) Co-authored-by: Sam Bull (cherry picked from commit 24b0e6f7c9d5a0815aebdc0dde5ff9a860a550d9) --- CHANGES/9406.misc.rst | 1 + aiohttp/helpers.py | 10 ++++++++++ pyproject.toml | 5 +++++ 3 files changed, 16 insertions(+) create mode 100644 CHANGES/9406.misc.rst diff --git a/CHANGES/9406.misc.rst b/CHANGES/9406.misc.rst new file mode 100644 index 00000000000..0a0f7e78677 --- /dev/null +++ b/CHANGES/9406.misc.rst @@ -0,0 +1 @@ +Reduced memory required for timer objects created during the client request lifecycle -- by :user:`bdraco`. diff --git a/aiohttp/helpers.py b/aiohttp/helpers.py index 070b04f8d82..1ea6a56db46 100644 --- a/aiohttp/helpers.py +++ b/aiohttp/helpers.py @@ -617,6 +617,8 @@ def calculate_timeout_when( class TimeoutHandle: """Timeout handle""" + __slots__ = ("_timeout", "_loop", "_ceil_threshold", "_callbacks") + def __init__( self, loop: asyncio.AbstractEventLoop, @@ -665,11 +667,17 @@ def __call__(self) -> None: class BaseTimerContext(ContextManager["BaseTimerContext"]): + + __slots__ = () + def assert_timeout(self) -> None: """Raise TimeoutError if timeout has been exceeded.""" class TimerNoop(BaseTimerContext): + + __slots__ = () + def __enter__(self) -> BaseTimerContext: return self @@ -685,6 +693,8 @@ def __exit__( class TimerContext(BaseTimerContext): """Low resolution timeout context manager""" + __slots__ = ("_loop", "_tasks", "_cancelled", "_cancelling") + def __init__(self, loop: asyncio.AbstractEventLoop) -> None: self._loop = loop self._tasks: List[asyncio.Task[Any]] = [] diff --git a/pyproject.toml b/pyproject.toml index 85d7c87eb34..33962686919 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -82,3 +82,8 @@ skip = "pp*" [tool.codespell] skip = '.git,*.pdf,*.svg,Makefile,CONTRIBUTORS.txt,venvs,_build' ignore-words-list = 'te' + +[tool.slotscheck] +# TODO(3.13): Remove aiohttp.helpers once https://github.com/python/cpython/pull/106771 +# is available in all supported cpython versions +exclude-modules = "(^aiohttp\\.helpers)"