aboutsummaryrefslogtreecommitdiff
path: root/py/emitcommon.c
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2022-05-07 15:51:41 +1000
committerDamien George <damien@micropython.org>2022-05-18 15:23:11 +1000
commit8588525868a59a2d4cd53e2fb24aa0d413659df4 (patch)
treeaafdb78f4967c51aadae35dfd9590a8d7b79b5b3 /py/emitcommon.c
parentb3d0f5f67ccabb57774b4cf677be1a4cfb36a39a (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/emitcommon.c')
-rw-r--r--py/emitcommon.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/py/emitcommon.c b/py/emitcommon.c
index 02f96f111..679ef1d97 100644
--- a/py/emitcommon.c
+++ b/py/emitcommon.c
@@ -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);