diff options
author | Damien George <damien.p.george@gmail.com> | 2019-03-01 14:33:03 +1100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2019-03-05 16:32:05 +1100 |
commit | 4f0931b21f72be86aae22f05b718aa36792afc9b (patch) | |
tree | 6ed029f6bc5ef4ee9b125f50e7795220232d5f14 /py/persistentcode.c | |
parent | 992a6e1deab06aba07ab09687402d39a0c028a73 (diff) | |
download | micropython-4f0931b21f72be86aae22f05b718aa36792afc9b.tar.gz micropython-4f0931b21f72be86aae22f05b718aa36792afc9b.zip |
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/persistentcode.c')
-rw-r--r-- | py/persistentcode.c | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/py/persistentcode.c b/py/persistentcode.c index d47425db92..c0a3281113 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 |