Mercurial > hg
diff mercurial/util.py @ 39567:ee087f0d7db5
util: allow lrucachedict to track cost of entries
Currently, lrucachedict allows tracking of arbitrary items with the
only limit being the total number of items in the cache.
Caches can be a lot more useful when they are bound by the size
of the items in them rather than the number of elements in the
cache.
In preparation for teaching lrucachedict to enforce a max size of
cached items, we teach lrucachedict to optionally associate a numeric
cost value with each node.
We purposefully let the caller define their own cost for nodes.
This does introduce some overhead. Most of it comes from __setitem__,
since that function now calls into insert(), thus introducing Python
function call overhead.
$ hg perflrucachedict --size 4 --gets 1000000 --sets 1000000 --mixed 1000000
! gets
! wall 0.599552 comb 0.600000 user 0.600000 sys 0.000000 (best of 17)
! wall 0.614643 comb 0.610000 user 0.610000 sys 0.000000 (best of 17)
! inserts
! <not available>
! wall 0.655817 comb 0.650000 user 0.650000 sys 0.000000 (best of 16)
! sets
! wall 0.540448 comb 0.540000 user 0.540000 sys 0.000000 (best of 18)
! wall 0.805644 comb 0.810000 user 0.810000 sys 0.000000 (best of 13)
! mixed
! wall 0.651556 comb 0.660000 user 0.660000 sys 0.000000 (best of 15)
! wall 0.781357 comb 0.780000 user 0.780000 sys 0.000000 (best of 13)
$ hg perflrucachedict --size 1000 --gets 1000000 --sets 1000000 --mixed 1000000
! gets
! wall 0.621014 comb 0.620000 user 0.620000 sys 0.000000 (best of 16)
! wall 0.615146 comb 0.620000 user 0.620000 sys 0.000000 (best of 17)
! inserts
! <not available>
! wall 0.698115 comb 0.700000 user 0.700000 sys 0.000000 (best of 15)
! sets
! wall 0.560247 comb 0.560000 user 0.560000 sys 0.000000 (best of 18)
! wall 0.832495 comb 0.830000 user 0.830000 sys 0.000000 (best of 12)
! mixed
! wall 0.686172 comb 0.680000 user 0.680000 sys 0.000000 (best of 15)
! wall 0.841359 comb 0.840000 user 0.840000 sys 0.000000 (best of 12)
We're still under 1us per insert, which seems like reasonable
performance for a cache.
If we comment out updating of self.totalcost during insert(),
performance of insert() is identical to __setitem__ before. However,
I don't want to make total cost evaluation lazy because it has
significant performance implications for when we need to evaluate the
total cost at mutation time (it requires a cache traversal, which could
be expensive for large caches).
Differential Revision: https://phab.mercurial-scm.org/D4502
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Fri, 07 Sep 2018 12:14:42 -0700 |
parents | bd9d3a89f07b |
children | 842cd0bdda75 |
line wrap: on
line diff
--- a/mercurial/util.py Wed Sep 05 23:15:20 2018 -0700 +++ b/mercurial/util.py Fri Sep 07 12:14:42 2018 -0700 @@ -1209,7 +1209,7 @@ Holds a reference to nodes on either side as well as a key-value pair for the dictionary entry. """ - __slots__ = (u'next', u'prev', u'key', u'value') + __slots__ = (u'next', u'prev', u'key', u'value', u'cost') def __init__(self): self.next = None @@ -1217,10 +1217,13 @@ self.key = _notset self.value = None + self.cost = 0 def markempty(self): """Mark the node as emptied.""" self.key = _notset + self.value = None + self.cost = 0 class lrucachedict(object): """Dict that caches most recent accesses and sets. @@ -1233,6 +1236,10 @@ we recycle head.prev and make it the new head. Cache accesses result in the node being moved to before the existing head and being marked as the new head node. + + Items in the cache can be inserted with an optional "cost" value. This is + simply an integer that is specified by the caller. The cache can be queried + for the total cost of all items presently in the cache. """ def __init__(self, max): self._cache = {} @@ -1242,6 +1249,7 @@ head.next = head self._size = 1 self.capacity = max + self.totalcost = 0 def __len__(self): return len(self._cache) @@ -1261,11 +1269,15 @@ self._movetohead(node) return node.value - def __setitem__(self, k, v): + def insert(self, k, v, cost=0): + """Insert a new item in the cache with optional cost value.""" node = self._cache.get(k) # Replace existing value and mark as newest. if node is not None: + self.totalcost -= node.cost node.value = v + node.cost = cost + self.totalcost += cost self._movetohead(node) return @@ -1277,17 +1289,24 @@ # At capacity. Kill the old entry. if node.key is not _notset: + self.totalcost -= node.cost del self._cache[node.key] node.key = k node.value = v + node.cost = cost + self.totalcost += cost self._cache[k] = node # And mark it as newest entry. No need to adjust order since it # is already self._head.prev. self._head = node + def __setitem__(self, k, v): + self.insert(k, v) + def __delitem__(self, k): node = self._cache.pop(k) + self.totalcost -= node.cost node.markempty() # Temporarily mark as newest item before re-adjusting head to make @@ -1306,6 +1325,7 @@ def clear(self): n = self._head while n.key is not _notset: + self.totalcost -= n.cost n.markempty() n = n.next @@ -1336,7 +1356,7 @@ # 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 + result.insert(n.key, n.value, cost=n.cost) n = n.prev return result @@ -1359,6 +1379,7 @@ # And remove it from the cache and mark it as empty. del self._cache[n.key] + self.totalcost -= n.cost n.markempty() return key, value