mirror of
https://github.com/micropython/micropython.git
synced 2025-08-30 06:20:40 +02:00
py/qstr: Separate hash and len from string data.
This allows the compiler to merge strings: e.g. "update", "difference_update" and "symmetric_difference_update" will all point to the same memory. No functional change. The size reduction depends on the number of qstrs in the build. The change this commit brings is: bare-arm: -4 -0.007% minimal x86: +150 +0.092% [incl +48(data)] unix x64: -608 -0.118% unix nanbox: -572 -0.126% [incl +32(data)] stm32: -1392 -0.352% PYBV10 cc3200: -448 -0.244% esp8266: -1208 -0.173% GENERIC esp32: -1028 -0.068% GENERIC[incl -1020(data)] nrf: -440 -0.252% pca10040 rp2: -1072 -0.217% PICO samd: -368 -0.264% ADAFRUIT_ITSYBITSY_M4_EXPRESS Performance is also improved (on bare metal at least) for the core_import_mpy_multi.py, core_import_mpy_single.py and core_qstr.py performance benchmarks. Originally at adafruit#4583 Signed-off-by: Artyom Skrobov <tyomitch@gmail.com>
This commit is contained in:
committed by
Damien George
parent
e8bc4a3a5b
commit
18b1ba086c
@@ -317,26 +317,24 @@ def parse_input_headers(infiles):
|
||||
return qcfgs, qstrs
|
||||
|
||||
|
||||
def escape_bytes(qstr, qbytes):
|
||||
if all(32 <= ord(c) <= 126 and c != "\\" and c != '"' for c in qstr):
|
||||
# qstr is all printable ASCII so render it as-is (for easier debugging)
|
||||
return qstr
|
||||
else:
|
||||
# qstr contains non-printable codes so render entire thing as hex pairs
|
||||
return "".join(("\\x%02x" % b) for b in qbytes)
|
||||
|
||||
|
||||
def make_bytes(cfg_bytes_len, cfg_bytes_hash, qstr):
|
||||
qbytes = bytes_cons(qstr, "utf8")
|
||||
qlen = len(qbytes)
|
||||
qhash = compute_hash(qbytes, cfg_bytes_hash)
|
||||
if all(32 <= ord(c) <= 126 and c != "\\" and c != '"' for c in qstr):
|
||||
# qstr is all printable ASCII so render it as-is (for easier debugging)
|
||||
qdata = qstr
|
||||
else:
|
||||
# qstr contains non-printable codes so render entire thing as hex pairs
|
||||
qdata = "".join(("\\x%02x" % b) for b in qbytes)
|
||||
if qlen >= (1 << (8 * cfg_bytes_len)):
|
||||
print("qstr is too long:", qstr)
|
||||
assert False
|
||||
qlen_str = ("\\x%02x" * cfg_bytes_len) % tuple(
|
||||
((qlen >> (8 * i)) & 0xFF) for i in range(cfg_bytes_len)
|
||||
)
|
||||
qhash_str = ("\\x%02x" * cfg_bytes_hash) % tuple(
|
||||
((qhash >> (8 * i)) & 0xFF) for i in range(cfg_bytes_hash)
|
||||
)
|
||||
return '(const byte*)"%s%s" "%s"' % (qhash_str, qlen_str, qdata)
|
||||
qdata = escape_bytes(qstr, qbytes)
|
||||
return '%d, %d, "%s"' % (qhash, qlen, qdata)
|
||||
|
||||
|
||||
def print_qstr_data(qcfgs, qstrs):
|
||||
@@ -349,10 +347,7 @@ def print_qstr_data(qcfgs, qstrs):
|
||||
print("")
|
||||
|
||||
# add NULL qstr with no hash or data
|
||||
print(
|
||||
'QDEF(MP_QSTRnull, (const byte*)"%s%s" "")'
|
||||
% ("\\x00" * cfg_bytes_hash, "\\x00" * cfg_bytes_len)
|
||||
)
|
||||
print('QDEF(MP_QSTRnull, 0, 0, "")')
|
||||
|
||||
# go through each qstr and print it out
|
||||
for order, ident, qstr in sorted(qstrs.values(), key=lambda x: x[0]):
|
||||
|
Reference in New Issue
Block a user