From dba8df119935d534ca0430ec59579dfbebe98a85 Mon Sep 17 00:00:00 2001 From: Mathieu Kniewallner Date: Sun, 22 May 2022 12:21:46 +0200 Subject: [PATCH] refactor: ensure garbage collection of objects using lru cache Decorating class instances methods using `lru_cache` prevents garbage collection to happen when an object gets deleted. This could be really bad for memory consumption. Some resources about this: - https://rednafi.github.io/reflections/dont-wrap-instance-methods-with-functoolslru_cache-decorator-in-python.html - https://www.youtube.com/watch?v=sVjtp6tGo0g Originally spotted by rule `B019` of `flake8-bugbear`. --- src/poetry/mixology/term.py | 8 ++++---- src/poetry/utils/authenticator.py | 6 ++++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/poetry/mixology/term.py b/src/poetry/mixology/term.py index d19a190024d..6e75158ebbe 100644 --- a/src/poetry/mixology/term.py +++ b/src/poetry/mixology/term.py @@ -23,6 +23,8 @@ class Term: def __init__(self, dependency: Dependency, is_positive: bool) -> None: self._dependency = dependency self._positive = is_positive + self.relation = functools.lru_cache(maxsize=None)(self._relation) + self.intersect = functools.lru_cache(maxsize=None)(self._intersect) @property def inverse(self) -> Term: @@ -48,8 +50,7 @@ def satisfies(self, other: Term) -> bool: and self.relation(other) == SetRelation.SUBSET ) - @functools.lru_cache(maxsize=None) - def relation(self, other: Term) -> str: + def _relation(self, other: Term) -> str: """ Returns the relationship between the package versions allowed by this term and another. @@ -111,8 +112,7 @@ def relation(self, other: Term) -> str: # not foo ^1.5.0 is a superset of not foo ^1.0.0 return SetRelation.OVERLAPPING - @functools.lru_cache(maxsize=None) - def intersect(self, other: Term) -> Term | None: + def _intersect(self, other: Term) -> Term | None: """ Returns a Term that represents the packages allowed by both this term and another diff --git a/src/poetry/utils/authenticator.py b/src/poetry/utils/authenticator.py index 006cc753362..11b9aa68dfe 100644 --- a/src/poetry/utils/authenticator.py +++ b/src/poetry/utils/authenticator.py @@ -104,6 +104,9 @@ def __init__( if not disable_cache else None ) + self.get_repository_config_for_url = functools.lru_cache(maxsize=None)( + self._get_repository_config_for_url + ) @property def cache(self) -> FileCache | None: @@ -351,8 +354,7 @@ def get_certs_for_url(self, url: str) -> dict[str, Path | None]: self._certs[url] = self._get_certs_for_url(url) return self._certs[url] - @functools.lru_cache(maxsize=None) - def get_repository_config_for_url( + def _get_repository_config_for_url( self, url: str, exact_match: bool = False ) -> AuthenticatorRepositoryConfig | None: parsed_url = urllib.parse.urlsplit(url)