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>
This commit is contained in:
Jim Mussared
2021-08-19 22:46:40 +10:00
committed by Damien George
parent 910e060f93
commit 7b89ad8dbf
4 changed files with 31 additions and 1 deletions

View File

@@ -1081,6 +1081,10 @@ void mp_load_method_maybe(mp_obj_t obj, qstr attr, mp_obj_t *dest) {
dest[0] = MP_OBJ_NULL;
dest[1] = MP_OBJ_NULL;
// Note: the specific case of obj being an instance type is fast-path'ed in the VM
// for the MP_BC_LOAD_ATTR opcode. Instance types handle type->attr and look up directly
// in their member's map.
// get the type
const mp_obj_type_t *type = mp_obj_get_type(obj);