py: Rework and compress second part of bytecode prelude.

This patch compresses the second part of the bytecode prelude which
contains the source file name, function name, source-line-number mapping
and cell closure information.  This part of the prelude now begins with a
single varible length unsigned integer which encodes 2 numbers, being the
byte-size of the following 2 sections in the header: the "source info
section" and the "closure section".  After decoding this variable unsigned
integer it's possible to skip over one or both of these sections very
easily.

This scheme saves about 2 bytes for most functions compared to the original
format: one in the case that there are no closure cells, and one because
padding was eliminated.
This commit is contained in:
Damien George
2019-09-25 15:45:47 +10:00
parent b5ebfadbd6
commit c8c0fd4ca3
14 changed files with 162 additions and 119 deletions

View File

@@ -167,11 +167,10 @@ STATIC void extract_prelude(const byte **ip, const byte **ip2, bytecode_prelude_
prelude->n_pos_args = n_pos_args;
prelude->n_kwonly_args = n_kwonly_args;
prelude->n_def_pos_args = n_def_pos_args;
MP_BC_PRELUDE_SIZE_DECODE(*ip);
*ip2 = *ip;
prelude->code_info_size = mp_decode_uint(ip2);
*ip += prelude->code_info_size;
while (*(*ip)++ != 255) {
}
*ip += n_info;
*ip += n_cell;
}
#endif // MICROPY_PERSISTENT_CODE_LOAD || MICROPY_PERSISTENT_CODE_SAVE
@@ -286,12 +285,9 @@ STATIC void load_prelude(mp_reader_t *reader, byte **ip, byte **ip2, bytecode_pr
byte *ip_read = *ip;
read_uint(reader, &ip_read); // read in n_state/etc (is effectively a var-uint)
byte *ip_read_save = ip_read;
size_t code_info_size = read_uint(reader, &ip_read); // read in code_info_size
code_info_size -= ip_read - ip_read_save; // subtract bytes taken by code_info_size itself
read_bytes(reader, ip_read, code_info_size); // read remaining code info
ip_read += code_info_size;
while ((*ip_read++ = read_byte(reader)) != 255) {
}
read_uint(reader, &ip_read); // read in n_info/n_cell (is effectively a var-uint)
MP_BC_PRELUDE_SIZE_DECODE(ip_read_save);
read_bytes(reader, ip_read, n_info + n_cell); // read remaining code info
// Entire prelude has been read into *ip, now decode and extract values from it
extract_prelude((const byte**)ip, (const byte**)ip2, prelude);