Commit dba8df11 by Mathieu Kniewallner Committed by Randy Döring

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`.
parent c66178a0
...@@ -23,6 +23,8 @@ class Term: ...@@ -23,6 +23,8 @@ class Term:
def __init__(self, dependency: Dependency, is_positive: bool) -> None: def __init__(self, dependency: Dependency, is_positive: bool) -> None:
self._dependency = dependency self._dependency = dependency
self._positive = is_positive self._positive = is_positive
self.relation = functools.lru_cache(maxsize=None)(self._relation)
self.intersect = functools.lru_cache(maxsize=None)(self._intersect)
@property @property
def inverse(self) -> Term: def inverse(self) -> Term:
...@@ -48,8 +50,7 @@ class Term: ...@@ -48,8 +50,7 @@ class Term:
and self.relation(other) == SetRelation.SUBSET 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 Returns the relationship between the package versions
allowed by this term and another. allowed by this term and another.
...@@ -111,8 +112,7 @@ class Term: ...@@ -111,8 +112,7 @@ class Term:
# not foo ^1.5.0 is a superset of not foo ^1.0.0 # not foo ^1.5.0 is a superset of not foo ^1.0.0
return SetRelation.OVERLAPPING 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 Returns a Term that represents the packages
allowed by both this term and another allowed by both this term and another
......
...@@ -104,6 +104,9 @@ class Authenticator: ...@@ -104,6 +104,9 @@ class Authenticator:
if not disable_cache if not disable_cache
else None else None
) )
self.get_repository_config_for_url = functools.lru_cache(maxsize=None)(
self._get_repository_config_for_url
)
@property @property
def cache(self) -> FileCache | None: def cache(self) -> FileCache | None:
...@@ -351,8 +354,7 @@ class Authenticator: ...@@ -351,8 +354,7 @@ class Authenticator:
self._certs[url] = self._get_certs_for_url(url) self._certs[url] = self._get_certs_for_url(url)
return self._certs[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 self, url: str, exact_match: bool = False
) -> AuthenticatorRepositoryConfig | None: ) -> AuthenticatorRepositoryConfig | None:
parsed_url = urllib.parse.urlsplit(url) parsed_url = urllib.parse.urlsplit(url)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment