comparison 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
comparison
equal deleted inserted replaced
39564:5d75a3c16193 39565:2dcc68c7d25b
1309 n.markempty() 1309 n.markempty()
1310 n = n.next 1310 n = n.next
1311 1311
1312 self._cache.clear() 1312 self._cache.clear()
1313 1313
1314 def copy(self): 1314 def copy(self, capacity=None):
1315 result = lrucachedict(self.capacity) 1315 """Create a new cache as a copy of the current one.
1316
1317 By default, the new cache has the same capacity as the existing one.
1318 But, the cache capacity can be changed as part of performing the
1319 copy.
1320
1321 Items in the copy have an insertion/access order matching this
1322 instance.
1323 """
1324
1325 capacity = capacity or self.capacity
1326 result = lrucachedict(capacity)
1316 1327
1317 # We copy entries by iterating in oldest-to-newest order so the copy 1328 # We copy entries by iterating in oldest-to-newest order so the copy
1318 # has the correct ordering. 1329 # has the correct ordering.
1319 1330
1320 # Find the first non-empty entry. 1331 # Find the first non-empty entry.
1321 n = self._head.prev 1332 n = self._head.prev
1322 while n.key is _notset and n is not self._head: 1333 while n.key is _notset and n is not self._head:
1323 n = n.prev 1334 n = n.prev
1324 1335
1336 # We could potentially skip the first N items when decreasing capacity.
1337 # But let's keep it simple unless it is a performance problem.
1325 for i in range(len(self._cache)): 1338 for i in range(len(self._cache)):
1326 result[n.key] = n.value 1339 result[n.key] = n.value
1327 n = n.prev 1340 n = n.prev
1328 1341
1329 return result 1342 return result