Skip to content

Commit

Permalink
tools/mpy-tool.py: Skip generating frozen mp_raw_code_t when possible.
Browse files Browse the repository at this point in the history
This reduces frozen code size by using the bytecode directly as the
`mp_proto_fun_t`.

Signed-off-by: Damien George <[email protected]>
  • Loading branch information
dpgeorge committed Feb 16, 2024
1 parent e2ff00e commit a3a73b6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions py/emitglue.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,18 @@ mp_obj_t mp_make_function_from_proto_fun(mp_proto_fun_t proto_fun, const mp_modu
// def_kw_args must be MP_OBJ_NULL or a dict
assert(def_args == NULL || def_args[1] == MP_OBJ_NULL || mp_obj_is_type(def_args[1], &mp_type_dict));

#if MICROPY_MODULE_FROZEN_MPY
if (mp_proto_fun_is_bytecode(proto_fun)) {
const uint8_t *bc = proto_fun;
mp_obj_t fun = mp_obj_new_fun_bc(def_args, bc, context, NULL);
MP_BC_PRELUDE_SIG_DECODE(bc);
if (scope_flags & MP_SCOPE_FLAG_GENERATOR) {
((mp_obj_base_t *)MP_OBJ_TO_PTR(fun))->type = &mp_type_gen_wrap;
}
return fun;
}
#endif

// the proto-function is a mp_raw_code_t
const mp_raw_code_t *rc = proto_fun;

Expand Down
12 changes: 12 additions & 0 deletions tools/mpy-tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,13 @@ def freeze_raw_code(self, prelude_ptr=None, type_sig=0):
raw_code_type = "mp_raw_code_t"
else:
raw_code_type = "mp_raw_code_truncated_t"

empty_children = len(self.children) == 0 and prelude_ptr is None
generate_minimal = self.code_kind == MP_CODE_BYTECODE and empty_children

if generate_minimal:
print("#if MICROPY_PERSISTENT_CODE_SAVE")

print("static const %s proto_fun_%s = {" % (raw_code_type, self.escaped_name))
print(" .proto_fun_indicator[0] = MP_PROTO_FUN_INDICATOR_RAW_CODE_0,")
print(" .proto_fun_indicator[1] = MP_PROTO_FUN_INDICATOR_RAW_CODE_1,")
Expand Down Expand Up @@ -961,6 +968,11 @@ def freeze_raw_code(self, prelude_ptr=None, type_sig=0):
print(" .asm_type_sig = %u," % type_sig)
print("};")

if generate_minimal:
print("#else")
print("#define proto_fun_%s fun_data_%s[0]" % (self.escaped_name, self.escaped_name))
print("#endif")

global raw_code_count, raw_code_content
raw_code_count += 1
raw_code_content += 4 * 4
Expand Down

0 comments on commit a3a73b6

Please sign in to comment.