summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2022-04-08 14:09:08 +1000
committerDamien George <damien@micropython.org>2022-04-14 23:52:14 +1000
commit07f526067e207f23529b4f2234baffd74ce1f35c (patch)
tree499e5bce01dbbb738c664240a7670956b595c8cf
parent40d431d1bbb7928ab399bf892e90cf232a83e2b9 (diff)
tools/mpy-tool.py: Intern more strings when freezing.
Signed-off-by: Damien George <damien@micropython.org>
-rwxr-xr-xtools/mpy-tool.py8
1 files changed, 8 insertions, 0 deletions
diff --git a/tools/mpy-tool.py b/tools/mpy-tool.py
index f4a8ef642..3ebbdd110 100755
--- a/tools/mpy-tool.py
+++ b/tools/mpy-tool.py
@@ -62,6 +62,11 @@ import struct
sys.path.append(sys.path[0] + "/../py")
import makeqstrdata as qstrutil
+# Threshold of str length below which it will be turned into a qstr when freezing.
+# This helps to reduce frozen code size because qstrs are more efficient to encode
+# as objects than full mp_obj_str_t instances.
+PERSISTENT_STR_INTERN_THRESHOLD = 25
+
class MPYReadError(Exception):
def __init__(self, filename, msg):
@@ -1187,6 +1192,9 @@ def read_obj(reader, segments):
reader.read_byte() # read and discard null terminator
if obj_type == MP_PERSISTENT_OBJ_STR:
obj = str_cons(buf, "utf8")
+ if len(obj) < PERSISTENT_STR_INTERN_THRESHOLD:
+ if not global_qstrs.find_by_str(obj):
+ global_qstrs.add(obj)
elif obj_type == MP_PERSISTENT_OBJ_BYTES:
obj = buf
elif obj_type == MP_PERSISTENT_OBJ_INT: