1337-diff: 3x speed improvement

This commit is contained in:
Vladislav Yarmak 2019-02-06 15:22:47 +02:00
parent 0fa7df74e1
commit 375972f14d

View File

@ -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):