rust: peek_mut optim for lazy ancestors
This is one of the two optimizations that are also
present in the Python code: replacing pairs of pop/push
on the BinaryHeap by single updates, hence having it
under the hood maintain its consistency (sift) only once.
On Mozilla central, the measured gain (see details below)
is around 7%.
Creating the PeekMut object by calling peek_mut() right away
instead of peek() first is less efficient (gain is only 4%, stats
not included).
Our interpretation is that its creation has a cost which is vasted
in the cases where it ends by droping the value (Peekmut::pop()
just does self.heap.pop() anyway). On the other hand, the immutable
peek() is very fast: it's just taking a reference in the
underlying vector.
The Python version still has another optimization:
if parent(current) == current-1, then the heap doesn't need
to maintain its consistency, since we already know that
it's bigger than all the others in the heap.
Rust's BinaryHeap doesn't allow us to mutate its biggest
element with no housekeeping, but we tried it anyway, with a
copy of the BinaryHeap implementation with a dedicaded added
method: it's not worth the technical debt in our opinion
(we measured only a further 1.6% improvement).
One possible explanation would be that the sift is really fast
anyway in that case, whereas it's not in the case of Python,
because it's at least partly done in slow Python code.
Still it's possible that replacing BinaryHeap by something more
dedicated to discrete ordered types could be faster.
Measurements on mozilla-central:
Three runs of 'hg perfancestors' on the parent changeset:
Moyenne des médianes: 0.100587
! wall 0.100062 comb 0.100000 user 0.100000 sys 0.000000 (best of 98)
! wall 0.135804 comb 0.130000 user 0.130000 sys 0.000000 (max of 98)
! wall 0.102864 comb 0.102755 user 0.099286 sys 0.003469 (avg of 98)
! wall 0.101486 comb 0.110000 user 0.110000 sys 0.000000 (median of 98)
! wall 0.096804 comb 0.090000 user 0.090000 sys 0.000000 (best of 100)
! wall 0.132235 comb 0.130000 user 0.120000 sys 0.010000 (max of 100)
! wall 0.100258 comb 0.100300 user 0.096000 sys 0.004300 (avg of 100)
! wall 0.098384 comb 0.100000 user 0.100000 sys 0.000000 (median of 100)
! wall 0.099925 comb 0.100000 user 0.100000 sys 0.000000 (best of 98)
! wall 0.133518 comb 0.140000 user 0.130000 sys 0.010000 (max of 98)
! wall 0.102381 comb 0.102449 user 0.098265 sys 0.004184 (avg of 98)
! wall 0.101891 comb 0.090000 user 0.090000 sys 0.000000 (median of 98)
Mean of the medians: 0.100587
On the present changeset:
! wall 0.091344 comb 0.090000 user 0.090000 sys 0.000000 (best of 100)
! wall 0.122728 comb 0.120000 user 0.110000 sys 0.010000 (max of 100)
! wall 0.093268 comb 0.093300 user 0.089300 sys 0.004000 (avg of 100)
! wall 0.092567 comb 0.100000 user 0.090000 sys 0.010000 (median of 100)
! wall 0.093294 comb 0.080000 user 0.080000 sys 0.000000 (best of 100)
! wall 0.144887 comb 0.150000 user 0.140000 sys 0.010000 (max of 100)
! wall 0.097708 comb 0.097700 user 0.093400 sys 0.004300 (avg of 100)
! wall 0.094980 comb 0.100000 user 0.090000 sys 0.010000 (median of 100)
! wall 0.091262 comb 0.090000 user 0.080000 sys 0.010000 (best of 100)
! wall 0.123772 comb 0.130000 user 0.120000 sys 0.010000 (max of 100)
! wall 0.093188 comb 0.093200 user 0.089300 sys 0.003900 (avg of 100)
! wall 0.092364 comb 0.100000 user 0.090000 sys 0.010000 (median of 100)
Mean of the medians is 0.0933
Differential Revision: https://phab.mercurial-scm.org/D5358
revlog.parseindex must be able to parse the index file even if
an index entry is split between two 64k blocks. The ideal test
would be to create an index file with inline data where
64k < size < 64k + 64 (64k is the size of the read buffer, 64 is
the size of an index entry) and with an index entry starting right
before the 64k block boundary, and try to read it.
We approximate that by reducing the read buffer to 1 byte.
$ hg init a
$ cd a
$ echo abc > foo
$ hg add foo
$ hg commit -m 'add foo'
$ echo >> foo
$ hg commit -m 'change foo'
$ hg log -r 0:
changeset: 0:7c31755bf9b5
user: test
date: Thu Jan 01 00:00:00 1970 +0000
summary: add foo
changeset: 1:26333235a41c
tag: tip
user: test
date: Thu Jan 01 00:00:00 1970 +0000
summary: change foo
$ cat >> test.py << EOF
> from __future__ import print_function
> from mercurial import changelog, node, vfs
>
> class singlebyteread(object):
> def __init__(self, real):
> self.real = real
>
> def read(self, size=-1):
> if size == 65536:
> size = 1
> return self.real.read(size)
>
> def __getattr__(self, key):
> return getattr(self.real, key)
>
> def __enter__(self):
> self.real.__enter__()
> return self
>
> def __exit__(self, *args, **kwargs):
> return self.real.__exit__(*args, **kwargs)
>
> def opener(*args):
> o = vfs.vfs(*args)
> def wrapper(*a, **kwargs):
> f = o(*a, **kwargs)
> return singlebyteread(f)
> return wrapper
>
> cl = changelog.changelog(opener('.hg/store'))
> print(len(cl), 'revisions:')
> for r in cl:
> print(node.short(cl.node(r)))
> EOF
$ "$PYTHON" test.py
2 revisions:
7c31755bf9b5
26333235a41c
$ cd ..
#if no-pure
Test SEGV caused by bad revision passed to reachableroots() (issue4775):
$ cd a
$ "$PYTHON" <<EOF
> from __future__ import print_function
> from mercurial import changelog, vfs
> cl = changelog.changelog(vfs.vfs('.hg/store'))
> print('good heads:')
> for head in [0, len(cl) - 1, -1]:
> print('%s: %r' % (head, cl.reachableroots(0, [head], [0])))
> print('bad heads:')
> for head in [len(cl), 10000, -2, -10000, None]:
> print('%s:' % head, end=' ')
> try:
> cl.reachableroots(0, [head], [0])
> print('uncaught buffer overflow?')
> except (IndexError, TypeError) as inst:
> print(inst)
> print('good roots:')
> for root in [0, len(cl) - 1, -1]:
> print('%s: %r' % (root, cl.reachableroots(root, [len(cl) - 1], [root])))
> print('out-of-range roots are ignored:')
> for root in [len(cl), 10000, -2, -10000]:
> print('%s: %r' % (root, cl.reachableroots(root, [len(cl) - 1], [root])))
> print('bad roots:')
> for root in [None]:
> print('%s:' % root, end=' ')
> try:
> cl.reachableroots(root, [len(cl) - 1], [root])
> print('uncaught error?')
> except TypeError as inst:
> print(inst)
> EOF
good heads:
0: [0]
1: [0]
-1: []
bad heads:
2: head out of range
10000: head out of range
-2: head out of range
-10000: head out of range
None: an integer is required
good roots:
0: [0]
1: [1]
-1: [-1]
out-of-range roots are ignored:
2: []
10000: []
-2: []
-10000: []
bad roots:
None: an integer is required
$ cd ..
Test corrupted p1/p2 fields that could cause SEGV at parsers.c:
$ mkdir invalidparent
$ cd invalidparent
$ hg clone --pull -q --config phases.publish=False ../a limit
$ hg clone --pull -q --config phases.publish=False ../a segv
$ rm -R limit/.hg/cache segv/.hg/cache
$ "$PYTHON" <<EOF
> data = open("limit/.hg/store/00changelog.i", "rb").read()
> for n, p in [(b'limit', b'\0\0\0\x02'), (b'segv', b'\0\x01\0\0')]:
> # corrupt p1 at rev0 and p2 at rev1
> d = data[:24] + p + data[28:127 + 28] + p + data[127 + 32:]
> open(n + b"/.hg/store/00changelog.i", "wb").write(d)
> EOF
$ hg -R limit debugrevlogindex -f1 -c
rev flag size link p1 p2 nodeid
0 0000 62 0 2 -1 7c31755bf9b5
1 0000 65 1 0 2 26333235a41c
$ hg -R limit debugdeltachain -c
rev chain# chainlen prev delta size rawsize chainsize ratio lindist extradist extraratio
0 1 1 -1 base 63 62 63 1.01613 63 0 0.00000
1 2 1 -1 base 66 65 66 1.01538 66 0 0.00000
$ hg -R segv debugrevlogindex -f1 -c
rev flag size link p1 p2 nodeid
0 0000 62 0 65536 -1 7c31755bf9b5
1 0000 65 1 0 65536 26333235a41c
$ hg -R segv debugdeltachain -c
rev chain# chainlen prev delta size rawsize chainsize ratio lindist extradist extraratio
0 1 1 -1 base 63 62 63 1.01613 63 0 0.00000
1 2 1 -1 base 66 65 66 1.01538 66 0 0.00000
$ cat <<EOF > test.py
> from __future__ import print_function
> import sys
> from mercurial import changelog, vfs
> cl = changelog.changelog(vfs.vfs(sys.argv[1]))
> n0, n1 = cl.node(0), cl.node(1)
> ops = [
> ('reachableroots',
> lambda: cl.index.reachableroots2(0, [1], [0], False)),
> ('compute_phases_map_sets', lambda: cl.computephases([[0], []])),
> ('index_headrevs', lambda: cl.headrevs()),
> ('find_gca_candidates', lambda: cl.commonancestorsheads(n0, n1)),
> ('find_deepest', lambda: cl.ancestor(n0, n1)),
> ]
> for l, f in ops:
> print(l + ':', end=' ')
> try:
> f()
> print('uncaught buffer overflow?')
> except ValueError as inst:
> print(inst)
> EOF
$ "$PYTHON" test.py limit/.hg/store
reachableroots: parent out of range
compute_phases_map_sets: parent out of range
index_headrevs: parent out of range
find_gca_candidates: parent out of range
find_deepest: parent out of range
$ "$PYTHON" test.py segv/.hg/store
reachableroots: parent out of range
compute_phases_map_sets: parent out of range
index_headrevs: parent out of range
find_gca_candidates: parent out of range
find_deepest: parent out of range
$ cd ..
#endif