diff options
| author | Paul Sokolovsky | 2014-05-31 03:41:08 +0300 |
|---|---|---|
| committer | Paul Sokolovsky | 2014-05-31 18:19:33 +0300 |
| commit | b16523aa95ca0a95f3d5a82e6f691e088135d7ba (patch) | |
| tree | abe793c14595c4fcd82e83e292b1a19c8fe77ce2 | |
| parent | ff8da0b835e94462e6b5da6fbe48409fdff40b28 (diff) | |
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.
| -rw-r--r-- | py/vm.c | 11 |
1 files changed, 7 insertions, 4 deletions
@@ -28,6 +28,7 @@ #include <stdio.h> #include <string.h> #include <assert.h> +#include <alloca.h> #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; |
