# HG changeset patch # User Alain Mazy # Date 1556052977 -7200 # Node ID e60e194531e5c72bb5e7788c8a8ce6e8b877f7a5 # Parent def55522f3578332c73baa1199d3d77bf8030ec9 optimized EmbedResources.py (especially in speed: 2-3x faster but also in space: 20% smaller) diff -r def55522f357 -r e60e194531e5 Resources/EmbedResources.py --- a/Resources/EmbedResources.py Tue Apr 23 10:33:19 2019 +0200 +++ b/Resources/EmbedResources.py Tue Apr 23 22:56:17 2019 +0200 @@ -231,6 +231,12 @@ # http://stackoverflow.com/a/1035360 pos = 0 + buffer = [] # instead of appending a few bytes at a time to the cpp file, + # we first append each chunk to a list, join it and write it + # to the file. We've measured that it was 2-3 times faster in python3. + # Note that speed is important since if generation is too slow, + # cmake might try to compile the EmbeddedResources.cpp file while it is + # still being generated ! for b in content: if PYTHON_MAJOR_VERSION == 2: c = ord(b[0]) @@ -238,17 +244,18 @@ c = b if pos > 0: - cpp.write(', ') + buffer.append(",") if (pos % 16) == 0: - cpp.write('\n ') + buffer.append("\n") if c < 0: raise Exception("Internal error") - cpp.write("0x%02x" % c) + buffer.append("0x%02x" % c) pos += 1 + cpp.write("".join(buffer)) # Zero-size array are disallowed, so we put one single void character in it. if pos == 0: cpp.write(' 0')