diff options
author | Jan Pokorný <jpokorny@redhat.com> | 2013-10-08 23:52:52 +0200 |
---|---|---|
committer | Jan Pokorný <jpokorny@redhat.com> | 2013-10-08 23:52:52 +0200 |
commit | ebec535c24763c0c8c31313a6a001560297fe085 (patch) | |
tree | 3f4b818d38c07c726002caf379f15e54489aed19 | |
parent | 1627b495355ddf90421847f0276bf64394023923 (diff) | |
download | patch-fix-offsets-ebec535c24763c0c8c31313a6a001560297fe085.tar.gz patch-fix-offsets-ebec535c24763c0c8c31313a6a001560297fe085.tar.xz patch-fix-offsets-ebec535c24763c0c8c31313a6a001560297fe085.zip |
Speed up a bit by threading
$ 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ý <jpokorny@redhat.com>
-rwxr-xr-x | fix-offsets | 66 |
1 files changed, 50 insertions, 16 deletions
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() |