comparison tests/test-lrucachedict.py @ 39568:842cd0bdda75

util: teach lrucachedict to enforce a max total cost Now that lrucachedict entries can have a numeric cost associated with them and we can easily pop the oldest item in the cache, it now becomes relatively trivial to implement support for enforcing a high water mark on the total cost of items in the cache. This commit teaches lrucachedict instances to have a max cost associated with them. When items are inserted, we pop old items until enough "cost" frees up to make room for the new item. This feature is close to zero cost when not used (modulo the insertion regressed introduced by the previous commit): $ ./hg perflrucachedict --size 4 --gets 1000000 --sets 1000000 --mixed 1000000 ! gets ! wall 0.607444 comb 0.610000 user 0.610000 sys 0.000000 (best of 17) ! wall 0.601653 comb 0.600000 user 0.600000 sys 0.000000 (best of 17) ! inserts ! wall 0.678261 comb 0.680000 user 0.680000 sys 0.000000 (best of 14) ! wall 0.685042 comb 0.680000 user 0.680000 sys 0.000000 (best of 15) ! sets ! wall 0.808770 comb 0.800000 user 0.800000 sys 0.000000 (best of 13) ! wall 0.834241 comb 0.830000 user 0.830000 sys 0.000000 (best of 12) ! mixed ! wall 0.782441 comb 0.780000 user 0.780000 sys 0.000000 (best of 13) ! wall 0.803804 comb 0.800000 user 0.800000 sys 0.000000 (best of 13) $ hg perflrucachedict --size 1000 --gets 1000000 --sets 1000000 --mixed 1000000 ! init ! wall 0.006952 comb 0.010000 user 0.010000 sys 0.000000 (best of 418) ! gets ! wall 0.613350 comb 0.610000 user 0.610000 sys 0.000000 (best of 17) ! wall 0.617415 comb 0.620000 user 0.620000 sys 0.000000 (best of 17) ! inserts ! wall 0.701270 comb 0.700000 user 0.700000 sys 0.000000 (best of 15) ! wall 0.700516 comb 0.700000 user 0.700000 sys 0.000000 (best of 15) ! sets ! wall 0.825720 comb 0.830000 user 0.830000 sys 0.000000 (best of 13) ! wall 0.837946 comb 0.840000 user 0.830000 sys 0.010000 (best of 12) ! mixed ! wall 0.821644 comb 0.820000 user 0.820000 sys 0.000000 (best of 13) ! wall 0.850559 comb 0.850000 user 0.850000 sys 0.000000 (best of 12) I reckon the slight slowdown on insert is due to added if checks. For caches with total cost limiting enabled: $ hg perflrucachedict --size 4 --gets 1000000 --sets 1000000 --mixed 1000000 --costlimit 100 ! gets w/ cost limit ! wall 0.598737 comb 0.590000 user 0.590000 sys 0.000000 (best of 17) ! inserts w/ cost limit ! wall 1.694282 comb 1.700000 user 1.700000 sys 0.000000 (best of 6) ! mixed w/ cost limit ! wall 1.157655 comb 1.150000 user 1.150000 sys 0.000000 (best of 9) $ hg perflrucachedict --size 1000 --gets 1000000 --sets 1000000 --mixed 1000000 --costlimit 10000 ! gets w/ cost limit ! wall 0.598526 comb 0.600000 user 0.600000 sys 0.000000 (best of 17) ! inserts w/ cost limit ! wall 37.838315 comb 37.840000 user 37.840000 sys 0.000000 (best of 3) ! mixed w/ cost limit ! wall 18.060198 comb 18.060000 user 18.060000 sys 0.000000 (best of 3) $ hg perflrucachedict --size 1000 --gets 1000000 --sets 1000000 --mixed 1000000 --costlimit 10000 --mixedgetfreq 90 ! gets w/ cost limit ! wall 0.600024 comb 0.600000 user 0.600000 sys 0.000000 (best of 17) ! inserts w/ cost limit ! wall 37.154547 comb 37.120000 user 37.120000 sys 0.000000 (best of 3) ! mixed w/ cost limit ! wall 4.381602 comb 4.380000 user 4.370000 sys 0.010000 (best of 3) The functions we're benchmarking are slightly different, which could move numbers by a few milliseconds. But the slowdown on insert is too great to be explained by that. The slowness is due to insert heavy operations needing to call popoldest() repeatedly when the cache is at capacity. The next commit will address this. Differential Revision: https://phab.mercurial-scm.org/D4503
author Gregory Szorc <gregory.szorc@gmail.com>
date Thu, 06 Sep 2018 14:04:46 -0700
parents ee087f0d7db5
children f296c0b366c8
comparison
equal deleted inserted replaced
39567:ee087f0d7db5 39568:842cd0bdda75
131 131
132 self.assertEqual(list(iter(d)), ['d', 'c', 'b', 'a']) 132 self.assertEqual(list(iter(d)), ['d', 'c', 'b', 'a'])
133 for key in ('a', 'b', 'c', 'd'): 133 for key in ('a', 'b', 'c', 'd'):
134 self.assertEqual(d[key], 'v%s' % key) 134 self.assertEqual(d[key], 'v%s' % key)
135 135
136 d = util.lrucachedict(4, maxcost=42)
137 d.insert('a', 'va', cost=5)
138 d.insert('b', 'vb', cost=4)
139 d.insert('c', 'vc', cost=3)
140 dc = d.copy()
141 self.assertEqual(dc.maxcost, 42)
142 self.assertEqual(len(dc), 3)
143
144 # Max cost can be lowered as part of copy.
145 dc = d.copy(maxcost=10)
146 self.assertEqual(dc.maxcost, 10)
147 self.assertEqual(len(dc), 2)
148 self.assertEqual(dc.totalcost, 7)
149 self.assertIn('b', dc)
150 self.assertIn('c', dc)
151
136 def testcopydecreasecapacity(self): 152 def testcopydecreasecapacity(self):
137 d = util.lrucachedict(5) 153 d = util.lrucachedict(5)
138 d.insert('a', 'va', cost=4) 154 d.insert('a', 'va', cost=4)
139 d.insert('b', 'vb', cost=2) 155 d.insert('b', 'vb', cost=2)
140 d['c'] = 'vc' 156 d['c'] = 'vc'
215 self.assertEqual(d[key], 'v%s' % key) 231 self.assertEqual(d[key], 'v%s' % key)
216 232
217 d['a'] = 'va' 233 d['a'] = 'va'
218 self.assertEqual(d.popoldest(), ('b', 'vb')) 234 self.assertEqual(d.popoldest(), ('b', 'vb'))
219 235
236 def testmaxcost(self):
237 # Item cost is zero by default.
238 d = util.lrucachedict(6, maxcost=10)
239 d['a'] = 'va'
240 d['b'] = 'vb'
241 d['c'] = 'vc'
242 d['d'] = 'vd'
243 self.assertEqual(len(d), 4)
244 self.assertEqual(d.totalcost, 0)
245
246 d.clear()
247
248 # Insertion to exact cost threshold works without eviction.
249 d.insert('a', 'va', cost=6)
250 d.insert('b', 'vb', cost=4)
251
252 self.assertEqual(len(d), 2)
253 self.assertEqual(d['a'], 'va')
254 self.assertEqual(d['b'], 'vb')
255
256 # Inserting a new element with 0 cost works.
257 d['c'] = 'vc'
258 self.assertEqual(len(d), 3)
259
260 # Inserting a new element with cost putting us above high
261 # water mark evicts oldest single item.
262 d.insert('d', 'vd', cost=1)
263 self.assertEqual(len(d), 3)
264 self.assertEqual(d.totalcost, 5)
265 self.assertNotIn('a', d)
266 for key in ('b', 'c', 'd'):
267 self.assertEqual(d[key], 'v%s' % key)
268
269 # Inserting a new element with enough room for just itself
270 # evicts all items before.
271 d.insert('e', 've', cost=10)
272 self.assertEqual(len(d), 1)
273 self.assertEqual(d.totalcost, 10)
274 self.assertIn('e', d)
275
276 # Inserting a new element with cost greater than threshold
277 # still retains that item.
278 d.insert('f', 'vf', cost=11)
279 self.assertEqual(len(d), 1)
280 self.assertEqual(d.totalcost, 11)
281 self.assertIn('f', d)
282
283 # Inserting a new element will evict the last item since it is
284 # too large.
285 d['g'] = 'vg'
286 self.assertEqual(len(d), 1)
287 self.assertEqual(d.totalcost, 0)
288 self.assertIn('g', d)
289
290 d.clear()
291
292 d.insert('a', 'va', cost=7)
293 d.insert('b', 'vb', cost=3)
294 self.assertEqual(len(d), 2)
295
296 # Replacing a value with smaller cost won't result in eviction.
297 d.insert('b', 'vb2', cost=2)
298 self.assertEqual(len(d), 2)
299
300 # Replacing a value with a higher cost will evict when threshold
301 # exceeded.
302 d.insert('b', 'vb3', cost=4)
303 self.assertEqual(len(d), 1)
304 self.assertNotIn('a', d)
305
306 def testmaxcostcomplex(self):
307 d = util.lrucachedict(100, maxcost=100)
308 d.insert('a', 'va', cost=9)
309 d.insert('b', 'vb', cost=21)
310 d.insert('c', 'vc', cost=7)
311 d.insert('d', 'vc', cost=50)
312 self.assertEqual(d.totalcost, 87)
313
314 # Inserting new element should free multiple elements so we hit
315 # low water mark.
316 d.insert('e', 'vd', cost=25)
317 self.assertEqual(len(d), 3)
318 self.assertNotIn('a', d)
319 self.assertNotIn('b', d)
320 self.assertIn('c', d)
321 self.assertIn('d', d)
322 self.assertIn('e', d)
323
220 if __name__ == '__main__': 324 if __name__ == '__main__':
221 silenttestrunner.main(__name__) 325 silenttestrunner.main(__name__)