Skip to content
This repository was archived by the owner on Sep 6, 2023. It is now read-only.

Commit 9956fd0

Browse files
author
Paul Sokolovsky
committed
py/objtype: Fit qstrs for special methods in byte type.
Update makeqstrdata.py to sort strings starting with "__" to the beginning of qstr list, so they get low qstr id's, guaranteedly fitting in 8 bits. Then use this property to further compact op_id => qstr mapping arrays.
1 parent f2baa9e commit 9956fd0

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

py/makeqstrdata.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,15 @@ def parse_input_headers(infiles):
108108
continue
109109

110110
# add the qstr to the list, with order number to retain original order in file
111-
qstrs[ident] = (len(qstrs), ident, qstr)
111+
order = len(qstrs)
112+
# but put special method names like __add__ at the top of list, so
113+
# that their id's fit into a byte
114+
if ident == "":
115+
# Sort empty qstr above all still
116+
order = -200000
117+
elif ident.startswith("__"):
118+
order -= 100000
119+
qstrs[ident] = (order, ident, qstr)
112120

113121
if not qcfgs:
114122
sys.stderr.write("ERROR: Empty preprocessor output - check for errors above\n")

py/objtype.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,9 @@ mp_obj_t mp_obj_instance_make_new(const mp_obj_type_t *self, size_t n_args, size
332332
return MP_OBJ_FROM_PTR(o);
333333
}
334334

335-
const uint16_t mp_unary_op_method_name[MP_UNARY_OP_NUM_RUNTIME] = {
335+
// Qstrs for special methods are guaranteed to have a small value, so we use byte
336+
// type to represent them.
337+
const byte mp_unary_op_method_name[MP_UNARY_OP_NUM_RUNTIME] = {
336338
[MP_UNARY_OP_BOOL] = MP_QSTR___bool__,
337339
[MP_UNARY_OP_LEN] = MP_QSTR___len__,
338340
[MP_UNARY_OP_HASH] = MP_QSTR___hash__,
@@ -406,9 +408,11 @@ STATIC mp_obj_t instance_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
406408
}
407409

408410
// Binary-op enum values not listed here will have the default value of 0 in the
409-
// table, corresponding to MP_QSTR_, and are therefore unsupported (a lookup will
411+
// table, corresponding to MP_QSTR_NULL, and are therefore unsupported (a lookup will
410412
// fail). They can be added at the expense of code size for the qstr.
411-
const uint16_t mp_binary_op_method_name[MP_BINARY_OP_NUM_RUNTIME] = {
413+
// Qstrs for special methods are guaranteed to have a small value, so we use byte
414+
// type to represent them.
415+
const byte mp_binary_op_method_name[MP_BINARY_OP_NUM_RUNTIME] = {
412416
[MP_BINARY_OP_LESS] = MP_QSTR___lt__,
413417
[MP_BINARY_OP_MORE] = MP_QSTR___gt__,
414418
[MP_BINARY_OP_EQUAL] = MP_QSTR___eq__,

py/runtime.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ typedef struct _mp_arg_t {
5757
} mp_arg_t;
5858

5959
// Tables mapping operator enums to qstrs, defined in objtype.c
60-
extern const uint16_t mp_unary_op_method_name[];
61-
extern const uint16_t mp_binary_op_method_name[];
60+
extern const byte mp_unary_op_method_name[];
61+
extern const byte mp_binary_op_method_name[];
6262

6363
void mp_init(void);
6464
void mp_deinit(void);

0 commit comments

Comments
 (0)