Skip to content

Commit 165388e

Browse files
jimmodpgeorge
authored andcommitted
py/objtype: Optimise slot RAM usage for instance types.
In all cases other than where you have a native base with a protocol, it now fits into 4 GC blocks (like it did before the slots representation). Signed-off-by: Jim Mussared <[email protected]>
1 parent cb0ffdd commit 165388e

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

py/misc.h

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ typedef unsigned int uint;
7272
#define m_new_obj(type) (m_new(type, 1))
7373
#define m_new_obj_maybe(type) (m_new_maybe(type, 1))
7474
#define m_new_obj_var(obj_type, var_type, var_num) ((obj_type *)m_malloc(sizeof(obj_type) + sizeof(var_type) * (var_num)))
75+
#define m_new_obj_var0(obj_type, var_type, var_num) ((obj_type *)m_malloc0(sizeof(obj_type) + sizeof(var_type) * (var_num)))
7576
#define m_new_obj_var_maybe(obj_type, var_type, var_num) ((obj_type *)m_malloc_maybe(sizeof(obj_type) + sizeof(var_type) * (var_num)))
7677
#if MICROPY_ENABLE_FINALISER
7778
#define m_new_obj_with_finaliser(type) ((type *)(m_malloc_with_finaliser(sizeof(type))))

py/objnamedtuple.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,7 @@ STATIC mp_obj_t namedtuple_make_new(const mp_obj_type_t *type_in, size_t n_args,
143143
}
144144

145145
mp_obj_namedtuple_type_t *mp_obj_new_namedtuple_base(size_t n_fields, mp_obj_t *fields) {
146-
mp_obj_namedtuple_type_t *o = m_new_obj_var(mp_obj_namedtuple_type_t, qstr, n_fields);
147-
memset(&o->base, 0, sizeof(o->base));
146+
mp_obj_namedtuple_type_t *o = m_new_obj_var0(mp_obj_namedtuple_type_t, qstr, n_fields);
148147
o->n_fields = n_fields;
149148
for (size_t i = 0; i < n_fields; i++) {
150149
o->fields[i] = mp_obj_str_get_qstr(fields[i]);

py/objtype.c

+20-13
Original file line numberDiff line numberDiff line change
@@ -1148,10 +1148,15 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict)
11481148
#endif
11491149
}
11501150

1151-
// Allocate a full-sized mp_obj_full_type_t instance (i.e. all slots / extended fields).
1152-
// Given that Python types use almost all the slots anyway, this doesn't cost anything
1153-
// extra.
1154-
mp_obj_type_t *o = (mp_obj_type_t *)m_new0(mp_obj_full_type_t, 1);
1151+
const void *base_protocol = NULL;
1152+
if (bases_len > 0) {
1153+
base_protocol = MP_OBJ_TYPE_GET_SLOT_OR_NULL(((mp_obj_type_t *)MP_OBJ_TO_PTR(bases_items[0])), protocol);
1154+
}
1155+
1156+
// Allocate a variable-sized mp_obj_type_t with as many slots as we need
1157+
// (currently 9, plus 1 for base, plus 1 for base-protocol).
1158+
// Note: 11 slots pushes it from 4 to 5 GC blocks.
1159+
mp_obj_type_t *o = m_new_obj_var0(mp_obj_type_t, void *, 9 + (bases_len ? 1 : 0) + (base_protocol ? 1 : 0));
11551160
o->base.type = &mp_type_type;
11561161
o->flags = base_flags;
11571162
o->name = name;
@@ -1166,13 +1171,10 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict)
11661171
// MP_OBJ_TYPE_SET_SLOT(o, iternext, not implemented)
11671172
MP_OBJ_TYPE_SET_SLOT(o, buffer, instance_get_buffer, 7);
11681173

1169-
if (bases_len > 0) {
1170-
// Inherit protocol from a base class. This allows to define an
1171-
// abstract base class which would translate C-level protocol to
1172-
// Python method calls, and any subclass inheriting from it will
1173-
// support this feature.
1174-
MP_OBJ_TYPE_SET_SLOT(o, protocol, MP_OBJ_TYPE_GET_SLOT_OR_NULL(((mp_obj_type_t *)MP_OBJ_TO_PTR(bases_items[0])), protocol), 8);
1174+
mp_obj_dict_t *locals_ptr = MP_OBJ_TO_PTR(locals_dict);
1175+
MP_OBJ_TYPE_SET_SLOT(o, locals_dict, locals_ptr, 8);
11751176

1177+
if (bases_len > 0) {
11761178
if (bases_len >= 2) {
11771179
#if MICROPY_MULTIPLE_INHERITANCE
11781180
MP_OBJ_TYPE_SET_SLOT(o, parent, MP_OBJ_TO_PTR(bases_tuple), 9);
@@ -1182,10 +1184,15 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict)
11821184
} else {
11831185
MP_OBJ_TYPE_SET_SLOT(o, parent, MP_OBJ_TO_PTR(bases_items[0]), 9);
11841186
}
1185-
}
11861187

1187-
mp_obj_dict_t *locals_ptr = MP_OBJ_TO_PTR(locals_dict);
1188-
MP_OBJ_TYPE_SET_SLOT(o, locals_dict, locals_ptr, 10);
1188+
// Inherit protocol from a base class. This allows to define an
1189+
// abstract base class which would translate C-level protocol to
1190+
// Python method calls, and any subclass inheriting from it will
1191+
// support this feature.
1192+
if (base_protocol) {
1193+
MP_OBJ_TYPE_SET_SLOT(o, protocol, base_protocol, 10);
1194+
}
1195+
}
11891196

11901197
#if ENABLE_SPECIAL_ACCESSORS
11911198
// Check if the class has any special accessor methods

0 commit comments

Comments
 (0)