diff options
author | Damien George <damien@micropython.org> | 2022-05-07 15:51:41 +1000 |
---|---|---|
committer | Damien George <damien@micropython.org> | 2022-05-18 15:23:11 +1000 |
commit | 8588525868a59a2d4cd53e2fb24aa0d413659df4 (patch) | |
tree | aafdb78f4967c51aadae35dfd9590a8d7b79b5b3 /py/compile.c | |
parent | b3d0f5f67ccabb57774b4cf677be1a4cfb36a39a (diff) |
py/compile: De-duplicate constant objects in module's constant table.
The recent rework of bytecode made all constants global with respect to the
module (previously, each function had its own constant table). That means
the constant table for a module is shared among all functions/methods/etc
within the module.
This commit add support to the compiler to de-duplicate constants in this
module constant table. So if a constant is used more than once -- eg 1.0
or (None, None) -- then the same object is reused for all instances.
For example, if there is code like `print(1.0, 1.0)` then the parser will
create two independent constants 1.0 and 1.0. The compiler will then (with
this commit) notice they are the same and only put one of them in the
constant table. The bytecode will then reuse that constant twice in the
print expression. That allows the second 1.0 to be reclaimed by the GC,
also means the constant table has one less entry so saves a word.
Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'py/compile.c')
-rw-r--r-- | py/compile.c | 54 |
1 files changed, 19 insertions, 35 deletions
diff --git a/py/compile.c b/py/compile.c index 75cd562a1..7852b68ef 100644 --- a/py/compile.c +++ b/py/compile.c @@ -185,8 +185,6 @@ typedef struct _compiler_t { scope_t *scope_head; scope_t *scope_cur; - mp_emit_common_t emit_common; - emit_t *emit; // current emitter #if NEED_METHOD_TABLE const emit_method_table_t *emit_method_table; // current emit method table @@ -196,6 +194,8 @@ typedef struct _compiler_t { emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm const emit_inline_asm_method_table_t *emit_inline_asm_method_table; // current emit method table for inline asm #endif + + mp_emit_common_t emit_common; } compiler_t; /******************************************************************************/ @@ -210,15 +210,11 @@ STATIC void mp_emit_common_init(mp_emit_common_t *emit, qstr source_file) { mp_map_elem_t *elem = mp_map_lookup(&emit->qstr_map, MP_OBJ_NEW_QSTR(source_file), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND); elem->value = MP_OBJ_NEW_SMALL_INT(0); #endif + mp_obj_list_init(&emit->const_obj_list, 0); } STATIC void mp_emit_common_start_pass(mp_emit_common_t *emit, pass_kind_t pass) { emit->pass = pass; - if (pass == MP_PASS_STACK_SIZE) { - emit->ct_cur_obj_base = emit->ct_cur_obj; - } else if (pass > MP_PASS_STACK_SIZE) { - emit->ct_cur_obj = emit->ct_cur_obj_base; - } if (pass == MP_PASS_CODE_SIZE) { if (emit->ct_cur_child == 0) { emit->children = NULL; @@ -229,22 +225,10 @@ STATIC void mp_emit_common_start_pass(mp_emit_common_t *emit, pass_kind_t pass) emit->ct_cur_child = 0; } -STATIC void mp_emit_common_finalise(mp_emit_common_t *emit, bool has_native_code) { - emit->ct_cur_obj += has_native_code; // allocate an additional slot for &mp_fun_table - emit->const_table = m_new0(mp_uint_t, emit->ct_cur_obj); - emit->ct_cur_obj = has_native_code; // reserve slot 0 for &mp_fun_table - #if MICROPY_EMIT_NATIVE - if (has_native_code) { - // store mp_fun_table pointer at the start of the constant table - emit->const_table[0] = (mp_uint_t)(uintptr_t)&mp_fun_table; - } - #endif -} - STATIC void mp_emit_common_populate_module_context(mp_emit_common_t *emit, qstr source_file, mp_module_context_t *context) { #if MICROPY_EMIT_BYTECODE_USES_QSTR_TABLE size_t qstr_map_used = emit->qstr_map.used; - mp_module_context_alloc_tables(context, qstr_map_used, emit->ct_cur_obj); + mp_module_context_alloc_tables(context, qstr_map_used, emit->const_obj_list.len); for (size_t i = 0; i < emit->qstr_map.alloc; ++i) { if (mp_map_slot_is_filled(&emit->qstr_map, i)) { size_t idx = MP_OBJ_SMALL_INT_VALUE(emit->qstr_map.table[i].value); @@ -253,12 +237,12 @@ STATIC void mp_emit_common_populate_module_context(mp_emit_common_t *emit, qstr } } #else - mp_module_context_alloc_tables(context, 0, emit->ct_cur_obj); + mp_module_context_alloc_tables(context, 0, emit->const_obj_list.len); context->constants.source_file = source_file; #endif - if (emit->ct_cur_obj > 0) { - memcpy(context->constants.obj_table, emit->const_table, emit->ct_cur_obj * sizeof(mp_uint_t)); + for (size_t i = 0; i < emit->const_obj_list.len; ++i) { + context->constants.obj_table[i] = emit->const_obj_list.items[i]; } } @@ -3501,23 +3485,13 @@ mp_compiled_module_t mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr so } // compute some things related to scope and identifiers - bool has_native_code = false; for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) { - #if MICROPY_EMIT_NATIVE - if (s->emit_options == MP_EMIT_OPT_NATIVE_PYTHON || s->emit_options == MP_EMIT_OPT_VIPER) { - has_native_code = true; - } - #endif - scope_compute_things(s); } // set max number of labels now that it's calculated emit_bc_set_max_num_labels(emit_bc, max_num_labels); - // finalise and allocate the constant table - mp_emit_common_finalise(&comp->emit_common, has_native_code); - // compile MP_PASS_STACK_SIZE, MP_PASS_CODE_SIZE, MP_PASS_EMIT #if MICROPY_EMIT_NATIVE emit_t *emit_native = NULL; @@ -3604,9 +3578,19 @@ mp_compiled_module_t mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr so cm.rc = module_scope->raw_code; cm.context = context; #if MICROPY_PERSISTENT_CODE_SAVE - cm.has_native = has_native_code; + cm.has_native = false; + #if MICROPY_EMIT_NATIVE + if (emit_native != NULL) { + cm.has_native = true; + } + #endif + #if MICROPY_EMIT_INLINE_ASM + if (comp->emit_inline_asm != NULL) { + cm.has_native = true; + } + #endif cm.n_qstr = comp->emit_common.qstr_map.used; - cm.n_obj = comp->emit_common.ct_cur_obj; + cm.n_obj = comp->emit_common.const_obj_list.len; #endif if (comp->compile_error == MP_OBJ_NULL) { mp_emit_common_populate_module_context(&comp->emit_common, source_file, context); |