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/emit.h | |
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/emit.h')
-rw-r--r-- | py/emit.h | 11 |
1 files changed, 2 insertions, 9 deletions
@@ -94,14 +94,12 @@ typedef struct _emit_t emit_t; typedef struct _mp_emit_common_t { pass_kind_t pass; - uint16_t ct_cur_obj_base; - uint16_t ct_cur_obj; uint16_t ct_cur_child; - mp_uint_t *const_table; mp_raw_code_t **children; #if MICROPY_EMIT_BYTECODE_USES_QSTR_TABLE mp_map_t qstr_map; #endif + mp_obj_list_t const_obj_list; } mp_emit_common_t; typedef struct _mp_emit_method_table_id_ops_t { @@ -181,12 +179,7 @@ static inline qstr_short_t mp_emit_common_use_qstr(mp_emit_common_t *emit, qstr } #endif -static inline size_t mp_emit_common_alloc_const_obj(mp_emit_common_t *emit, mp_obj_t obj) { - if (emit->pass == MP_PASS_EMIT) { - emit->const_table[emit->ct_cur_obj] = (mp_uint_t)obj; - } - return emit->ct_cur_obj++; -} +size_t mp_emit_common_use_const_obj(mp_emit_common_t *emit, mp_obj_t const_obj); static inline size_t mp_emit_common_alloc_const_child(mp_emit_common_t *emit, mp_raw_code_t *rc) { if (emit->pass == MP_PASS_EMIT) { |