view tests/test-rust-discovery.py @ 42743:8c9a6adec67a

rust-discovery: using the children cache in add_missing The DAG range computation often needs to get back to very old revisions, and turns out to be disproportionately long, given that the end goal is to remove the descendents of the given missing revisons from the undecided set. The fast iteration capabilities available in the Rust case make it possible to avoid the DAG range entirely, at the cost of precomputing the children cache, and to simply iterate on children of the given missing revisions. This is a case where staying on the same side of the interface between the two languages has clear benefits. On discoveries with initial undecided sets small enough to bypass sampling entirely, the total cost of computing the children cache and the subsequent iteration becomes better than the Python + C counterpart, which relies on reachableroots2. For example, on a repo with more than one million revisions with an initial undecided set of 11 elements, we get these figures: Rust version with simple iteration addcommons: 57.287us first undecided computation: 184.278334ms first children cache computation: 131.056us addmissings iteration: 42.766us first addinfo total: 185.24 ms Python + C version first addcommons: 0.29 ms addcommons 0.21 ms first undecided computation 191.35 ms addmissings 45.75 ms first addinfo total: 237.77 ms On discoveries with large undecided sets, the initial price paid makes the first addinfo slower than the Python + C version, but that's more than compensated by the gain in sampling and subsequent iterations. Here's an extreme example with an undecided set of a million revisions: Rust version: first undecided computation: 293.842629ms first children cache computation: 407.911297ms addmissings iteration: 34.312869ms first addinfo total: 776.02 ms taking initial sample query 2: sampling time: 1318.38 ms query 2; still undecided: 1005013, sample size is: 200 addmissings: 143.062us Python + C version: first undecided computation 298.13 ms addmissings 80.13 ms first addinfo total: 399.62 ms taking initial sample query 2: sampling time: 3957.23 ms query 2; still undecided: 1005013, sample size is: 200 addmissings 52.88 ms Differential Revision: https://phab.mercurial-scm.org/D6428
author Georges Racinet <georges.racinet@octobus.net>
date Tue, 16 Apr 2019 01:16:39 +0200
parents 4e7bd6180b53
children 791791a1fd4e
line wrap: on
line source

from __future__ import absolute_import
import unittest

from mercurial import policy

PartialDiscovery = policy.importrust('discovery', member='PartialDiscovery')

try:
    from mercurial.cext import parsers as cparsers
except ImportError:
    cparsers = None

# picked from test-parse-index2, copied rather than imported
# so that it stays stable even if test-parse-index2 changes or disappears.
data_non_inlined = (
    b'\x00\x00\x00\x01\x00\x00\x00\x00\x00\x01D\x19'
    b'\x00\x07e\x12\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff'
    b'\xff\xff\xff\xff\xd1\xf4\xbb\xb0\xbe\xfc\x13\xbd\x8c\xd3\x9d'
    b'\x0f\xcd\xd9;\x8c\x07\x8cJ/\x00\x00\x00\x00\x00\x00\x00\x00\x00'
    b'\x00\x00\x00\x00\x00\x00\x01D\x19\x00\x00\x00\x00\x00\xdf\x00'
    b'\x00\x01q\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\xff'
    b'\xff\xff\xff\xc1\x12\xb9\x04\x96\xa4Z1t\x91\xdfsJ\x90\xf0\x9bh'
    b'\x07l&\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
    b'\x00\x01D\xf8\x00\x00\x00\x00\x01\x1b\x00\x00\x01\xb8\x00\x00'
    b'\x00\x01\x00\x00\x00\x02\x00\x00\x00\x01\xff\xff\xff\xff\x02\n'
    b'\x0e\xc6&\xa1\x92\xae6\x0b\x02i\xfe-\xe5\xbao\x05\xd1\xe7\x00'
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01F'
    b'\x13\x00\x00\x00\x00\x01\xec\x00\x00\x03\x06\x00\x00\x00\x01'
    b'\x00\x00\x00\x03\x00\x00\x00\x02\xff\xff\xff\xff\x12\xcb\xeby1'
    b'\xb6\r\x98B\xcb\x07\xbd`\x8f\x92\xd9\xc4\x84\xbdK\x00\x00\x00'
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x00'
    )

class fakerepo(object):
    def __init__(self, idx):
        """Just make so that self.changelog.index is the given idx."""
        self.index = idx
        self.changelog = self

@unittest.skipIf(PartialDiscovery is None or cparsers is None,
                 "rustext or the C Extension parsers module "
                 "discovery relies on is not available")
class rustdiscoverytest(unittest.TestCase):
    """Test the correctness of binding to Rust code.

    This test is merely for the binding to Rust itself: extraction of
    Python variable, giving back the results etc.

    It is not meant to test the algorithmic correctness of the provided
    methods. Hence the very simple embedded index data is good enough.

    Algorithmic correctness is asserted by the Rust unit tests.
    """

    def parseindex(self):
        return cparsers.parse_index2(data_non_inlined, False)[0]

    def repo(self):
        return fakerepo(self.parseindex())

    def testindex(self):
        idx = self.parseindex()
        # checking our assumptions about the index binary data:
        self.assertEqual({i: (r[5], r[6]) for i, r in enumerate(idx)},
                         {0: (-1, -1),
                          1: (0, -1),
                          2: (1, -1),
                          3: (2, -1)})

    def testaddcommonsmissings(self):
        disco = PartialDiscovery(self.repo(), [3], True)
        self.assertFalse(disco.hasinfo())
        self.assertFalse(disco.iscomplete())

        disco.addcommons([1])
        self.assertTrue(disco.hasinfo())
        self.assertFalse(disco.iscomplete())

        disco.addmissings([2])
        self.assertTrue(disco.hasinfo())
        self.assertTrue(disco.iscomplete())

        self.assertEqual(disco.commonheads(), {1})

    def testaddmissingsstats(self):
        disco = PartialDiscovery(self.repo(), [3], True)
        self.assertIsNone(disco.stats()['undecided'], None)

        disco.addmissings([2])
        self.assertEqual(disco.stats()['undecided'], 2)

    def testaddinfocommonfirst(self):
        disco = PartialDiscovery(self.repo(), [3], True)
        disco.addinfo([(1, True), (2, False)])
        self.assertTrue(disco.hasinfo())
        self.assertTrue(disco.iscomplete())
        self.assertEqual(disco.commonheads(), {1})

    def testaddinfomissingfirst(self):
        disco = PartialDiscovery(self.repo(), [3], True)
        disco.addinfo([(2, False), (1, True)])
        self.assertTrue(disco.hasinfo())
        self.assertTrue(disco.iscomplete())
        self.assertEqual(disco.commonheads(), {1})

    def testinitnorandom(self):
        PartialDiscovery(self.repo(), [3], True, randomize=False)

if __name__ == '__main__':
    import silenttestrunner
    silenttestrunner.main(__name__)