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/emitbc.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/emitbc.c')
-rw-r--r-- | py/emitbc.c | 3 |
1 files changed, 1 insertions, 2 deletions
diff --git a/py/emitbc.c b/py/emitbc.c index a9d5b3799..2007975c5 100644 --- a/py/emitbc.c +++ b/py/emitbc.c @@ -204,8 +204,7 @@ STATIC void emit_write_bytecode_byte_qstr(emit_t *emit, int stack_adj, byte b, q } STATIC void emit_write_bytecode_byte_obj(emit_t *emit, int stack_adj, byte b, mp_obj_t obj) { - emit_write_bytecode_byte_const(emit, stack_adj, b, - mp_emit_common_alloc_const_obj(emit->emit_common, obj)); + emit_write_bytecode_byte_const(emit, stack_adj, b, mp_emit_common_use_const_obj(emit->emit_common, obj)); } STATIC void emit_write_bytecode_byte_child(emit_t *emit, int stack_adj, byte b, mp_raw_code_t *rc) { |