8000 py: Add n_state to mp_code_state_t struct. · jimmo/micropython@81d04a0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 81d04a0

Browse files
committed
py: Add n_state to mp_code_state_t struct.
This value is used often enough that it is better to cache it instead of decode it each time.
1 parent 4c5e1a0 commit 81d04a0

File tree

5 files changed

+12
-5
lines changed

5 files changed

+12
-5
lines changed

py/bc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ typedef struct _mp_code_state_t {
9999
mp_obj_fun_bc_t *fun_bc;
100100
const byte *ip;
101101
mp_obj_t *sp;
102+
uint16_t n_state;
102103
uint16_t exc_sp_idx;
103104
mp_obj_dict_t *old_globals;
104105
#if MICROPY_STACKLESS

py/emitnative.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,9 @@ STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scop
521521
// TODO this encoding may change size in the final pass, need to make it fixed
522522
emit_native_mov_state_imm_via(emit, emit->code_state_start + OFFSETOF_CODE_STATE_IP, emit->prelude_offset, REG_ARG_1);
523523

524+
// Set code_state.n_state (only works on little endian targets due to n_state being uint16_t)
525+
emit_native_mov_state_imm_via(emit, emit->code_state_start + offsetof(mp_code_state_t, n_state) / sizeof(uintptr_t), emit->n_state, REG_ARG_1);
526+
524527
// Put address of code_state into first arg
525528
ASM_MOV_REG_LOCAL_ADDR(emit->as, REG_ARG_1, emit->code_state_start);
526529

py/objfun.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,10 @@ STATIC void dump_args(const mp_obj_t *a, size_t sz) {
206206
+ n_exc_stack * sizeof(mp_exc_stack_t); \
207207
}
208208

209-
#define INIT_CODESTATE(code_state, _fun_bc, n_args, n_kw, args) \
209+
#define INIT_CODESTATE(code_state, _fun_bc, _n_state, n_args, n_kw, args) \
210210
code_state->fun_bc = _fun_bc; \
211211
code_state->ip = 0; \
212+
code_state->n_state = _n_state; \
212213
mp_setup_code_state(code_state, n_args, n_kw, args); \
213214
code_state->old_globals = mp_globals_get();
214215

@@ -235,7 +236,7 @@ mp_code_state_t *mp_obj_fun_bc_prepare_codestate(mp_obj_t self_in, size_t n_args
235236
}
236237
#endif
237238

238-
INIT_CODESTATE(code_state, self, n_args, n_kw, args);
239+
INIT_CODESTATE(code_state, self, n_state, n_args, n_kw, args);
239240

240241
// execute the byte code with the correct globals context
241242
mp_globals_set(self->globals);
@@ -280,7 +281,7 @@ STATIC mp_obj_t fun_bc_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const
280281
}
281282
#endif
282283

283-
INIT_CODESTATE(code_state, self, n_args, n_kw, args);
284+
INIT_CODESTATE(code_state, self, n_state, n_args, n_kw, args);
284285

285286
// execute the byte code with the correct globals context
286287
mp_globals_set(self->globals);

py/objgenerator.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ STATIC mp_obj_t gen_wrap_call(mp_obj_t self_in, size_t n_args, size_t n_kw, cons
6262
o->globals = self_fun->globals;
6363
o->code_state.fun_bc = self_fun;
6464
o->code_state.ip = 0;
65+
o->code_state.n_state = n_state;
6566
mp_setup_code_state(&o->code_state, n_args, n_kw, args);
6667
return MP_OBJ_FROM_PTR(o);
6768
}
@@ -99,6 +100,7 @@ STATIC mp_obj_t native_gen_wrap_call(mp_obj_t self_in, size_t n_args, size_t n_k
99100
o->globals = self_fun->globals;
100101
o->code_state.fun_bc = self_fun;
101102
o->code_state.ip = (const byte*)prelude_offset;
103+
o->code_state.n_state = n_state;
102104
mp_setup_code_state(&o->code_state, n_args, n_kw, args);
103105

104106
// Indicate we are a native function, which doesn't use this variable

py/vm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ FRAME_SETUP();
228228
mp_obj_t * /*const*/ fastn;
229229
mp_exc_stack_t * /*const*/ exc_stack;
230230
{
231-
size_t n_state = mp_decode_uint_value(code_state->fun_bc->bytecode);
231+
size_t n_state = code_state->n_state;
232232
fastn = &code_state->state[n_state - 1];
233233
exc_stack = (mp_exc_stack_t*)(code_state->state + n_state);
234234
}
@@ -1499,7 +1499,7 @@ unwind_jump:;
14991499
mp_nonlocal_free(code_state, sizeof(mp_code_state_t));
15001500
#endif
15011501
code_state = new_code_state;
1502-
size_t n_state = mp_decode_uint_value(code_state->fun_bc->bytecode);
1502+
size_t n_state = code_state->n_state;
15031503
fastn = &code_state->state[n_state - 1];
15041504
exc_stack = (mp_exc_stack_t*)(code_state->state + n_state);
15051505
// variables that are visible to the exception handler (declared volatile)

0 commit comments

Comments
 (0)
0