From a28ea9cca62f104923f3ed38757cda88a261e517 Mon Sep 17 00:00:00 2001 From: Edd Barrett Date: Fri, 6 Mar 2026 10:52:07 +0000 Subject: [PATCH] Optimise away bytecode dispatch. ``` Mean Std.Dev. Min Median Max real 1.504 0.003 1.500 1.505 1.507 user 1.510 0.003 1.506 1.509 1.513 sys 0.008 0.004 0.000 0.008 0.012 ``` ``` Mean Std.Dev. Min Median Max real 0.166 0.001 0.165 0.166 0.167 user 0.171 0.004 0.166 0.172 0.177 sys 0.006 0.004 0.000 0.004 0.012 ``` ``` Mean Std.Dev. Min Median Max real 0.247 0.001 0.244 0.247 0.249 user 0.247 0.001 0.244 0.247 0.248 sys 0.000 0.000 0.000 0.000 0.000 ``` --- py/vm.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/py/vm.c b/py/vm.c index ee139ab6efe8a..e7cf713def1b4 100644 --- a/py/vm.c +++ b/py/vm.c @@ -215,6 +215,17 @@ MP_NOINLINE static mp_obj_t *build_slice_stack_allocated(byte op, mp_obj_t *sp, } #endif +#ifdef USE_YK +// Elide instruction lookup. +// +// FIXME: Can the bytecode be mutated? If so we would need to add and promote a +// bytecode version tag (see yklua for an example). +__attribute__((yk_idempotent)) +byte load_inst(const byte *pc) { + return *pc; +} +#endif + // fastn has items in reverse order (fastn[0] is local[0], fastn[-1] is local[1], etc) // sp points to bottom of stack which grows up // returns: @@ -339,8 +350,12 @@ FRAME_SETUP(); #ifdef USE_YK mp_uint_t locidx = ip - code_state->fun_bc->bytecode; yk_mt_control_point(mp_state_ctx.ykmt, &yklocs[locidx]); + ip = (const byte *) yk_promote((void *) ip); + byte opcode = load_inst(ip++); +#else + byte opcode = *ip++; #endif - switch (*ip++) { + switch (opcode) { #endif ENTRY(MP_BC_LOAD_CONST_FALSE):