changeset 3354:e60e194531e5

optimized EmbedResources.py (especially in speed: 2-3x faster but also in space: 20% smaller)
author Alain Mazy <alain@mazy.be>
date Tue, 23 Apr 2019 22:56:17 +0200
parents def55522f357
children f744730c294b
files Resources/EmbedResources.py
diffstat 1 files changed, 10 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- 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')