Skip to content

Commit c60a261

Browse files
committed
py, vm: Replace save_ip, save_sp with code_state->{ip, sp}.
This may seem a bit of a risky change, in that it may introduce crazy bugs with respect to volatile variables in the VM loop. But, I think it should be fine: code_state points to some external memory, so the compiler should always read/write to that memory when accessing the ip/sp variables (ie not put them in registers). Anyway, it passes all tests and improves on all efficiency fronts: about 2-4% faster (64-bit unix), 16 bytes less stack space per call (64-bit unix) and slightly less executable size (unix and stmhal). The reason it's more efficient is save_ip and save_sp were volatile variables, so were anyway stored on the stack (in memory, not regs). Thus converting them to code_state->{ip, sp} doesn't cost an extra memory dereference (except maybe to get code_state, but that can be put in a register and then made more efficient for other uses of it).
1 parent c796985 commit c60a261

1 file changed

Lines changed: 8 additions & 10 deletions

File tree

py/vm.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ typedef enum {
9393
#define PUSH_EXC_BLOCK() \
9494
DECODE_ULABEL; /* except labels are always forward */ \
9595
++exc_sp; \
96-
exc_sp->opcode = *save_ip; \
96+
exc_sp->opcode = *code_state->ip; \
9797
exc_sp->handler = ip + unum; \
9898
exc_sp->val_sp = MP_TAGPTR_MAKE(sp, currently_in_except_block); \
9999
exc_sp->prev_exc = MP_OBJ_NULL; \
@@ -224,7 +224,7 @@ mp_vm_return_kind_t mp_execute_bytecode2(mp_code_state *code_state, volatile mp_
224224
#include "vmentrytable.h"
225225
#define DISPATCH() do { \
226226
TRACE(ip); \
227-
save_ip = ip; \
227+
code_state->ip = ip; \
228228
goto *entry_table[*ip++]; \
229229
} while(0)
230230
#define ENTRY(op) entry_##op
@@ -248,8 +248,6 @@ mp_vm_return_kind_t mp_execute_bytecode2(mp_code_state *code_state, volatile mp_
248248
// variables that are visible to the exception handler (declared volatile)
249249
volatile bool currently_in_except_block = MP_TAGPTR_TAG(code_state->exc_sp); // 0 or 1, to detect nested exceptions
250250
mp_exc_stack_t *volatile exc_sp = MP_TAGPTR_PTR(code_state->exc_sp); // stack grows up, exc_sp points to top of stack
251-
const byte *volatile save_ip = code_state->ip; // this is so we can access ip in the exception handler without making ip volatile (which means the compiler can't keep it in a register in the main loop)
252-
mp_obj_t *volatile save_sp = code_state->sp; // this is so we can access sp in the exception handler when needed
253251

254252
// outer exception handling loop
255253
for (;;) {
@@ -281,7 +279,7 @@ mp_vm_return_kind_t mp_execute_bytecode2(mp_code_state *code_state, volatile mp_
281279
DISPATCH();
282280
#else
283281
TRACE(ip);
284-
save_ip = ip;
282+
code_state->ip = ip;
285283
switch (*ip++) {
286284
#endif
287285

@@ -686,7 +684,7 @@ mp_vm_return_kind_t mp_execute_bytecode2(mp_code_state *code_state, volatile mp_
686684

687685
ENTRY(MP_BC_FOR_ITER): {
688686
DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
689-
save_sp = sp;
687+
code_state->sp = sp;
690688
assert(TOP());
691689
mp_obj_t value = mp_iternext_allow_raise(TOP());
692690
if (value == MP_OBJ_STOP_ITERATION) {
@@ -1024,12 +1022,12 @@ mp_vm_return_kind_t mp_execute_bytecode2(mp_code_state *code_state, volatile mp_
10241022
// exception occurred
10251023

10261024
// check if it's a StopIteration within a for block
1027-
if (*save_ip == MP_BC_FOR_ITER && mp_obj_is_subclass_fast(mp_obj_get_type(nlr.ret_val), &mp_type_StopIteration)) {
1028-
const byte *ip = save_ip + 1;
1025+
if (*code_state->ip == MP_BC_FOR_ITER && mp_obj_is_subclass_fast(mp_obj_get_type(nlr.ret_val), &mp_type_StopIteration)) {
1026+
const byte *ip = code_state->ip + 1;
10291027
machine_uint_t unum;
10301028
DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
10311029
code_state->ip = ip + unum; // jump to after for-block
1032-
code_state->sp = save_sp - 1; // pop the exhausted iterator
1030+
code_state->sp -= 1; // pop the exhausted iterator
10331031
goto outer_dispatch_loop; // continue with dispatch loop
10341032
}
10351033

@@ -1043,7 +1041,7 @@ mp_vm_return_kind_t mp_execute_bytecode2(mp_code_state *code_state, volatile mp_
10431041
qstr source_file = code_info[4] | (code_info[5] << 8) | (code_info[6] << 16) | (code_info[7] << 24);
10441042
qstr block_name = code_info[8] | (code_info[9] << 8) | (code_info[10] << 16) | (code_info[11] << 24);
10451043
machine_uint_t source_line = 1;
1046-
machine_uint_t bc = save_ip - code_info - code_info_size;
1044+
machine_uint_t bc = code_state->ip - code_info - code_info_size;
10471045
//printf("find %lu %d %d\n", bc, code_info[12], code_info[13]);
10481046
for (const byte* ci = code_info + 12; *ci && bc >= ((*ci) & 31); ci++) {
10491047
bc -= *ci & 31;

0 commit comments

Comments
 (0)