Skip to content

Commit

Permalink
scripts: build: Optimize hex character list generation in file2hex.py
Browse files Browse the repository at this point in the history
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: Irfan Ahmad <[email protected]>
  • Loading branch information
hIAhmad authored and kartben committed Jan 28, 2025
1 parent ccd2fd6 commit e30c108
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions scripts/build/file2hex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit e30c108

Please sign in to comment.