aboutsummaryrefslogtreecommitdiff
path: root/py/vm.c
diff options
context:
space:
mode:
authorJim Mussared <jim.mussared@gmail.com>2021-08-19 22:46:40 +1000
committerDamien George <damien@micropython.org>2021-09-16 16:02:15 +1000
commit7b89ad8dbf432ab51eea6d138e179bf51394c786 (patch)
treea94bbd7cd3cdb8e297a6582b7a160789241a8c1e /py/vm.c
parent910e060f93c02c51a498f57ef77ccbe02566855b (diff)
py/vm: Add a fast path for LOAD_ATTR on instance types.
When the LOAD_ATTR opcode is executed there are quite a few different cases that have to be handled, but the common case is accessing a member on an instance type. Typically, built-in types provide methods which is why this is common. Fortunately, for this specific case, if the member is found in the member map then there's no further processing. This optimisation does a relatively cheap check (type is instance) and then forwards directly to the member map lookup, falling back to the regular path if necessary. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
Diffstat (limited to 'py/vm.c')
-rw-r--r--py/vm.c21
1 files changed, 20 insertions, 1 deletions
diff --git a/py/vm.c b/py/vm.c
index bbfc9914e..e5a62e441 100644
--- a/py/vm.c
+++ b/py/vm.c
@@ -414,7 +414,26 @@ dispatch_loop:
FRAME_UPDATE();
MARK_EXC_IP_SELECTIVE();
DECODE_QSTR;
- SET_TOP(mp_load_attr(TOP(), qst));
+ mp_obj_t top = TOP();
+ mp_obj_t obj;
+ #if MICROPY_OPT_LOAD_ATTR_FAST_PATH
+ // For the specific case of an instance type, it implements .attr
+ // and forwards to its members map. Attribute lookups on instance
+ // types are extremely common, so avoid all the other checks and
+ // calls that normally happen first.
+ mp_map_elem_t *elem = NULL;
+ if (mp_obj_is_instance_type(mp_obj_get_type(top))) {
+ mp_obj_instance_t *self = MP_OBJ_TO_PTR(top);
+ elem = mp_map_lookup(&self->members, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
+ }
+ if (elem) {
+ obj = elem->value;
+ } else
+ #endif
+ {
+ obj = mp_load_attr(top, qst);
+ }
+ SET_TOP(obj);
DISPATCH();
}
#else