aboutsummaryrefslogtreecommitdiff
path: root/py/makeqstrdata.py
diff options
context:
space:
mode:
authorArtyom Skrobov <tyomitch@gmail.com>2021-05-03 14:17:36 -0400
committerDamien George <damien@micropython.org>2022-02-11 22:52:32 +1100
commit18b1ba086c0e5547ca81030bf13b026961f80720 (patch)
tree2eda1e61c4c61053b6ddd2f1c53e434854dadbd0 /py/makeqstrdata.py
parente8bc4a3a5be12ad639b811852337c63e8f1d6277 (diff)
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>
Diffstat (limited to 'py/makeqstrdata.py')
-rw-r--r--py/makeqstrdata.py29
1 files changed, 12 insertions, 17 deletions
diff --git a/py/makeqstrdata.py b/py/makeqstrdata.py
index 403c40688..e332ab94e 100644
--- a/py/makeqstrdata.py
+++ b/py/makeqstrdata.py
@@ -317,26 +317,24 @@ def parse_input_headers(infiles):
return qcfgs, qstrs
-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)
+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)
- qdata = qstr
+ return qstr
else:
# qstr contains non-printable codes so render entire thing as hex pairs
- qdata = "".join(("\\x%02x" % b) for b in qbytes)
+ 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 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]):