Commit b16523aa authored by Paul Sokolovsky's avatar Paul Sokolovsky
Browse files

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.
parent ff8da0b8
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include <alloca.h>
#include "mpconfig.h" #include "mpconfig.h"
#include "nlr.h" #include "nlr.h"
...@@ -117,21 +118,23 @@ mp_vm_return_kind_t mp_execute_bytecode(const byte *code, const mp_obj_t *args, ...@@ -117,21 +118,23 @@ mp_vm_return_kind_t mp_execute_bytecode(const byte *code, const mp_obj_t *args,
ip += 4; ip += 4;
// allocate state for locals and stack // 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 #if DETECT_VM_STACK_OVERFLOW
n_state += 1; n_state += 1;
#endif #endif
mp_obj_t *state;
if (n_state > VM_MAX_STATE_ON_STACK) { if (n_state > VM_MAX_STATE_ON_STACK) {
state = m_new(mp_obj_t, n_state); state = m_new(mp_obj_t, n_state);
} else {
state = alloca(sizeof(mp_obj_t) * n_state);
} }
mp_obj_t *sp = &state[0] - 1; mp_obj_t *sp = &state[0] - 1;
// allocate state for exceptions // allocate state for exceptions
mp_exc_stack_t exc_state[VM_MAX_EXC_STATE_ON_STACK]; mp_exc_stack_t *exc_stack;
mp_exc_stack_t *exc_stack = &exc_state[0];
if (n_exc_stack > VM_MAX_EXC_STATE_ON_STACK) { if (n_exc_stack > VM_MAX_EXC_STATE_ON_STACK) {
exc_stack = m_new(mp_exc_stack_t, n_exc_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; mp_exc_stack_t *exc_sp = &exc_stack[0] - 1;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment