diff options
| author | Jeff Epler <jepler@gmail.com> | 2020-09-12 10:10:18 -0500 |
|---|---|---|
| committer | Jeff Epler <jepler@gmail.com> | 2020-09-12 10:10:45 -0500 |
| commit | 40ab5c6b21ff93ad11ca51dc66d3613dcd77e5ef (patch) | |
| tree | 001b7d6ea0489ac12d81efcdf9dc72c1d073202e /supervisor | |
| parent | 7611e71a1bc49bcb426e0854519d5143eb262a17 (diff) | |
compression: Implement ciscorn's dictionary approach
Massive savings. Thanks so much @ciscorn for providing the initial
code for choosing the dictionary.
This adds a bit of time to the build, both to find the dictionary
but also because (for reasons I don't fully understand), the binary
search in the compress() function no longer worked and had to be
replaced with a linear search.
I think this is because the intended invariant is that for codebook
entries that encode to the same number of bits, the entries are ordered
in ascending value. However, I mis-placed the transition from "words"
to "byte/char values" so the codebook entries for words are in word-order
rather than their code order.
Because this price is only paid at build time, I didn't care to determine
exactly where the correct fix was.
I also commented out a line to produce the "estimated total memory size"
-- at least on the unix build with TRANSLATION=ja, this led to a build
time KeyError trying to compute the codebook size for all the strings.
I think this occurs because some single unicode code point ('ァ') is
no longer present as itself in the compressed strings, due to always
being replaced by a word.
As promised, this seems to save hundreds of bytes in the German translation
on the trinket m0.
Testing performed:
- built trinket_m0 in several languages
- built and ran unix port in several languages (en, de_DE, ja) and ran
simple error-producing codes like ./micropython -c '1/0'
Diffstat (limited to 'supervisor')
| -rw-r--r-- | supervisor/shared/translate.c | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/supervisor/shared/translate.c b/supervisor/shared/translate.c index 5cd7b8dd8..cc0de7f61 100644 --- a/supervisor/shared/translate.c +++ b/supervisor/shared/translate.c @@ -47,13 +47,22 @@ STATIC int put_utf8(char *buf, int u) { if(u <= 0x7f) { *buf = u; return 1; - } else if(bigram_start <= u && u <= bigram_end) { - int n = (u - 0x80) * 2; - // (note that at present, entries in the bigrams table are - // guaranteed not to represent bigrams themselves, so this adds + } else if(word_start <= u && u <= word_end) { + int n = (u - 0x80); + size_t off = 0; + for(int i=0; i<n; i++) { + off += wlen[i]; + } + int ret = 0; + // note that at present, entries in the words table are + // guaranteed not to represent words themselves, so this adds // at most 1 level of recursive call - int ret = put_utf8(buf, bigrams[n]); - return ret + put_utf8(buf + ret, bigrams[n+1]); + for(int i=0; i<wlen[n]; i++) { + int len = put_utf8(buf, words[off+i]); + buf += len; + ret += len; + } + return ret; } else if(u <= 0x07ff) { *buf++ = 0b11000000 | (u >> 6); *buf = 0b10000000 | (u & 0b00111111); |
