##// END OF EJS Templates
branches: improve performance by removing redundant operations...
branches: improve performance by removing redundant operations This refactors the branches command so it collects all the information it needs about a branch in one pass over the branch map. In particular, it fixes an issue where the command called repo.branchtags() to get branch tips, and then used repo.branchheads() to get the closed/open status. Both repo methods read the changelog to determine if the branch is closed, resulting in extra, redundant I/O. For the PyPy repo with 744 branches and 843 branch heads, this brings hg branches over NFS from: CallCount Recursive Total(ms) Inline(ms) module:lineno(function) 2427 0 0.9057 0.9057 <open> 2424 0 0.4096 0.4096 <method 'close' of 'file' objects> 2424 0 0.0476 0.0476 <method 'read' of 'file' objects> 1 0 0.0468 0.0468 mercurial.revlog:637(headrevs) +1 0 0.0000 0.0000 +<len> 2422 0 0.0443 0.0443 <method 'seek' of 'file' objects> 2962 0 0.0337 0.0337 <zlib.decompress> 2491 0 1.8008 0.0322 mercurial.changelog:182(read) +2491 0 1.6982 0.0315 +mercurial.revlog:881(revision) +2488 0 0.0269 0.0134 +mercurial.changelog:28(decodeextra) +4982 0 0.0085 0.0085 +<method 'split' of 'str' objects> +4982 0 0.0274 0.0049 +mercurial.encoding:61(tolocal) +2491 0 0.0039 0.0039 +<method 'index' of 'str' objects> 2491 0 1.6982 0.0315 mercurial.revlog:881(revision) +2413 0 0.0112 0.0112 +mercurial.revlog:305(rev) +2491 0 1.5315 0.0068 +mercurial.revlog:847(_chunkraw) +2491 0 0.0414 0.0058 +mercurial.revlog:945(_checkhash) +2491 0 0.0028 0.0028 +mercurial.revlog:349(flags) +2491 0 0.0025 0.0025 +<mercurial.mpatch.patches> 2422 0 1.5089 0.0286 mercurial.revlog:818(_loadchunk) +2422 0 0.4093 0.4093 +<method 'close' of 'file' objects> +2422 0 0.0451 0.0451 +<method 'read' of 'file' objects> +2422 0 0.0443 0.0443 +<method 'seek' of 'file' objects> +2422 0 0.9703 0.0096 +mercurial.store:374(__call__) +2422 0 0.0079 0.0069 +mercurial.revlog:810(_addchunk) 5804 0 0.0204 0.0204 mercurial.revlog:305(rev) 2426 0 0.9552 0.0177 mercurial.scmutil:218(__call__) +2426 0 0.9057 0.9057 +<open> +2426 0 0.0120 0.0083 +os.path:80(split) +2426 0 0.0061 0.0049 +mercurial.scmutil:92(__call__) Time: real 1.950 secs (user 0.560+0.000 sys 0.220+0.000) down to: CallCount Recursive Total(ms) Inline(ms) module:lineno(function) 1545 0 0.6035 0.6035 <open> 1542 0 0.2697 0.2697 <method 'close' of 'file' objects> 1 0 0.0547 0.0547 mercurial.revlog:637(headrevs) +1 0 0.0000 0.0000 +<len> 1542 0 0.0389 0.0389 <method 'read' of 'file' objects> 1540 0 0.0316 0.0316 <method 'seek' of 'file' objects> 1853 0 0.0227 0.0227 <zlib.decompress> 1557 0 1.2131 0.0226 mercurial.changelog:182(read) +1557 0 1.1398 0.0221 +mercurial.revlog:881(revision) +1555 0 0.0199 0.0094 +mercurial.changelog:28(decodeextra) +3114 0 0.0058 0.0058 +<method 'split' of 'str' objects> +3114 0 0.0196 0.0035 +mercurial.encoding:61(tolocal) +1557 0 0.0026 0.0026 +<method 'index' of 'str' objects> 1557 0 1.1398 0.0221 mercurial.revlog:881(revision) +1557 0 1.0307 0.0047 +mercurial.revlog:847(_chunkraw) +1557 0 0.0287 0.0040 +mercurial.revlog:945(_checkhash) +1557 0 0.0018 0.0018 +<mercurial.mpatch.patches> +1557 0 0.0018 0.0018 +mercurial.revlog:326(node) +1557 0 0.0417 0.0013 +mercurial.revlog:857(_chunkbase) 1540 0 1.0147 0.0210 mercurial.revlog:818(_loadchunk) +1540 0 0.2693 0.2693 +<method 'close' of 'file' objects> +1540 0 0.0360 0.0360 +<method 'read' of 'file' objects> +1540 0 0.0316 0.0316 +<method 'seek' of 'file' objects> +1540 0 0.6487 0.0070 +mercurial.store:374(__call__) +1540 0 0.0059 0.0052 +mercurial.revlog:810(_addchunk) 3192 0 0.0173 0.0173 mercurial.revlog:305(rev) 8184 0 0.0300 0.0147 <method 'decode' of 'str' objects> +5204 0 0.0149 0.0048 +encodings.utf_8:15(decode) +1 0 0.0004 0.0000 +encodings:71(search_function) 43 26 0.0147 0.0129 <__import__> Time: real 1.390 secs (user 0.450+0.000 sys 0.170+0.000)

