D4504: util: optimize cost auditing on insert

indygreg (Gregory Szorc) phabricator at mercurial-scm.org
Wed Sep 12 10:53:30 EDT 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rHGcc23c09bc562: util: optimize cost auditing on insert (authored by indygreg, committed by ).

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D4504?vs=10823&id=10943

REVISION DETAIL
  https://phab.mercurial-scm.org/D4504

AFFECTED FILES
  mercurial/util.py

CHANGE DETAILS

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -1464,8 +1464,23 @@
         # This should run after an insertion. It should only be called if total
         # cost limits are being enforced.
         # The most recently inserted node is never evicted.
+        if len(self) <= 1 or self.totalcost <= self.maxcost:
+            return
+
+        # This is logically equivalent to calling popoldest() until we
+        # free up enough cost. We don't do that since popoldest() needs
+        # to walk the linked list and doing this in a loop would be
+        # quadratic. So we find the first non-empty node and then
+        # walk nodes until we free up enough capacity.
+        n = self._head.prev
+        while n.key is _notset:
+            n = n.prev
+
         while len(self) > 1 and self.totalcost > self.maxcost:
-            self.popoldest()
+            del self._cache[n.key]
+            self.totalcost -= n.cost
+            n.markempty()
+            n = n.prev
 
 def lrucachefunc(func):
     '''cache most recent results of function calls'''



To: indygreg, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list