mirror of
https://github.com/micropython/micropython.git
synced 2026-01-06 20:20:14 +01:00
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>
This commit is contained in:
@@ -27,6 +27,7 @@
|
||||
#include <assert.h>
|
||||
|
||||
#include "py/emit.h"
|
||||
#include "py/nativeglue.h"
|
||||
|
||||
#if MICROPY_ENABLE_COMPILER
|
||||
|
||||
@@ -40,6 +41,51 @@ qstr_short_t mp_emit_common_use_qstr(mp_emit_common_t *emit, qstr qst) {
|
||||
}
|
||||
#endif
|
||||
|
||||
// Compare two objects for strict equality, including equality of type. This is
|
||||
// different to the semantics of mp_obj_equal which, eg, has (True,) == (1.0,).
|
||||
static bool strictly_equal(mp_obj_t a, mp_obj_t b) {
|
||||
if (a == b) {
|
||||
return true;
|
||||
}
|
||||
|
||||
#if MICROPY_EMIT_NATIVE
|
||||
if (a == MP_OBJ_FROM_PTR(&mp_fun_table) || b == MP_OBJ_FROM_PTR(&mp_fun_table)) {
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
const mp_obj_type_t *a_type = mp_obj_get_type(a);
|
||||
const mp_obj_type_t *b_type = mp_obj_get_type(b);
|
||||
if (a_type != b_type) {
|
||||
return false;
|
||||
}
|
||||
if (a_type == &mp_type_tuple) {
|
||||
mp_obj_tuple_t *a_tuple = MP_OBJ_TO_PTR(a);
|
||||
mp_obj_tuple_t *b_tuple = MP_OBJ_TO_PTR(b);
|
||||
if (a_tuple->len != b_tuple->len) {
|
||||
return false;
|
||||
}
|
||||
for (size_t i = 0; i < a_tuple->len; ++i) {
|
||||
if (!strictly_equal(a_tuple->items[i], b_tuple->items[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
return mp_obj_equal(a, b);
|
||||
}
|
||||
}
|
||||
|
||||
size_t mp_emit_common_use_const_obj(mp_emit_common_t *emit, mp_obj_t const_obj) {
|
||||
for (size_t i = 0; i < emit->const_obj_list.len; ++i) {
|
||||
if (strictly_equal(emit->const_obj_list.items[i], const_obj)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
mp_obj_list_append(MP_OBJ_FROM_PTR(&emit->const_obj_list), const_obj);
|
||||
return emit->const_obj_list.len - 1;
|
||||
}
|
||||
|
||||
void mp_emit_common_get_id_for_modification(scope_t *scope, qstr qst) {
|
||||
// name adding/lookup
|
||||
id_info_t *id = scope_find_or_add_id(scope, qst, ID_INFO_KIND_GLOBAL_IMPLICIT);
|
||||
|
||||
Reference in New Issue
Block a user