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
Tests for the journal extension; records bookmark locations.
$ cat >> testmocks.py << EOF
> # mock out procutil.getuser() and util.makedate() to supply testable values
> import os
> from mercurial import util, pycompat
> from mercurial.utils import dateutil, procutil
> def mockgetuser():
> return b'foobar'
>
> def mockmakedate():
> filename = os.path.join(os.environ['TESTTMP'], 'testtime')
> try:
> with open(filename, 'rb') as timef:
> time = float(timef.read()) + 1
> except IOError:
> time = 0.0
> with open(filename, 'wb') as timef:
> timef.write(pycompat.bytestr(time))
> return (time, 0)
>
> procutil.getuser = mockgetuser
> dateutil.makedate = mockmakedate
> EOF
$ cat >> $HGRCPATH << EOF
> [extensions]
> journal=
> testmocks=`pwd`/testmocks.py
> EOF
Setup repo
$ hg init repo
$ cd repo
Test empty journal
$ hg journal
previous locations of '.':
no recorded locations
$ hg journal foo
previous locations of 'foo':
no recorded locations
Test that working copy changes are tracked
$ echo a > a
$ hg commit -Aqm a
$ hg journal
previous locations of '.':
cb9a9f314b8b commit -Aqm a
$ echo b > a
$ hg commit -Aqm b
$ hg journal
previous locations of '.':
1e6c11564562 commit -Aqm b
cb9a9f314b8b commit -Aqm a
$ hg up 0
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
$ hg journal
previous locations of '.':
cb9a9f314b8b up 0
1e6c11564562 commit -Aqm b
cb9a9f314b8b commit -Aqm a
Test that bookmarks are tracked
$ hg book -r tip bar
$ hg journal bar
previous locations of 'bar':
1e6c11564562 book -r tip bar
$ hg book -f bar
$ hg journal bar
previous locations of 'bar':
cb9a9f314b8b book -f bar
1e6c11564562 book -r tip bar
$ hg up
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
updating bookmark bar
$ hg journal bar
previous locations of 'bar':
1e6c11564562 up
cb9a9f314b8b book -f bar
1e6c11564562 book -r tip bar
Test that bookmarks and working copy tracking is not mixed
$ hg journal
previous locations of '.':
1e6c11564562 up
cb9a9f314b8b up 0
1e6c11564562 commit -Aqm b
cb9a9f314b8b commit -Aqm a
Test that you can list all entries as well as limit the list or filter on them
$ hg book -r tip baz
$ hg journal --all
previous locations of the working copy and bookmarks:
1e6c11564562 baz book -r tip baz
1e6c11564562 bar up
1e6c11564562 . up
cb9a9f314b8b bar book -f bar
1e6c11564562 bar book -r tip bar
cb9a9f314b8b . up 0
1e6c11564562 . commit -Aqm b
cb9a9f314b8b . commit -Aqm a
$ hg journal --limit 2
previous locations of '.':
1e6c11564562 up
cb9a9f314b8b up 0
$ hg journal bar
previous locations of 'bar':
1e6c11564562 up
cb9a9f314b8b book -f bar
1e6c11564562 book -r tip bar
$ hg journal foo
previous locations of 'foo':
no recorded locations
$ hg journal .
previous locations of '.':
1e6c11564562 up
cb9a9f314b8b up 0
1e6c11564562 commit -Aqm b
cb9a9f314b8b commit -Aqm a
$ hg journal "re:ba."
previous locations of 're:ba.':
1e6c11564562 baz book -r tip baz
1e6c11564562 bar up
cb9a9f314b8b bar book -f bar
1e6c11564562 bar book -r tip bar
Test that verbose, JSON, template and commit output work
$ hg journal --verbose --all
previous locations of the working copy and bookmarks:
000000000000 -> 1e6c11564562 foobar baz 1970-01-01 00:00 +0000 book -r tip baz
cb9a9f314b8b -> 1e6c11564562 foobar bar 1970-01-01 00:00 +0000 up
cb9a9f314b8b -> 1e6c11564562 foobar . 1970-01-01 00:00 +0000 up
1e6c11564562 -> cb9a9f314b8b foobar bar 1970-01-01 00:00 +0000 book -f bar
000000000000 -> 1e6c11564562 foobar bar 1970-01-01 00:00 +0000 book -r tip bar
1e6c11564562 -> cb9a9f314b8b foobar . 1970-01-01 00:00 +0000 up 0
cb9a9f314b8b -> 1e6c11564562 foobar . 1970-01-01 00:00 +0000 commit -Aqm b
000000000000 -> cb9a9f314b8b foobar . 1970-01-01 00:00 +0000 commit -Aqm a
$ hg journal --verbose -Tjson
[
{
"command": "up",
"date": [5, 0],
"name": ".",
"newhashes": ["1e6c11564562b4ed919baca798bc4338bd299d6a"],
"oldhashes": ["cb9a9f314b8b07ba71012fcdbc544b5a4d82ff5b"],
"user": "foobar"
},
{
"command": "up 0",
"date": [2, 0],
"name": ".",
"newhashes": ["cb9a9f314b8b07ba71012fcdbc544b5a4d82ff5b"],
"oldhashes": ["1e6c11564562b4ed919baca798bc4338bd299d6a"],
"user": "foobar"
},
{
"command": "commit -Aqm b",
"date": [1, 0],
"name": ".",
"newhashes": ["1e6c11564562b4ed919baca798bc4338bd299d6a"],
"oldhashes": ["cb9a9f314b8b07ba71012fcdbc544b5a4d82ff5b"],
"user": "foobar"
},
{
"command": "commit -Aqm a",
"date": [0, 0],
"name": ".",
"newhashes": ["cb9a9f314b8b07ba71012fcdbc544b5a4d82ff5b"],
"oldhashes": ["0000000000000000000000000000000000000000"],
"user": "foobar"
}
]
$ cat <<EOF >> $HGRCPATH
> [templates]
> j = "{oldhashes % '{node|upper}'} -> {newhashes % '{node|upper}'}
> - user: {user}
> - command: {command}
> - date: {date|rfc3339date}
> - newhashes: {newhashes}
> - oldhashes: {oldhashes}
> "
> EOF
$ hg journal -Tj -l1
previous locations of '.':
CB9A9F314B8B07BA71012FCDBC544B5A4D82FF5B -> 1E6C11564562B4ED919BACA798BC4338BD299D6A
- user: foobar
- command: up
- date: 1970-01-01T00:00:05+00:00
- newhashes: 1e6c11564562b4ed919baca798bc4338bd299d6a
- oldhashes: cb9a9f314b8b07ba71012fcdbc544b5a4d82ff5b
$ hg journal --commit
previous locations of '.':
1e6c11564562 up
changeset: 1:1e6c11564562
bookmark: bar
bookmark: baz
tag: tip
user: test
date: Thu Jan 01 00:00:00 1970 +0000
summary: b
cb9a9f314b8b up 0
changeset: 0:cb9a9f314b8b
user: test
date: Thu Jan 01 00:00:00 1970 +0000
summary: a
1e6c11564562 commit -Aqm b
changeset: 1:1e6c11564562
bookmark: bar
bookmark: baz
tag: tip
user: test
date: Thu Jan 01 00:00:00 1970 +0000
summary: b
cb9a9f314b8b commit -Aqm a
changeset: 0:cb9a9f314b8b
user: test
date: Thu Jan 01 00:00:00 1970 +0000
summary: a
Test for behaviour on unexpected storage version information
$ printf '42\0' > .hg/namejournal
$ hg journal
previous locations of '.':
abort: unknown journal file version '42'
[255]
$ hg book -r tip doomed
unsupported journal file version '42'