Skip to content

Commit a0d3299

Browse files
committed
mp_load_name(): Optimize for outer scope where locals == globals.
1 parent e3f58c8 commit a0d3299

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

py/runtime.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,15 @@ mp_obj_t mp_load_const_bytes(qstr qstr) {
9393

9494
mp_obj_t mp_load_name(qstr qstr) {
9595
// logic: search locals, globals, builtins
96-
DEBUG_OP_printf("load name %s\n", qstr_str(qstr));
97-
mp_map_elem_t *elem = mp_map_lookup(map_locals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
98-
if (elem != NULL) {
99-
return elem->value;
100-
} else {
101-
return mp_load_global(qstr);
96+
DEBUG_OP_printf("load name %s\n", map_locals, qstr_str(qstr));
97+
// If we're at the outer scope (locals == globals), dispatch to load_global right away
98+
if (map_locals != map_globals) {
99+
mp_map_elem_t *elem = mp_map_lookup(map_locals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
100+
if (elem != NULL) {
101+
return elem->value;
102+
}
102103
}
104+
return mp_load_global(qstr);
103105
}
104106

105107
mp_obj_t mp_load_global(qstr qstr) {

0 commit comments

Comments
 (0)