Skip to content

Commit bc36521

Browse files
committed
py/vm: Optimise handling of stackless mode when pystack is enabled.
When pystack is enabled mp_obj_fun_bc_prepare_codestate() will always return a valid pointer, and if there is no more pystack available then it will raise an exception (a RuntimeError). So having pystack enabled with stackless enabled automatically gives strict stackless mode. There is therefore no need to have code for strict stackless mode when pystack is enabled, and this patch optimises the VM for such a case.
1 parent c7f880e commit bc36521

1 file changed

Lines changed: 41 additions & 37 deletions

File tree

py/vm.c

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -911,21 +911,22 @@ unwind_jump:;
911911
code_state->sp = sp;
912912
code_state->exc_sp = MP_TAGPTR_MAKE(exc_sp, currently_in_except_block);
913913
mp_code_state_t *new_state = mp_obj_fun_bc_prepare_codestate(*sp, unum & 0xff, (unum >> 8) & 0xff, sp + 1);
914-
if (new_state) {
914+
#if !MICROPY_ENABLE_PYSTACK
915+
if (new_state == NULL) {
916+
// Couldn't allocate codestate on heap: in the strict case raise
917+
// an exception, otherwise just fall through to stack allocation.
918+
#if MICROPY_STACKLESS_STRICT
919+
deep_recursion_error:
920+
mp_raise_recursion_depth();
921+
#endif
922+
} else
923+
#endif
924+
{
915925
new_state->prev = code_state;
916926
code_state = new_state;
917927
nlr_pop();
918928
goto run_code_state;
919929
}
920-
#if MICROPY_STACKLESS_STRICT
921-
else {
922-
deep_recursion_error:
923-
mp_raise_recursion_depth();
924-
}
925-
#else
926-
// If we couldn't allocate codestate on heap, in
927-
// non non-strict case fall thru to stack allocation.
928-
#endif
929930
}
930931
#endif
931932
SET_TOP(mp_call_function_n_kw(*sp, unum & 0xff, (unum >> 8) & 0xff, sp + 1));
@@ -956,20 +957,21 @@ unwind_jump:;
956957
// pystack is not enabled. For pystack, they are freed when code_state is.
957958
mp_nonlocal_free(out_args.args, out_args.n_alloc * sizeof(mp_obj_t));
958959
#endif
959-
if (new_state) {
960+
#if !MICROPY_ENABLE_PYSTACK
961+
if (new_state == NULL) {
962+
// Couldn't allocate codestate on heap: in the strict case raise
963+
// an exception, otherwise just fall through to stack allocation.
964+
#if MICROPY_STACKLESS_STRICT
965+
goto deep_recursion_error;
966+
#endif
967+
} else
968+
#endif
969+
{
960970
new_state->prev = code_state;
961971
code_state = new_state;
962972
nlr_pop();
963973
goto run_code_state;
964974
}
965-
#if MICROPY_STACKLESS_STRICT
966-
else {
967-
goto deep_recursion_error;
968-
}
969-
#else
970-
// If we couldn't allocate codestate on heap, in
971-
// non non-strict case fall thru to stack allocation.
972-
#endif
973975
}
974976
#endif
975977
SET_TOP(mp_call_method_n_kw_var(false, unum, sp));
@@ -993,20 +995,21 @@ unwind_jump:;
993995
int adjust = (sp[1] == MP_OBJ_NULL) ? 0 : 1;
994996

995997
mp_code_state_t *new_state = mp_obj_fun_bc_prepare_codestate(*sp, n_args + adjust, n_kw, sp + 2 - adjust);
996-
if (new_state) {
998+
#if !MICROPY_ENABLE_PYSTACK
999+
if (new_state == NULL) {
1000+
// Couldn't allocate codestate on heap: in the strict case raise
1001+
// an exception, otherwise just fall through to stack allocation.
1002+
#if MICROPY_STACKLESS_STRICT
1003+
goto deep_recursion_error;
1004+
#endif
1005+
} else
1006+
#endif
1007+
{
9971008
new_state->prev = code_state;
9981009
code_state = new_state;
9991010
nlr_pop();
10001011
goto run_code_state;
10011012
}
1002-
#if MICROPY_STACKLESS_STRICT
1003-
else {
1004-
goto deep_recursion_error;
1005-
}
1006-
#else
1007-
// If we couldn't allocate codestate on heap, in
1008-
// non non-strict case fall thru to stack allocation.
1009-
#endif
10101013
}
10111014
#endif
10121015
SET_TOP(mp_call_method_n_kw(unum & 0xff, (unum >> 8) & 0xff, sp));
@@ -1037,20 +1040,21 @@ unwind_jump:;
10371040
// pystack is not enabled. For pystack, they are freed when code_state is.
10381041
mp_nonlocal_free(out_args.args, out_args.n_alloc * sizeof(mp_obj_t));
10391042
#endif
1040-
if (new_state) {
1043+
#if !MICROPY_ENABLE_PYSTACK
1044+
if (new_state == NULL) {
1045+
// Couldn't allocate codestate on heap: in the strict case raise
1046+
// an exception, otherwise just fall through to stack allocation.
1047+
#if MICROPY_STACKLESS_STRICT
1048+
goto deep_recursion_error;
1049+
#endif
1050+
} else
1051+
#endif
1052+
{
10411053
new_state->prev = code_state;
10421054
code_state = new_state;
10431055
nlr_pop();
10441056
goto run_code_state;
10451057
}
1046-
#if MICROPY_STACKLESS_STRICT
1047-
else {
1048-
goto deep_recursion_error;
1049-
}
1050-
#else
1051-
// If we couldn't allocate codestate on heap, in
1052-
// non non-strict case fall thru to stack allocation.
1053-
#endif
10541058
}
10551059
#endif
10561060
SET_TOP(mp_call_method_n_kw_var(true, unum, sp));

0 commit comments

Comments
 (0)