aboutsummaryrefslogtreecommitdiff
path: root/py/makeqstrdata.py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2019-03-01 14:33:03 +1100
committerDamien George <damien.p.george@gmail.com>2019-03-05 16:32:05 +1100
commit4f0931b21f72be86aae22f05b718aa36792afc9b (patch)
tree6ed029f6bc5ef4ee9b125f50e7795220232d5f14 /py/makeqstrdata.py
parent992a6e1deab06aba07ab09687402d39a0c028a73 (diff)
py/persistentcode: Define static qstr set to reduce size of mpy files.
When encoded in the mpy file, if qstr <= QSTR_LAST_STATIC then store two bytes: 0, static_qstr_id. Otherwise encode the qstr as usual (either with string data or a reference into the qstr window). Reduces mpy file size by about 5%.
Diffstat (limited to 'py/makeqstrdata.py')
-rw-r--r--py/makeqstrdata.py185
1 files changed, 184 insertions, 1 deletions
diff --git a/py/makeqstrdata.py b/py/makeqstrdata.py
index 3c0a60909..060ebb7fd 100644
--- a/py/makeqstrdata.py
+++ b/py/makeqstrdata.py
@@ -51,6 +51,176 @@ codepoint2name[ord('^')] = 'caret'
codepoint2name[ord('|')] = 'pipe'
codepoint2name[ord('~')] = 'tilde'
+# static qstrs, should be sorted
+
+static_qstr_list = [
+ "",
+ "__dir__", # Put __dir__ after empty qstr for builtin dir() to work
+ "\n",
+ " ",
+ "*",
+ "/",
+ "<module>",
+ "_",
+ "__call__",
+ "__class__",
+ "__delitem__",
+ "__enter__",
+ "__exit__",
+ "__getattr__",
+ "__getitem__",
+ "__hash__",
+ "__init__",
+ "__int__",
+ "__iter__",
+ "__len__",
+ "__main__",
+ "__module__",
+ "__name__",
+ "__new__",
+ "__next__",
+ "__qualname__",
+ "__repr__",
+ "__setitem__",
+ "__str__",
+ "ArithmeticError",
+ "AssertionError",
+ "AttributeError",
+ "BaseException",
+ "EOFError",
+ "Ellipsis",
+ "Exception",
+ "GeneratorExit",
+ "ImportError",
+ "IndentationError",
+ "IndexError",
+ "KeyError",
+ "KeyboardInterrupt",
+ "LookupError",
+ "MemoryError",
+ "NameError",
+ "NoneType",
+ "NotImplementedError",
+ "OSError",
+ "OverflowError",
+ "RuntimeError",
+ "StopIteration",
+ "SyntaxError",
+ "SystemExit",
+ "TypeError",
+ "ValueError",
+ "ZeroDivisionError",
+ "abs",
+ "all",
+ "any",
+ "append",
+ "args",
+ "bool",
+ "builtins",
+ "bytearray",
+ "bytecode",
+ "bytes",
+ "callable",
+ "chr",
+ "classmethod",
+ "clear",
+ "close",
+ "const",
+ "copy",
+ "count",
+ "dict",
+ "dir",
+ "divmod",
+ "end",
+ "endswith",
+ "eval",
+ "exec",
+ "extend",
+ "find",
+ "format",
+ "from_bytes",
+ "get",
+ "getattr",
+ "globals",
+ "hasattr",
+ "hash",
+ "id",
+ "index",
+ "insert",
+ "int",
+ "isalpha",
+ "isdigit",
+ "isinstance",
+ "islower",
+ "isspace",
+ "issubclass",
+ "isupper",
+ "items",
+ "iter",
+ "join",
+ "key",
+ "keys",
+ "len",
+ "list",
+ "little",
+ "locals",
+ "lower",
+ "lstrip",
+ "main",
+ "map",
+ "micropython",
+ "next",
+ "object",
+ "open",
+ "ord",
+ "pop",
+ "popitem",
+ "pow",
+ "print",
+ "range",
+ "read",
+ "readinto",
+ "readline",
+ "remove",
+ "replace",
+ "repr",
+ "reverse",
+ "rfind",
+ "rindex",
+ "round",
+ "rsplit",
+ "rstrip",
+ "self",
+ "send",
+ "sep",
+ "set",
+ "setattr",
+ "setdefault",
+ "sort",
+ "sorted",
+ "split",
+ "start",
+ "startswith",
+ "staticmethod",
+ "step",
+ "stop",
+ "str",
+ "strip",
+ "sum",
+ "super",
+ "throw",
+ "to_bytes",
+ "tuple",
+ "type",
+ "update",
+ "upper",
+ "utf-8",
+ "value",
+ "values",
+ "write",
+ "zip",
+]
+
# this must match the equivalent function in qstr.c
def compute_hash(qstr, bytes_hash):
hash = 5381
@@ -70,9 +240,22 @@ def qstr_escape(qst):
return re.sub(r'[^A-Za-z0-9_]', esc_char, qst)
def parse_input_headers(infiles):
- # read the qstrs in from the input files
qcfgs = {}
qstrs = {}
+
+ # add static qstrs
+ for qstr in static_qstr_list:
+ # work out the corresponding qstr name
+ ident = qstr_escape(qstr)
+
+ # don't add duplicates
+ assert ident not in qstrs
+
+ # add the qstr to the list, with order number to retain original order in file
+ order = len(qstrs) - 300000
+ qstrs[ident] = (order, ident, qstr)
+
+ # read the qstrs in from the input files
for infile in infiles:
with open(infile, 'rt') as f:
for line in f: