tests/test-hashutil.py
author Arun Kulshreshtha <akulshreshtha@janestreet.com>
Tue, 30 Aug 2022 15:29:55 -0400
changeset 49491 c6a1beba27e9
parent 48946 642e31cb55f0
permissions -rw-r--r--
bisect: avoid copying ancestor list for non-merge commits During a bisection, hg needs to compute a list of all ancestors for every candidate commit. This is accomplished via a bottom-up traversal of the set of candidates, during which each revision's ancestor list is populated using the ancestor list of its parent(s). Previously, this involved copying the entire list, which could be very long in if the bisection range was large. To help improve this, we can observe that each candidate commit is visited exactly once, at which point its ancestor list is copied into its children's lists and then dropped. In the case of non-merge commits, a commit's ancestor list consists exactly of its parent's list plus itself. This means that we can trivially reuse the parent's existing list for one of its non-merge children, which avoids copying entirely if that commit is the parent's only child. This makes bisections over linear ranges of commits much faster. During some informal testing in the large publicly-available `mozilla-central` repository, this noticeably sped up bisections over large ranges of history: Setup: $ cd mozilla-central $ hg bisect --reset $ hg bisect --good 0 $ hg log -r tip -T '{rev}\n' 628417 Test: $ time hg bisect --bad tip --noupdate Before: real 3m35.927s user 3m35.553s sys 0m0.319s After: real 1m41.142s user 1m40.810s sys 0m0.285s

# Tests to ensure that sha1dc.sha1 is exactly a drop-in for
# hashlib.sha1 for our needs.

import hashlib
import unittest

import silenttestrunner

try:
    from mercurial.thirdparty import sha1dc
except ImportError:
    sha1dc = None


class hashertestsbase:
    def test_basic_hash(self):
        h = self.hasher()
        h.update(b'foo')
        self.assertEqual(
            '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33', h.hexdigest()
        )
        h.update(b'bar')
        self.assertEqual(
            '8843d7f92416211de9ebb963ff4ce28125932878', h.hexdigest()
        )

    def test_copy_hasher(self):
        h = self.hasher()
        h.update(b'foo')
        h2 = h.copy()
        h.update(b'baz')
        h2.update(b'bar')
        self.assertEqual(
            '21eb6533733a5e4763acacd1d45a60c2e0e404e1', h.hexdigest()
        )
        self.assertEqual(
            '8843d7f92416211de9ebb963ff4ce28125932878', h2.hexdigest()
        )

    def test_init_hasher(self):
        h = self.hasher(b'initial string')
        self.assertEqual(
            b'\xc9y|n\x1f3S\xa4:\xbaJ\xca,\xc1\x1a\x9e\xb8\xd8\xdd\x86',
            h.digest(),
        )

    def test_bytes_like_types(self):
        h = self.hasher()
        h.update(bytearray(b'foo'))
        h.update(memoryview(b'baz'))
        self.assertEqual(
            '21eb6533733a5e4763acacd1d45a60c2e0e404e1', h.hexdigest()
        )

        h = self.hasher(bytearray(b'foo'))
        h.update(b'baz')
        self.assertEqual(
            '21eb6533733a5e4763acacd1d45a60c2e0e404e1', h.hexdigest()
        )

        h = self.hasher(memoryview(b'foo'))
        h.update(b'baz')
        self.assertEqual(
            '21eb6533733a5e4763acacd1d45a60c2e0e404e1', h.hexdigest()
        )


class hashlibtests(unittest.TestCase, hashertestsbase):
    hasher = hashlib.sha1


if sha1dc:

    class sha1dctests(unittest.TestCase, hashertestsbase):
        hasher = sha1dc.sha1


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