From ebec535c24763c0c8c31313a6a001560297fe085 Mon Sep 17 00:00:00 2001 From: Jan Pokorný Date: Tue, 8 Oct 2013 23:52:52 +0200 Subject: Speed up a bit by threading MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit $ time fix-offsets -p1 ../nongit1.patch > ../nongit2.patch before: > real 0m0.472s > user 0m0.202s > sys 0m0.262s after: > real 0m0.296s > user 0m0.188s > sys 0m0.217s Signed-off-by: Jan Pokorný --- fix-offsets | 66 ++++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 50 insertions(+), 16 deletions(-) (limited to 'fix-offsets') diff --git a/fix-offsets b/fix-offsets index 75b08f9..d9417b5 100755 --- a/fix-offsets +++ b/fix-offsets @@ -9,6 +9,7 @@ import re import getopt from os import environ from subprocess import Popen, PIPE +from threading import Thread # svn checkout http://python-patch.googlecode.com/svn/trunk/ python-patch # pushd python-patch @@ -23,6 +24,7 @@ re_h = re.compile( re.MULTILINE ) +use_threading = True def adjustment(ret, stderrdata): #print >>sys.stderr, "ret = {0}\nstderrdata = {1}".format(ret, stderrdata) @@ -35,12 +37,33 @@ def adjustment(ret, stderrdata): return ret +def hunk_worker(hunk, header, tres, cmd, **kwargs): + proc = Popen(cmd, **kwargs) + str_hunk = str(hunk) + partial_patch = header + '\n' + str_hunk + _, stderrdata = proc.communicate(partial_patch) + delta = adjustment(proc.wait(), stderrdata) + if delta: + hunk.startsrc += delta + hunk.starttgt += delta + str_hunk = str(hunk) + tres[hunk] = str_hunk + + def proceed(opts, args): - null = open('/dev/null', 'a') if not args: args.append('-') cmd_args = reduce(lambda a, x: a + list(x), opts, []) #print >>sys.stderr, "cmdargs: {0}, opts: {1}".format(cmd_args, opts) + + null = open('/dev/null', 'a') + cmd = (lambda *a: a)('/usr/bin/patch', '-o-', *cmd_args) + e = dict(LC='C') + for name, value in environ.iteritems(): + if name in ('TMPDIR', 'TMP', 'TEMP'): + e[name] = value + kwargs = dict(stdin=PIPE, stdout=null, stderr=PIPE, env=e) + for arg in args: arg = arg if arg != '-' else '/dev/stdin' ps = patch.fromfile(arg) @@ -51,22 +74,33 @@ def proceed(opts, args): header = '{0}--- {1}\n+++ {2}'.format('\n'.join(p.header), p.source, p.target) print header + tres = dict() + ts = set() + tmax = 10 for hunk in p.hunks: - cmd = (lambda *a: a)('/usr/bin/patch', '-o-', *cmd_args) - e = dict(LC='C') - for name, value in environ.iteritems(): - if name in ('TMPDIR', 'TMP', 'TEMP'): - e[name] = value - proc = Popen(cmd, stdin=PIPE, stdout=null, stderr=PIPE, env=e) - str_hunk = str(hunk) - partial_patch = header + '\n' + str_hunk - _, stderrdata = proc.communicate(partial_patch) - delta = adjustment(proc.wait(), stderrdata) - if delta: - hunk.startsrc += delta - hunk.starttgt += delta - str_hunk = str(hunk) - print str_hunk, + if not use_threading: + hunk_worker(hunk, header, tres, cmd, **kwargs) + continue + if len(ts) >= tmax: + while True: + for t in ts: + t.join(0.001) + if not t.is_alive(): + ts.remove(t) + break + else: + continue + break + t = Thread(target=hunk_worker, args=(hunk, header, tres, cmd), + kwargs=kwargs) + t.start() + ts.add(t) + if use_threading: + for t in ts: + t.join() + for hunk in p.hunks: + print tres[hunk], + null.close() -- cgit