Skip to content

Commit

Permalink
refactor: ensure garbage collection of objects using lru cache
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
mkniewallner authored and radoering committed May 22, 2022
1 parent c66178a commit dba8df1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/poetry/mixology/term.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/poetry/utils/authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit dba8df1

Please sign in to comment.