From 4747becc6423e8161519ad6850e09137f14742a8 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 31 Jul 2014 16:12:01 +0000 Subject: 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. --- py/showbc.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'py/showbc.c') diff --git a/py/showbc.c b/py/showbc.c index 12400fa7c..6c10333c9 100644 --- a/py/showbc.c +++ b/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); } } -- cgit v1.2.3