Mercurial > hg
view mercurial/treediscovery.py @ 50338:81c7d04f4722 stable
match: match explicit file using a set
The matcher as all the logic to do quick comparison against explicit patterns,
however the pattern matcher was shadowing the code using that set and used the
compiled regex pattern in all cases, which is quite slow. We restore the usage
of the set based matching to boost performance.
Building the regexp is still consuming a large amount of time (actually, the
majority of the time), which is still silly. Maybe using re2 would help that,
but this is a quest for another adventure.
Another path to improve this is to have a pattern type dedicated to match the
exact path to a file only (not a directory). This pattern could use the set
matching only and be skipped in the regex all together.
Benchmarks
==========
In the following benchmark we are comparing the `hg cat` and `hg files` run
time when matching against all files in the repository. They are
run:
- without the rust extensions
- with the standard python engine (so without re2)
Performance improvement in this series
--------------------------------------
###### hg files ###############################################################
### mercurial-2018-08-01-zstd-sparse-revlog ### sorted
base-changeset: 0.230092 seconds
prev-changeset: 0.230069 seconds
this-changeset: 0.211425 seconds (-8.36%)
### mercurial-2018-08-01-zstd-sparse-revlog ### shuffled
base-changeset: 0.234235 seconds
prev-changeset: 0.231165 seconds (-1.38%)
this-changeset: 0.212300 seconds (-9.43%)
### pypy-2018-08-01-zstd-sparse-revlog ### sorted
base-changeset: 0.613567 seconds
prev-changeset: 0.616799 seconds
this-changeset: 0.510852 seconds (-16.82%)
### pypy-2018-08-01-zstd-sparse-revlog ### shuffled
base-changeset: 0.801880 seconds
prev-changeset: 0.616393 seconds (-23.22%)
this-changeset: 0.511903 seconds (-36.23%)
### netbeans-2018-08-01-zstd-sparse-revlog ### sorted
base-changeset: 21.541828 seconds
prev-changeset: 21.586773 seconds
this-changeset: 13.648347 seconds (-36.76%)
### netbeans-2018-08-01-zstd-sparse-revlog ### shuffled
base-changeset: 172.759857 seconds
prev-changeset: 21.908197 seconds (-87.32%)
this-changeset: 13.945110 seconds (-91.93%)
### mozilla-central-2018-08-01-zstd-sparse-revlog ### sorted
base-changeset: 62.474221 seconds
prev-changeset: 61.279490 seconds (-1.22%)
this-changeset: 29.529469 seconds (-52.40%)
### mozilla-central-2018-08-01-zstd-sparse-revlog ### shuffled
base-changeset: 1364.180218 seconds
prev-changeset: 62.473549 seconds (-95.40%)
this-changeset: 30.625249 seconds (-97.75%)
###### hg cat #################################################################
### mercurial-2018-08-01-zstd-sparse-revlog ### sorted
base-changeset: 0.764407 seconds
prev-changeset: 0.763883 seconds
this-changeset: 0.737326 seconds (-3.68%)
### mercurial-2018-08-01-zstd-sparse-revlog ### shuffled
base-changeset: 0.768924 seconds
prev-changeset: 0.765848 seconds
this-changeset: 0.174d0b seconds (-4.44%)
### pypy-2018-08-01-zstd-sparse-revlog ### sorted
base-changeset: 2.065220 seconds
prev-changeset: 2.070498 seconds
this-changeset: 1.939482 seconds (-6.08%)
### pypy-2018-08-01-zstd-sparse-revlog ### shuffled
base-changeset: 2.276388 seconds
prev-changeset: 2.069197 seconds (-9.15%)
this-changeset: 1.931746 seconds (-15.19%)
### netbeans-2018-08-01-zstd-sparse-revlog ### sorted
base-changeset: 40.967983 seconds
prev-changeset: 41.392423 seconds
this-changeset: 32.181681 seconds (-22.20%)
### netbeans-2018-08-01-zstd-sparse-revlog ### shuffled
base-changeset: 216.388709 seconds
prev-changeset: 41.648689 seconds (-80.88%)
this-changeset: 32.580817 seconds (-85.04%)
### mozilla-central-2018-08-01-zstd-sparse-revlog ### sorted
base-changeset: 105.228510 seconds
prev-changeset: 103.315670 seconds (-1.23%)
this-changeset: 69.416118 seconds (-33.64%)
### mozilla-central-2018-08-01-zstd-sparse-revlog ### shuffled
base-changeset: 1448.722784 seconds
prev-changeset: 104.369358 seconds (-92.80%)
this-changeset: 70.554789 seconds (-95.13%)
Different way to list the same data with this revision
------------------------------------------------------
###### hg files ###############################################################
### mercurial-2018-08-01-zstd-sparse-revlog
root: 0.119182 seconds
glob: 0.120697 seconds (+1.27%)
sorted: 0.211425 seconds (+77.40%)
shuffled: 0.212300 seconds (+78.13%)
### pypy-2018-08-01-zstd-sparse-revlog
root: 0.121986 seconds
glob: 0.124822 seconds (+2.32%)
sorted: 0.510852 seconds (+318.78%)
shuffled: 0.511903 seconds (+319.64%)
### netbeans-2018-08-01-zstd-sparse-revlog
root: 0.173984 seconds
glob: 0.227203 seconds (+30.59%)
sorted: 13.648347 seconds (+7744.59%)
shuffled: 13.945110 seconds (+7915.16%)
### mozilla-central-2018-08-01-zstd-sparse-revlog
root: 0.366463 seconds
glob: 0.491030 seconds (+33.99%)
sorted: 29.529469 seconds (+7957.96%)
shuffled: 30.625249 seconds (+8256.97%)
###### hg cat #################################################################
### mercurial-2018-08-01-zstd-sparse-revlog
glob: 0.647471 seconds
root: 0.643120 seconds
shuffled: 0.174d0b seconds (+13.92%)
sorted: 0.737326 seconds (+13.88%)
### mozilla-central-2018-08-01-zstd-sparse-revlog
glob: 40.596983 seconds
root: 40.129136 seconds
shuffled: 70.554789 seconds (+73.79%)
sorted: 69.416118 seconds (+70.99%)
### netbeans-2018-08-01-zstd-sparse-revlog
glob: 18.777924 seconds
root: 18.613905 seconds
shuffled: 32.580817 seconds (+73.51%)
sorted: 32.181681 seconds (+71.38%)
### pypy-2018-08-01-zstd-sparse-revlog
glob: 1.555319 seconds
root: 1.536534 seconds
shuffled: 1.931746 seconds (+24.20%)
sorted: 1.939482 seconds (+24.70%)
author | Pierre-Yves David <pierre-yves.david@octobus.net> |
---|---|
date | Sat, 01 Apr 2023 05:58:59 +0200 |
parents | f64f66167afc |
children | f4733654f144 |
line wrap: on
line source
# discovery.py - protocol changeset discovery functions # # Copyright 2010 Olivia Mackall <olivia@selenic.com> # # This software may be used and distributed according to the terms of the # GNU General Public License version 2 or any later version. import collections from .i18n import _ from .node import short from . import ( error, ) def findcommonincoming(repo, remote, heads=None, force=False, audit=None): """Return a tuple (common, fetch, heads) used to identify the common subset of nodes between repo and remote. "common" is a list of (at least) the heads of the common subset. "fetch" is a list of roots of the nodes that would be incoming, to be supplied to changegroupsubset. "heads" is either the supplied heads, or else the remote's heads. """ knownnode = repo.changelog.hasnode search = [] fetch = set() seen = set() seenbranch = set() base = set() if not heads: with remote.commandexecutor() as e: heads = e.callcommand(b'heads', {}).result() if audit is not None: audit[b'total-roundtrips'] = 1 audit[b'total-roundtrips-heads'] = 1 audit[b'total-roundtrips-branches'] = 0 audit[b'total-roundtrips-between'] = 0 audit[b'total-queries'] = 0 audit[b'total-queries-branches'] = 0 audit[b'total-queries-between'] = 0 if repo.changelog.tip() == repo.nullid: base.add(repo.nullid) if heads != [repo.nullid]: return [repo.nullid], [repo.nullid], list(heads) return [repo.nullid], [], heads # assume we're closer to the tip than the root # and start by examining the heads repo.ui.status(_(b"searching for changes\n")) unknown = [] for h in heads: if not knownnode(h): unknown.append(h) else: base.add(h) if not unknown: return list(base), [], list(heads) req = set(unknown) reqcnt = 0 progress = repo.ui.makeprogress(_(b'searching'), unit=_(b'queries')) # search through remote branches # a 'branch' here is a linear segment of history, with four parts: # head, root, first parent, second parent # (a branch always has two parents (or none) by definition) with remote.commandexecutor() as e: if audit is not None: audit[b'total-queries'] += len(unknown) audit[b'total-queries-branches'] += len(unknown) audit[b'total-roundtrips'] += 1 audit[b'total-roundtrips-branches'] += 1 branches = e.callcommand(b'branches', {b'nodes': unknown}).result() unknown = collections.deque(branches) while unknown: r = [] while unknown: n = unknown.popleft() if n[0] in seen: continue repo.ui.debug(b"examining %s:%s\n" % (short(n[0]), short(n[1]))) if n[0] == repo.nullid: # found the end of the branch pass elif n in seenbranch: repo.ui.debug(b"branch already found\n") continue elif n[1] and knownnode(n[1]): # do we know the base? repo.ui.debug( b"found incomplete branch %s:%s\n" % (short(n[0]), short(n[1])) ) search.append(n[0:2]) # schedule branch range for scanning seenbranch.add(n) else: if n[1] not in seen and n[1] not in fetch: if knownnode(n[2]) and knownnode(n[3]): repo.ui.debug(b"found new changeset %s\n" % short(n[1])) fetch.add(n[1]) # earliest unknown for p in n[2:4]: if knownnode(p): base.add(p) # latest known for p in n[2:4]: if p not in req and not knownnode(p): r.append(p) req.add(p) seen.add(n[0]) if r: for p in range(0, len(r), 10): reqcnt += 1 progress.increment() if repo.ui.debugflag: msg = b"request %d: %s\n" msg %= (reqcnt, b" ".join(map(short, r))) repo.ui.debug(msg) with remote.commandexecutor() as e: subset = r[p : p + 10] if audit is not None: audit[b'total-queries'] += len(subset) audit[b'total-queries-branches'] += len(subset) audit[b'total-roundtrips'] += 1 audit[b'total-roundtrips-branches'] += 1 branches = e.callcommand( b'branches', { b'nodes': subset, }, ).result() for b in branches: repo.ui.debug( b"received %s:%s\n" % (short(b[0]), short(b[1])) ) unknown.append(b) # do binary search on the branches we found while search: newsearch = [] reqcnt += 1 progress.increment() with remote.commandexecutor() as e: if audit is not None: audit[b'total-queries'] += len(search) audit[b'total-queries-between'] += len(search) audit[b'total-roundtrips'] += 1 audit[b'total-roundtrips-between'] += 1 between = e.callcommand(b'between', {b'pairs': search}).result() for n, l in zip(search, between): l.append(n[1]) p = n[0] f = 1 for i in l: repo.ui.debug(b"narrowing %d:%d %s\n" % (f, len(l), short(i))) if knownnode(i): if f <= 2: repo.ui.debug( b"found new branch changeset %s\n" % short(p) ) fetch.add(p) base.add(i) else: repo.ui.debug( b"narrowed branch search to %s:%s\n" % (short(p), short(i)) ) newsearch.append((p, i)) break p, f = i, f * 2 search = newsearch # sanity check our fetch list for f in fetch: if knownnode(f): raise error.RepoError(_(b"already have changeset ") + short(f[:4])) base = list(base) if base == [repo.nullid]: if force: repo.ui.warn(_(b"warning: repository is unrelated\n")) else: raise error.Abort(_(b"repository is unrelated")) repo.ui.debug( b"found new changesets starting at " + b" ".join([short(f) for f in fetch]) + b"\n" ) progress.complete() repo.ui.debug(b"%d total queries\n" % reqcnt) return base, list(fetch), heads