commit:     c6ee4348df65210ee4c052b894cbc9f4f54f6ce8
Author:     Daniel Harding <dharding <AT> living180 <DOT> net>
AuthorDate: Sun Sep 10 06:51:13 2023 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Thu Sep 21 15:46:00 2023 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=c6ee4348

depgraph: improve reverse dep caching

Avoid applying @functools.lru_cache() to the
depgraph._slot_operator_check_reverse_depencencies() method, as applying
the lru_cache decorator to a method can prevent the class instances from
being garbage collected due to self being stored in the cache as part of
the args key[1].  Instead, set up a per-instance cache in the __init__()
method.

[1] https://stackoverflow.com/q/33672412/3573385

Bug: https://bugs.gentoo.org/883071
Signed-off-by: Daniel Harding <dharding <AT> living180.net>
Signed-off-by: Sam James <sam <AT> gentoo.org>

 lib/_emerge/depgraph.py | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/lib/_emerge/depgraph.py b/lib/_emerge/depgraph.py
index 01a49bcb53..0629acab22 100644
--- a/lib/_emerge/depgraph.py
+++ b/lib/_emerge/depgraph.py
@@ -679,6 +679,12 @@ class depgraph:
 
         self.query = UserQuery(myopts).query
 
+        # Set up a per-instance memoization cache for the
+        # _slot_operator_check_reverse_dependencies() method:
+        self._slot_operator_check_reverse_dependencies = functools.lru_cache(
+            maxsize=100
+        )(self._slot_operator_check_reverse_dependencies)
+
     def _index_binpkgs(self):
         for root in self._frozen_config.trees:
             bindb = self._frozen_config.trees[root]["bintree"].dbapi
@@ -2214,7 +2220,8 @@ class depgraph:
 
         return None
 
-    @functools.lru_cache(maxsize=100)
+    # This method is memoized on a per-instance basis via a decorator applied
+    # in __init__().
     def _slot_operator_check_reverse_dependencies(
         self, existing_pkg, candidate_pkg, replacement_parent=None
     ):

Reply via email to