summaryrefslogtreecommitdiff
path: root/py/qstr.c
diff options
context:
space:
mode:
authorJim Mussared <jim.mussared@gmail.com>2019-09-26 22:19:29 +1000
committerDamien George <damien.p.george@gmail.com>2020-04-05 14:20:57 +1000
commit154b4eb354d97c7c28253bdc5212b2e58ea6462e (patch)
treec1cc3d61fa0b9dff50766d2057e1627eb818a25e /py/qstr.c
parent19212242725f3d3392e0dfaa831eef0b8c64f9ce (diff)
py: Implement "common word" compression scheme for error messages.
The idea here is that there's a moderate amount of ROM used up by exception text. Obviously we try to keep the messages short, and the code can enable terse errors, but it still adds up. Listed below is the total string data size for various ports: bare-arm 2860 minimal 2876 stm32 8926 (PYBV11) cc3200 3751 esp32 5721 This commit implements compression of these strings. It takes advantage of the fact that these strings are all 7-bit ascii and extracts the top 128 frequently used words from the messages and stores them packed (dropping their null-terminator), then uses (0x80 | index) inside strings to refer to these common words. Spaces are automatically added around words, saving more bytes. This happens transparently in the build process, mirroring the steps that are used to generate the QSTR data. The MP_COMPRESSED_ROM_TEXT macro wraps any literal string that should compressed, and it's automatically decompressed in mp_decompress_rom_string. There are many schemes that could be used for the compression, and some are included in py/makecompresseddata.py for reference (space, Huffman, ngram, common word). Results showed that the common-word compression gets better results. This is before counting the increased cost of the Huffman decoder. This might be slightly counter-intuitive, but this data is extremely repetitive at a word-level, and the byte-level entropy coder can't quite exploit that as efficiently. Ideally one would combine both approaches, but for now the common-word approach is the one that is used. For additional comparison, the size of the raw data compressed with gzip and zlib is calculated, as a sort of proxy for a lower entropy bound. With this scheme we come within 15% on stm32, and 30% on bare-arm (i.e. we use x% more bytes than the data compressed with gzip -- not counting the code overhead of a decoder, and how this would be hypothetically implemented). The feature is disabled by default and can be enabled by setting MICROPY_ROM_TEXT_COMPRESSION at the Makefile-level.
Diffstat (limited to 'py/qstr.c')
-rw-r--r--py/qstr.c75
1 files changed, 75 insertions, 0 deletions
diff --git a/py/qstr.c b/py/qstr.c
index c3d78bfda..2b1cea405 100644
--- a/py/qstr.c
+++ b/py/qstr.c
@@ -310,3 +310,78 @@ void qstr_dump_data(void) {
QSTR_EXIT();
}
#endif
+
+#if MICROPY_ROM_TEXT_COMPRESSION
+
+#ifdef NO_QSTR
+
+// If NO_QSTR is set, it means we're doing QSTR extraction.
+// So we won't yet have "genhdr/compressed.data.h"
+
+#else
+
+// Emit the compressed_string_data string.
+#define MP_COMPRESSED_DATA(x) STATIC const char *compressed_string_data = x;
+#define MP_MATCH_COMPRESSED(a, b)
+#include "genhdr/compressed.data.h"
+#undef MP_COMPRESSED_DATA
+#undef MP_MATCH_COMPRESSED
+
+#endif // NO_QSTR
+
+// This implements the "common word" compression scheme (see makecompresseddata.py) where the most
+// common 128 words in error messages are replaced by their index into the list of common words.
+
+// The compressed string data is delimited by setting high bit in the final char of each word.
+// e.g. aaaa<0x80|a>bbbbbb<0x80|b>....
+// This method finds the n'th string.
+STATIC const byte *find_uncompressed_string(uint8_t n) {
+ const byte *c = (byte *)compressed_string_data;
+ while (n > 0) {
+ while ((*c & 0x80) == 0) {
+ ++c;
+ }
+ ++c;
+ --n;
+ }
+ return c;
+}
+
+// Given a compressed string in src, decompresses it into dst.
+// dst must be large enough (use MP_MAX_UNCOMPRESSED_TEXT_LEN+1).
+void mp_decompress_rom_string(byte *dst, const mp_rom_error_text_t src_chr) {
+ // Skip past the 0xff marker.
+ const byte *src = (byte *)src_chr + 1;
+ // Need to add spaces around compressed words, except for the first (i.e. transition from 1<->2).
+ // 0 = start, 1 = compressed, 2 = regular.
+ int state = 0;
+ while (*src) {
+ if ((byte) * src >= 128) {
+ if (state != 0) {
+ *dst++ = ' ';
+ }
+ state = 1;
+
+ // High bit set, replace with common word.
+ const byte *word = find_uncompressed_string(*src & 0x7f);
+ // The word is terminated by the final char having its high bit set.
+ while ((*word & 0x80) == 0) {
+ *dst++ = *word++;
+ }
+ *dst++ = (*word & 0x7f);
+ } else {
+ // Otherwise just copy one char.
+ if (state == 1) {
+ *dst++ = ' ';
+ }
+ state = 2;
+
+ *dst++ = *src;
+ }
+ ++src;
+ }
+ // Add null-terminator.
+ *dst = 0;
+}
+
+#endif // MICROPY_ROM_TEXT_COMPRESSION