Skip to content

Commit 07f5260

Browse files
committed
tools/mpy-tool.py: Intern more strings when freezing.
Signed-off-by: Damien George <[email protected]>
1 parent 40d431d commit 07f5260

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

tools/mpy-tool.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ def hexlify_to_str(b):
6262
sys.path.append(sys.path[0] + "/../py")
6363
import makeqstrdata as qstrutil
6464

65+
# Threshold of str length below which it will be turned into a qstr when freezing.
66+
# This helps to reduce frozen code size because qstrs are more efficient to encode
67+
# as objects than full mp_obj_str_t instances.
68+
PERSISTENT_STR_INTERN_THRESHOLD = 25
69+
6570

6671
class MPYReadError(Exception):
6772
def __init__(self, filename, msg):
@@ -1187,6 +1192,9 @@ def read_obj(reader, segments):
11871192
reader.read_byte() # read and discard null terminator
11881193
if obj_type == MP_PERSISTENT_OBJ_STR:
11891194
obj = str_cons(buf, "utf8")
1195+
if len(obj) < PERSISTENT_STR_INTERN_THRESHOLD:
1196+
if not global_qstrs.find_by_str(obj):
1197+
global_qstrs.add(obj)
11901198
elif obj_type == MP_PERSISTENT_OBJ_BYTES:
11911199
obj = buf
11921200
elif obj_type == MP_PERSISTENT_OBJ_INT:

0 commit comments

Comments
 (0)