File last commit:

r15381:c519cd8f stable
r16721:3e6d59ae default
Show More
gnuarch.py
338 lines | 12.4 KiB | text/x-python | PythonLexer
# gnuarch.py - GNU Arch support for the convert extension
#
# Copyright 2008, 2009 Aleix Conchillo Flaque <aleix@member.fsf.org>
# and others
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
from common import NoRepo, commandline, commit, converter_source
from mercurial.i18n import _
from mercurial import encoding, util
import os, shutil, tempfile, stat
from email.Parser import Parser
class gnuarch_source(converter_source, commandline):
class gnuarch_rev(object):
def __init__(self, rev):
self.rev = rev
self.summary = ''
self.date = None
self.author = ''
self.continuationof = None
self.add_files = []
self.mod_files = []
self.del_files = []
self.ren_files = {}
self.ren_dirs = {}
def __init__(self, ui, path, rev=None):
super(gnuarch_source, self).__init__(ui, path, rev=rev)
if not os.path.exists(os.path.join(path, '{arch}')):
raise NoRepo(_("%s does not look like a GNU Arch repository")
% path)
# Could use checktool, but we want to check for baz or tla.
self.execmd = None
if util.findexe('baz'):
self.execmd = 'baz'
else:
if util.findexe('tla'):
self.execmd = 'tla'
else:
raise util.Abort(_('cannot find a GNU Arch tool'))
commandline.__init__(self, ui, self.execmd)
self.path = os.path.realpath(path)
self.tmppath = None
self.treeversion = None
self.lastrev = None
self.changes = {}
self.parents = {}
self.tags = {}
self.catlogparser = Parser()
self.encoding = encoding.encoding
self.archives = []
def before(self):
# Get registered archives
self.archives = [i.rstrip('\n')
for i in self.runlines0('archives', '-n')]
if self.execmd == 'tla':
output = self.run0('tree-version', self.path)
else:
output = self.run0('tree-version', '-d', self.path)
self.treeversion = output.strip()
# Get name of temporary directory
version = self.treeversion.split('/')
self.tmppath = os.path.join(tempfile.gettempdir(),
'hg-%s' % version[1])
# Generate parents dictionary
self.parents[None] = []
treeversion = self.treeversion
child = None
while treeversion:
self.ui.status(_('analyzing tree version %s...\n') % treeversion)
archive = treeversion.split('/')[0]
if archive not in self.archives:
self.ui.status(_('tree analysis stopped because it points to '
'an unregistered archive %s...\n') % archive)
break
# Get the complete list of revisions for that tree version
output, status = self.runlines('revisions', '-r', '-f', treeversion)
self.checkexit(status, 'failed retrieveing revisions for %s'
% treeversion)
# No new iteration unless a revision has a continuation-of header
treeversion = None
for l in output:
rev = l.strip()
self.changes[rev] = self.gnuarch_rev(rev)
self.parents[rev] = []
# Read author, date and summary
catlog, status = self.run('cat-log', '-d', self.path, rev)
if status:
catlog = self.run0('cat-archive-log', rev)
self._parsecatlog(catlog, rev)
# Populate the parents map
self.parents[child].append(rev)
# Keep track of the current revision as the child of the next
# revision scanned
child = rev
# Check if we have to follow the usual incremental history
# or if we have to 'jump' to a different treeversion given
# by the continuation-of header.
if self.changes[rev].continuationof:
treeversion = '--'.join(
self.changes[rev].continuationof.split('--')[:-1])
break
# If we reached a base-0 revision w/o any continuation-of
# header, it means the tree history ends here.
if rev[-6:] == 'base-0':
break
def after(self):
self.ui.debug('cleaning up %s\n' % self.tmppath)
shutil.rmtree(self.tmppath, ignore_errors=True)
def getheads(self):
return self.parents[None]
def getfile(self, name, rev):
if rev != self.lastrev:
raise util.Abort(_('internal calling inconsistency'))
# Raise IOError if necessary (i.e. deleted files).
if not os.path.lexists(os.path.join(self.tmppath, name)):
raise IOError
return self._getfile(name, rev)
def getchanges(self, rev):
self._update(rev)
changes = []
copies = {}
for f in self.changes[rev].add_files:
changes.append((f, rev))
for f in self.changes[rev].mod_files:
changes.append((f, rev))
for f in self.changes[rev].del_files:
changes.append((f, rev))
for src in self.changes[rev].ren_files:
to = self.changes[rev].ren_files[src]
changes.append((src, rev))
changes.append((to, rev))
copies[to] = src
for src in self.changes[rev].ren_dirs:
to = self.changes[rev].ren_dirs[src]
chgs, cps = self._rendirchanges(src, to)
changes += [(f, rev) for f in chgs]
copies.update(cps)
self.lastrev = rev
return sorted(set(changes)), copies
def getcommit(self, rev):
changes = self.changes[rev]
return commit(author=changes.author, date=changes.date,
desc=changes.summary, parents=self.parents[rev], rev=rev)
def gettags(self):
return self.tags
def _execute(self, cmd, *args, **kwargs):
cmdline = [self.execmd, cmd]
cmdline += args
cmdline = [util.shellquote(arg) for arg in cmdline]
cmdline += ['>', util.nulldev, '2>', util.nulldev]
cmdline = util.quotecommand(' '.join(cmdline))
self.ui.debug(cmdline, '\n')
return os.system(cmdline)
def _update(self, rev):
self.ui.debug('applying revision %s...\n' % rev)
changeset, status = self.runlines('replay', '-d', self.tmppath,
rev)
if status:
# Something went wrong while merging (baz or tla
# issue?), get latest revision and try from there
shutil.rmtree(self.tmppath, ignore_errors=True)
self._obtainrevision(rev)
else:
old_rev = self.parents[rev][0]
self.ui.debug('computing changeset between %s and %s...\n'
% (old_rev, rev))
self._parsechangeset(changeset, rev)
def _getfile(self, name, rev):
mode = os.lstat(os.path.join(self.tmppath, name)).st_mode
if stat.S_ISLNK(mode):
data = os.readlink(os.path.join(self.tmppath, name))
mode = mode and 'l' or ''
else:
data = open(os.path.join(self.tmppath, name), 'rb').read()
mode = (mode & 0111) and 'x' or ''
return data, mode
def _exclude(self, name):
exclude = ['{arch}', '.arch-ids', '.arch-inventory']
for exc in exclude:
if name.find(exc) != -1:
return True
return False
def _readcontents(self, path):
files = []
contents = os.listdir(path)
while len(contents) > 0:
c = contents.pop()
p = os.path.join(path, c)
# os.walk could be used, but here we avoid internal GNU
# Arch files and directories, thus saving a lot time.
if not self._exclude(p):
if os.path.isdir(p):
contents += [os.path.join(c, f) for f in os.listdir(p)]
else:
files.append(c)
return files
def _rendirchanges(self, src, dest):
changes = []
copies = {}
files = self._readcontents(os.path.join(self.tmppath, dest))
for f in files:
s = os.path.join(src, f)
d = os.path.join(dest, f)
changes.append(s)
changes.append(d)
copies[d] = s
return changes, copies
def _obtainrevision(self, rev):
self.ui.debug('obtaining revision %s...\n' % rev)
output = self._execute('get', rev, self.tmppath)
self.checkexit(output)
self.ui.debug('analyzing revision %s...\n' % rev)
files = self._readcontents(self.tmppath)
self.changes[rev].add_files += files
def _stripbasepath(self, path):
if path.startswith('./'):
return path[2:]
return path
def _parsecatlog(self, data, rev):
try:
catlog = self.catlogparser.parsestr(data)
# Commit date
self.changes[rev].date = util.datestr(
util.strdate(catlog['Standard-date'],
'%Y-%m-%d %H:%M:%S'))
# Commit author
self.changes[rev].author = self.recode(catlog['Creator'])
# Commit description
self.changes[rev].summary = '\n\n'.join((catlog['Summary'],
catlog.get_payload()))
self.changes[rev].summary = self.recode(self.changes[rev].summary)
# Commit revision origin when dealing with a branch or tag
if 'Continuation-of' in catlog:
self.changes[rev].continuationof = self.recode(
catlog['Continuation-of'])
except Exception:
raise util.Abort(_('could not parse cat-log of %s') % rev)
def _parsechangeset(self, data, rev):
for l in data:
l = l.strip()
# Added file (ignore added directory)
if l.startswith('A') and not l.startswith('A/'):
file = self._stripbasepath(l[1:].strip())
if not self._exclude(file):
self.changes[rev].add_files.append(file)
# Deleted file (ignore deleted directory)
elif l.startswith('D') and not l.startswith('D/'):
file = self._stripbasepath(l[1:].strip())
if not self._exclude(file):
self.changes[rev].del_files.append(file)
# Modified binary file
elif l.startswith('Mb'):
file = self._stripbasepath(l[2:].strip())
if not self._exclude(file):
self.changes[rev].mod_files.append(file)
# Modified link
elif l.startswith('M->'):
file = self._stripbasepath(l[3:].strip())
if not self._exclude(file):
self.changes[rev].mod_files.append(file)
# Modified file
elif l.startswith('M'):
file = self._stripbasepath(l[1:].strip())
if not self._exclude(file):
self.changes[rev].mod_files.append(file)
# Renamed file (or link)
elif l.startswith('=>'):
files = l[2:].strip().split(' ')
if len(files) == 1:
files = l[2:].strip().split('\t')
src = self._stripbasepath(files[0])
dst = self._stripbasepath(files[1])
if not self._exclude(src) and not self._exclude(dst):
self.changes[rev].ren_files[src] = dst
# Conversion from file to link or from link to file (modified)
elif l.startswith('ch'):
file = self._stripbasepath(l[2:].strip())
if not self._exclude(file):
self.changes[rev].mod_files.append(file)
# Renamed directory
elif l.startswith('/>'):
dirs = l[2:].strip().split(' ')
if len(dirs) == 1:
dirs = l[2:].strip().split('\t')
src = self._stripbasepath(dirs[0])
dst = self._stripbasepath(dirs[1])
if not self._exclude(src) and not self._exclude(dst):
self.changes[rev].ren_dirs[src] = dst