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

@@ -34,7 +34,7 @@
STATIC uint mp_prof_bytecode_lineno(const mp_raw_code_t *rc, size_t bc) {
const mp_bytecode_prelude_t *prelude = &rc->prelude;
return mp_bytecode_get_source_line(prelude->line_info, bc + prelude->opcodes - prelude->locals);
return mp_bytecode_get_source_line(prelude->line_info, bc);
}
void mp_prof_extract_prelude(const byte *bytecode, mp_bytecode_prelude_t *prelude) {
@@ -48,22 +48,15 @@ void mp_prof_extract_prelude(const byte *bytecode, mp_bytecode_prelude_t *prelud
prelude->n_kwonly_args = n_kwonly_args;
prelude->n_def_pos_args = n_def_pos_args;
const byte *code_info = ip;
size_t code_info_size = mp_decode_uint(&ip);
MP_BC_PRELUDE_SIZE_DECODE(ip);
prelude->line_info = ip + 4;
prelude->opcodes = ip + n_info + n_cell;
qstr block_name = ip[0] | (ip[1] << 8);
qstr source_file = ip[2] | (ip[3] << 8);
ip += 4;
prelude->qstr_block_name = block_name;
prelude->qstr_source_file = source_file;
prelude->line_info = ip;
prelude->locals = code_info + code_info_size;
ip = prelude->locals;
while (*ip++ != 255) {
}
prelude->opcodes = ip;
}
/******************************************************************************/