diff mercurial/util.py @ 39565:2dcc68c7d25b

util: ability to change capacity when copying lrucachedict This will allow us to easily replace an lrucachedict with one with a higher or lower capacity as consumers deem necessary. IMO it is easier to just create a new cache instance than to muck with the capacity of an existing cache. Mutating an existing cache's capacity feels more prone to bugs. Differential Revision: https://phab.mercurial-scm.org/D4500
author Gregory Szorc <gregory.szorc@gmail.com>
date Thu, 06 Sep 2018 11:40:20 -0700
parents 5d75a3c16193
children bd9d3a89f07b
line wrap: on
line diff
--- a/mercurial/util.py	Thu Sep 06 11:37:27 2018 -0700
+++ b/mercurial/util.py	Thu Sep 06 11:40:20 2018 -0700
@@ -1311,8 +1311,19 @@
 
         self._cache.clear()
 
-    def copy(self):
-        result = lrucachedict(self.capacity)
+    def copy(self, capacity=None):
+        """Create a new cache as a copy of the current one.
+
+        By default, the new cache has the same capacity as the existing one.
+        But, the cache capacity can be changed as part of performing the
+        copy.
+
+        Items in the copy have an insertion/access order matching this
+        instance.
+        """
+
+        capacity = capacity or self.capacity
+        result = lrucachedict(capacity)
 
         # We copy entries by iterating in oldest-to-newest order so the copy
         # has the correct ordering.
@@ -1322,6 +1333,8 @@
         while n.key is _notset and n is not self._head:
             n = n.prev
 
+        # We could potentially skip the first N items when decreasing capacity.
+        # But let's keep it simple unless it is a performance problem.
         for i in range(len(self._cache)):
             result[n.key] = n.value
             n = n.prev