Skip to content

Commit 12d6d77

Browse files
committed
py: Small optimisation of logic flow in BC_WITH_CLEANUP bytecode.
Slightly smaller code, and does not need to use C stack to save temporaries.
1 parent 74589cb commit 12d6d77

1 file changed

Lines changed: 12 additions & 14 deletions

File tree

py/vm.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -566,26 +566,24 @@ mp_vm_return_kind_t mp_execute_bytecode(mp_code_state *code_state, volatile mp_o
566566
SET_TOP(mp_const_none);
567567
mp_call_function_n_kw(obj, 3, 0, no_exc);
568568
} else if (MP_OBJ_IS_SMALL_INT(TOP())) {
569-
mp_obj_t cause = POP();
569+
mp_obj_t cause = TOP();
570570
switch (MP_OBJ_SMALL_INT_VALUE(cause)) {
571-
case UNWIND_RETURN: {
572-
mp_obj_t retval = POP();
573-
mp_call_function_n_kw(TOP(), 3, 0, no_exc);
574-
SET_TOP(retval);
575-
PUSH(cause);
576-
break;
577-
}
578-
case UNWIND_JUMP: {
571+
case UNWIND_RETURN:
579572
mp_call_function_n_kw(sp[-2], 3, 0, no_exc);
580-
// Pop __exit__ boundmethod at sp[-2]
581-
sp[-2] = sp[-1];
582-
sp[-1] = sp[0];
583-
SET_TOP(cause);
584573
break;
585-
}
574+
case UNWIND_JUMP:
575+
with_cleanup_no_other_choice:
576+
mp_call_function_n_kw(sp[-3], 3, 0, no_exc);
577+
// Pop __exit__ boundmethod at sp[-3]
578+
sp[-3] = sp[-2];
579+
break;
586580
default:
587581
assert(0);
582+
goto with_cleanup_no_other_choice; // to help flow control analysis
588583
}
584+
sp[-2] = sp[-1]; // copy retval down
585+
sp[-1] = sp[0]; // copy cause down
586+
sp--; // discard top value (was cause)
589587
} else if (mp_obj_is_exception_type(TOP())) {
590588
// Need to pass (sp[0], sp[-1], sp[-2]) as arguments so must reverse the
591589
// order of these on the value stack (don't want to create a temporary

0 commit comments

Comments
 (0)