From b16523aa95ca0a95f3d5a82e6f691e088135d7ba Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 31 May 2014 03:41:08 +0300 Subject: [PATCH] vm: Don't unconditionally allocate state on stack, do that only if needed. This makes sure that only as much stack allocated as actually used, reducing stack usage for each Python function call. --- py/vm.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/py/vm.c b/py/vm.c index aa7e0e2cfc..b7a7569b52 100644 --- a/py/vm.c +++ b/py/vm.c @@ -28,6 +28,7 @@ #include #include #include +#include #include "mpconfig.h" #include "nlr.h" @@ -117,21 +118,23 @@ mp_vm_return_kind_t mp_execute_bytecode(const byte *code, const mp_obj_t *args, ip += 4; // allocate state for locals and stack - mp_obj_t temp_state[VM_MAX_STATE_ON_STACK]; - mp_obj_t *state = &temp_state[0]; #if DETECT_VM_STACK_OVERFLOW n_state += 1; #endif + mp_obj_t *state; if (n_state > VM_MAX_STATE_ON_STACK) { state = m_new(mp_obj_t, n_state); + } else { + state = alloca(sizeof(mp_obj_t) * n_state); } mp_obj_t *sp = &state[0] - 1; // allocate state for exceptions - mp_exc_stack_t exc_state[VM_MAX_EXC_STATE_ON_STACK]; - mp_exc_stack_t *exc_stack = &exc_state[0]; + mp_exc_stack_t *exc_stack; if (n_exc_stack > VM_MAX_EXC_STATE_ON_STACK) { exc_stack = m_new(mp_exc_stack_t, n_exc_stack); + } else { + exc_stack = alloca(sizeof(mp_exc_stack_t) * n_exc_stack); } mp_exc_stack_t *exc_sp = &exc_stack[0] - 1;