mirror of
https://github.com/micropython/micropython.git
synced 2026-01-04 19:20:22 +01:00
py: Improve encoding scheme for line-number to bytecode map.
Reduces by about a factor of 10 on average the amount of RAM needed to store the line-number to bytecode map in the bytecode prelude. Using CPython3.4's stdlib for statistics: previously, an average of 13 bytes were used per (bytecode offset, line-number offset) pair, and now with this improvement, that's down to 1.3 bytes on average. Large RAM usage before was due to some very large steps in line numbers, both from the start of the first line in a function way down in the file, and also functions that have big comments and/or big strings in them (both cases were significant). Although the savings are large on average for the CPython stdlib, it won't have such a big effect for small scripts used in embedded programming. Addresses issue #648.
This commit is contained in:
15
py/showbc.c
15
py/showbc.c
@@ -95,9 +95,18 @@ void mp_bytecode_print(const void *descr, const byte *ip, int len) {
|
||||
mp_int_t bc = (code_info + code_info_size) - ip;
|
||||
mp_uint_t source_line = 1;
|
||||
printf(" bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line);
|
||||
for (const byte* ci = code_info + 12; *ci; ci++) {
|
||||
bc += *ci & 31;
|
||||
source_line += *ci >> 5;
|
||||
for (const byte* ci = code_info + 12; *ci;) {
|
||||
if ((ci[0] & 0x80) == 0) {
|
||||
// 0b0LLBBBBB encoding
|
||||
bc += ci[0] & 0x1f;
|
||||
source_line += ci[0] >> 5;
|
||||
ci += 1;
|
||||
} else {
|
||||
// 0b1LLLBBBB 0bLLLLLLLL encoding (l's LSB in second byte)
|
||||
bc += ci[0] & 0xf;
|
||||
source_line += ((ci[0] << 4) & 0x700) | ci[1];
|
||||
ci += 2;
|
||||
}
|
||||
printf(" bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user