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:
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 @@ class Term:
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 @@ class Term:
# 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
......
......@@ -104,6 +104,9 @@ class Authenticator:
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 @@ class Authenticator:
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)
......
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