From 375972f14d1eb171aaebf434efdc2008e4873a48 Mon Sep 17 00:00:00 2001 From: Vladislav Yarmak Date: Wed, 6 Feb 2019 15:22:47 +0200 Subject: [PATCH] 1337-diff: 3x speed improvement --- win/tools/1337-diff/1337-diff.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/win/tools/1337-diff/1337-diff.py b/win/tools/1337-diff/1337-diff.py index c9ab947..a1c196d 100755 --- a/win/tools/1337-diff/1337-diff.py +++ b/win/tools/1337-diff/1337-diff.py @@ -64,24 +64,29 @@ def feed_chunks(f, chunk_size=4096): yield buf -def zip_files_bytes(*files): +def zip_files_bytes(left, right): """ Iterate over two files, returning pair of bytes. Throw LengthMismatch if file sizes is uneven. """ class EndMarker(object): pass end_marker = EndMarker() - iterators = (itertools.chain.from_iterable(feed_chunks(f)) for f in files) - for tup in itertools.zip_longest(*iterators, fillvalue=end_marker): - if any(v is end_marker for v in tup): + left_iter = itertools.chain.from_iterable(feed_chunks(left)) + right_iter = itertools.chain.from_iterable(feed_chunks(right)) + for a, b in itertools.zip_longest(left_iter, + right_iter, + fillvalue=end_marker): + if a is end_marker or b is end_marker: raise LengthMismatchException("Length of input files inequal.") - yield tup + yield a, b def diff(left, right): - for offset, (a, b) in enumerate(zip_files_bytes(left, right)): + offset = 0 + for a, b in zip_files_bytes(left, right): if a != b: yield offset, a, b + offset += 1 def compose_diff_file(orig, patched, output, header, offset_adjustment=True):