comparison Resources/EmbedResources.py @ 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 4e43e67f8ecf
children 76b3228f99b0
comparison
equal deleted inserted replaced
3352:def55522f357 3354:e60e194531e5
229 content = f.read() 229 content = f.read()
230 f.close() 230 f.close()
231 231
232 # http://stackoverflow.com/a/1035360 232 # http://stackoverflow.com/a/1035360
233 pos = 0 233 pos = 0
234 buffer = [] # instead of appending a few bytes at a time to the cpp file,
235 # we first append each chunk to a list, join it and write it
236 # to the file. We've measured that it was 2-3 times faster in python3.
237 # Note that speed is important since if generation is too slow,
238 # cmake might try to compile the EmbeddedResources.cpp file while it is
239 # still being generated !
234 for b in content: 240 for b in content:
235 if PYTHON_MAJOR_VERSION == 2: 241 if PYTHON_MAJOR_VERSION == 2:
236 c = ord(b[0]) 242 c = ord(b[0])
237 else: 243 else:
238 c = b 244 c = b
239 245
240 if pos > 0: 246 if pos > 0:
241 cpp.write(', ') 247 buffer.append(",")
242 248
243 if (pos % 16) == 0: 249 if (pos % 16) == 0:
244 cpp.write('\n ') 250 buffer.append("\n")
245 251
246 if c < 0: 252 if c < 0:
247 raise Exception("Internal error") 253 raise Exception("Internal error")
248 254
249 cpp.write("0x%02x" % c) 255 buffer.append("0x%02x" % c)
250 pos += 1 256 pos += 1
251 257
258 cpp.write("".join(buffer))
252 # Zero-size array are disallowed, so we put one single void character in it. 259 # Zero-size array are disallowed, so we put one single void character in it.
253 if pos == 0: 260 if pos == 0:
254 cpp.write(' 0') 261 cpp.write(' 0')
255 262
256 cpp.write(' };\n') 263 cpp.write(' };\n')