From 4f0931b21f72be86aae22f05b718aa36792afc9b Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 1 Mar 2019 14:33:03 +1100 Subject: 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%. --- py/persistentcode.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'py/persistentcode.c') diff --git a/py/persistentcode.c b/py/persistentcode.c index d47425db9..c0a328111 100644 --- a/py/persistentcode.c +++ b/py/persistentcode.c @@ -38,6 +38,8 @@ #include "py/smallint.h" +#define QSTR_LAST_STATIC MP_QSTR_zip + // The current version of .mpy files #define MPY_VERSION (3) @@ -187,6 +189,10 @@ STATIC size_t read_uint(mp_reader_t *reader, byte **out) { STATIC qstr load_qstr(mp_reader_t *reader, qstr_window_t *qw) { size_t len = read_uint(reader, NULL); + if (len == 0) { + // static qstr + return read_byte(reader); + } if (len & 1) { // qstr in window return qstr_window_access(qw, len >> 1); @@ -362,6 +368,12 @@ STATIC void mp_print_uint(mp_print_t *print, size_t n) { } STATIC void save_qstr(mp_print_t *print, qstr_window_t *qw, qstr qst) { + if (qst <= QSTR_LAST_STATIC) { + // encode static qstr + byte buf[2] = {0, qst & 0xff}; + mp_print_bytes(print, buf, 2); + return; + } size_t idx = qstr_window_insert(qw, qst); if (idx < QSTR_WINDOW_SIZE) { // qstr found in window, encode index to it -- cgit v1.2.3