Commit e30c1087 authored by Irfan Ahmad's avatar Irfan Ahmad Committed by Benjamin Cabé
Browse files

scripts: build: Optimize hex character list generation in file2hex.py



Read and convert binary data in chunks of 1024 bytes, instead of 8 bytes.
This significantly reduces the conversion time. The improvement increases
with increasing file sizes but saturates to around 60-61% as file size
approaches 64MiB, and beyond.

The existing generated output format of eight byte-values per line is still
preserved.

Signed-off-by: default avatarIrfan Ahmad <irfan.ahmad@siemens.com>
parent ccd2fd6c
Loading
Loading
Loading
Loading
+7 −3
Original line number Diff line number Diff line
@@ -43,7 +43,11 @@ def parse_args():


def get_nice_string(list_or_iterator):
    return ", ".join("0x" + str(x) for x in list_or_iterator)
    # Convert into comma separated list form.
    s = ", ".join("0x" + str(x) for x in list_or_iterator)

    # Format the list to eight values per line.
    return "\n".join(s[i:i+47] for i in range(0, len(s), 48))


def make_hex(chunk):
@@ -71,11 +75,11 @@ def main():
        with open(args.file, "rb") as fp:
            fp.seek(args.offset)
            if args.length < 0:
                for chunk in iter(lambda: fp.read(8), b''):
                for chunk in iter(lambda: fp.read(1024), b''):
                    make_hex(chunk)
            else:
                remainder = args.length
                for chunk in iter(lambda: fp.read(min(8, remainder)), b''):
                for chunk in iter(lambda: fp.read(min(1024, remainder)), b''):
                    make_hex(chunk)
                    remainder = remainder - len(chunk)