py/persistentcode: Make .mpy more compact with qstr directly in prelude.

Instead of encoding 4 zero bytes as placeholders for the simple_name and
source_file qstrs, and storing the qstrs after the bytecode, store the
qstrs at the location of these 4 bytes.  This saves 4 bytes per bytecode
function stored in a .mpy file (for example lcd160cr.mpy drops by 232
bytes, 4x 58 functions).  And resulting code size is slightly reduced on
ports that use this feature.
pull/5201/head
Damien George 2019-10-10 15:30:16 +11:00
rodzic 858e992d2e
commit 23f0691fdd
3 zmienionych plików z 57 dodań i 49 usunięć

Wyświetl plik

@ -160,8 +160,8 @@ typedef struct _bytecode_prelude_t {
} bytecode_prelude_t;
// ip will point to start of opcodes
// ip2 will point to simple_name, source_file qstrs
STATIC void extract_prelude(const byte **ip, const byte **ip2, bytecode_prelude_t *prelude) {
// return value will point to simple_name, source_file qstrs
STATIC byte *extract_prelude(const byte **ip, bytecode_prelude_t *prelude) {
MP_BC_PRELUDE_SIG_DECODE(*ip);
prelude->n_state = n_state;
prelude->n_exc_stack = n_exc_stack;
@ -170,9 +170,10 @@ STATIC void extract_prelude(const byte **ip, const byte **ip2, bytecode_prelude_
prelude->n_kwonly_args = n_kwonly_args;
prelude->n_def_pos_args = n_def_pos_args;
MP_BC_PRELUDE_SIZE_DECODE(*ip);
*ip2 = *ip;
byte *ip_info = (byte*)*ip;
*ip += n_info;
*ip += n_cell;
return ip_info;
}
#endif // MICROPY_PERSISTENT_CODE_LOAD || MICROPY_PERSISTENT_CODE_SAVE
@ -282,17 +283,28 @@ STATIC mp_obj_t load_obj(mp_reader_t *reader) {
}
}
STATIC void load_prelude(mp_reader_t *reader, byte **ip, byte **ip2, bytecode_prelude_t *prelude) {
// Read in the prelude
STATIC void load_prelude_qstrs(mp_reader_t *reader, qstr_window_t *qw, byte *ip) {
qstr simple_name = load_qstr(reader, qw);
ip[0] = simple_name; ip[1] = simple_name >> 8;
qstr source_file = load_qstr(reader, qw);
ip[2] = source_file; ip[3] = source_file >> 8;
}
STATIC void load_prelude(mp_reader_t *reader, qstr_window_t *qw, byte **ip, bytecode_prelude_t *prelude) {
// Read in the prelude header
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;
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);
// Prelude header has been read into *ip, now decode and extract values from it
extract_prelude((const byte**)ip, prelude);
// Load qstrs in prelude
load_prelude_qstrs(reader, qw, ip_read);
ip_read += 4;
// Read remaining code info
read_bytes(reader, ip_read, *ip - ip_read);
}
STATIC void load_bytecode(mp_reader_t *reader, qstr_window_t *qw, byte *ip, byte *ip_top) {
@ -329,7 +341,6 @@ STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader, qstr_window_t *qw) {
#endif
uint8_t *fun_data = NULL;
byte *ip2;
bytecode_prelude_t prelude = {0};
#if MICROPY_EMIT_MACHINE_CODE
size_t prelude_offset = 0;
@ -343,7 +354,7 @@ STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader, qstr_window_t *qw) {
// Load prelude
byte *ip = fun_data;
load_prelude(reader, &ip, &ip2, &prelude);
load_prelude(reader, qw, &ip, &prelude);
// Load bytecode
load_bytecode(reader, qw, ip, fun_data + fun_data_len);
@ -377,7 +388,9 @@ STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader, qstr_window_t *qw) {
// Extract prelude for later use
prelude_offset = read_uint(reader, NULL);
const byte *ip = fun_data + prelude_offset;
extract_prelude(&ip, (const byte**)&ip2, &prelude);
byte *ip_info = extract_prelude(&ip, &prelude);
// Load qstrs in prelude
load_prelude_qstrs(reader, qw, ip_info);
} else {
// Load basic scope info for viper and asm
prelude.scope_flags = read_uint(reader, NULL);
@ -391,14 +404,6 @@ STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader, qstr_window_t *qw) {
#endif
}
if (kind == MP_CODE_BYTECODE || kind == MP_CODE_NATIVE_PY) {
// Load qstrs in prelude
qstr simple_name = load_qstr(reader, qw);
qstr source_file = load_qstr(reader, qw);
ip2[0] = simple_name; ip2[1] = simple_name >> 8;
ip2[2] = source_file; ip2[3] = source_file >> 8;
}
size_t n_obj = 0;
size_t n_raw_code = 0;
mp_uint_t *const_table = NULL;
@ -591,6 +596,11 @@ STATIC void save_obj(mp_print_t *print, mp_obj_t o) {
}
}
STATIC void save_prelude_qstrs(mp_print_t *print, qstr_window_t *qw, const byte *ip) {
save_qstr(print, qw, ip[0] | (ip[1] << 8)); // simple_name
save_qstr(print, qw, ip[2] | (ip[3] << 8)); // source_file
}
STATIC void save_bytecode(mp_print_t *print, qstr_window_t *qw, const byte *ip, const byte *ip_top) {
while (ip < ip_top) {
size_t sz;
@ -611,18 +621,21 @@ STATIC void save_raw_code(mp_print_t *print, mp_raw_code_t *rc, qstr_window_t *q
// Save function kind and data length
mp_print_uint(print, (rc->fun_data_len << 2) | (rc->kind - MP_CODE_BYTECODE));
const byte *ip2;
bytecode_prelude_t prelude;
if (rc->kind == MP_CODE_BYTECODE) {
// Save prelude
// Extract prelude
const byte *ip = rc->fun_data;
extract_prelude(&ip, &ip2, &prelude);
size_t prelude_len = ip - (const byte*)rc->fun_data;
const byte *ip_top = (const byte*)rc->fun_data + rc->fun_data_len;
mp_print_bytes(print, rc->fun_data, prelude_len);
const byte *ip_info = extract_prelude(&ip, &prelude);
// Save prelude
mp_print_bytes(print, rc->fun_data, ip_info - (const byte*)rc->fun_data);
save_prelude_qstrs(print, qstr_window, ip_info);
ip_info += 4;
mp_print_bytes(print, ip_info, ip - ip_info);
// Save bytecode
const byte *ip_top = (const byte*)rc->fun_data + rc->fun_data_len;
save_bytecode(print, qstr_window, ip, ip_top);
#if MICROPY_EMIT_MACHINE_CODE
} else {
@ -639,10 +652,13 @@ STATIC void save_raw_code(mp_print_t *print, mp_raw_code_t *rc, qstr_window_t *q
}
if (rc->kind == MP_CODE_NATIVE_PY) {
// Save prelude size, and extract prelude for later use
// Save prelude size
mp_print_uint(print, rc->prelude_offset);
// Extract prelude and save qstrs in prelude
const byte *ip = (const byte*)rc->fun_data + rc->prelude_offset;
extract_prelude(&ip, &ip2, &prelude);
const byte *ip_info = extract_prelude(&ip, &prelude);
save_prelude_qstrs(print, qstr_window, ip_info);
} else {
// Save basic scope info for viper and asm
mp_print_uint(print, rc->scope_flags);
@ -656,12 +672,6 @@ STATIC void save_raw_code(mp_print_t *print, mp_raw_code_t *rc, qstr_window_t *q
#endif
}
if (rc->kind == MP_CODE_BYTECODE || rc->kind == MP_CODE_NATIVE_PY) {
// Save qstrs in prelude
save_qstr(print, qstr_window, ip2[0] | (ip2[1] << 8)); // simple_name
save_qstr(print, qstr_window, ip2[2] | (ip2[3] << 8)); // source_file
}
if (rc->kind != MP_CODE_NATIVE_ASM) {
// Save constant table for bytecode, native and viper
@ -699,9 +709,8 @@ STATIC bool mp_raw_code_has_native(mp_raw_code_t *rc) {
}
const byte *ip = rc->fun_data;
const byte *ip2;
bytecode_prelude_t prelude;
extract_prelude(&ip, &ip2, &prelude);
extract_prelude(&ip, &prelude);
const mp_uint_t *const_table = rc->const_table
+ prelude.n_pos_args + prelude.n_kwonly_args

Wyświetl plik

@ -57,11 +57,11 @@ user_files = {
b'M\x05\x0b\x1f\x20' # header
b'\x20' # n bytes, bytecode
b'\x00\x08\x00\x00\x00\x00' # prelude
b'\x00\x08\x02m\x02m' # prelude
b'\x51' # LOAD_CONST_NONE
b'\x63' # RETURN_VALUE
b'\x02m\x02m\x00\x02' # simple_name, source_file, n_obj, n_raw_code
b'\x00\x02' # n_obj, n_raw_code
b'\x22' # n bytes, viper code
b'\x00\x00\x00\x00\x00\x00' # dummy machine code

Wyświetl plik

@ -583,13 +583,14 @@ def read_obj(f):
else:
assert 0
def read_prelude(f, bytecode):
def read_prelude(f, bytecode, qstr_win):
n_state, n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_pos_args = read_prelude_sig(lambda: read_byte(f, bytecode))
n_info, n_cell = read_prelude_size(lambda: read_byte(f, bytecode))
l2 = bytecode.idx
for _ in range(n_info + n_cell):
read_qstr_and_pack(f, bytecode, qstr_win) # simple_name
read_qstr_and_pack(f, bytecode, qstr_win) # source_file
for _ in range(n_info - 4 + n_cell):
read_byte(f, bytecode)
return l2, (n_state, n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_pos_args)
return n_state, n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_pos_args
def read_qstr_and_pack(f, bytecode, qstr_win):
qst = read_qstr(f, qstr_win)
@ -617,7 +618,7 @@ def read_raw_code(f, qstr_win):
fun_data = BytecodeBuffer(fun_data_len)
if kind == MP_CODE_BYTECODE:
name_idx, prelude = read_prelude(f, fun_data)
prelude = read_prelude(f, fun_data, qstr_win)
read_bytecode(f, fun_data, qstr_win)
else:
fun_data.buf[:] = f.read(fun_data_len)
@ -635,6 +636,9 @@ def read_raw_code(f, qstr_win):
if kind == MP_CODE_NATIVE_PY:
prelude_offset = read_uint(f)
_, name_idx, prelude = extract_prelude(fun_data.buf, prelude_offset)
fun_data.idx = name_idx # rewind to where qstrs are in prelude
read_qstr_and_pack(f, fun_data, qstr_win) # simple_name
read_qstr_and_pack(f, fun_data, qstr_win) # source_file
else:
prelude_offset = None
scope_flags = read_uint(f)
@ -644,11 +648,6 @@ def read_raw_code(f, qstr_win):
type_sig = read_uint(f)
prelude = (None, None, scope_flags, n_pos_args, 0)
if kind in (MP_CODE_BYTECODE, MP_CODE_NATIVE_PY):
fun_data.idx = name_idx # rewind to where qstrs are in prelude
read_qstr_and_pack(f, fun_data, qstr_win) # simple_name
read_qstr_and_pack(f, fun_data, qstr_win) # source_file
qstrs = []
objs = []
raw_codes = []