@@ -19,7 +19,7 @@ pub use upvalue_instructions::*;
1919use super :: { LuaError , LuaResult , LuaVM , OpCode } ;
2020use crate :: lua_value:: { TAG_FALSE , TAG_FLOAT , TAG_INTEGER , TAG_NIL , TAG_TRUE , TYPE_MASK } ;
2121use crate :: lua_vm:: LuaCallFrame ;
22- use crate :: { LuaValue , UpvalueState , get_a, get_ax, get_b, get_bx, get_k, get_op, get_sbx, get_sj} ;
22+ use crate :: { LuaValue , get_a, get_ax, get_b, get_bx, get_k, get_op, get_sbx, get_sj} ;
2323
2424/// Save current pc to frame (like Lua C's savepc macro)
2525/// Called before operations that may call Lua functions (CALL, metamethods, etc.)
@@ -452,6 +452,7 @@ pub fn luavm_execute(vm: &mut LuaVM) -> LuaResult<LuaValue> {
452452 // ============ Upvalue operations (inline for performance) ============
453453 OpCode :: GetUpval => {
454454 // INLINED GETUPVAL: R[A] := UpValue[B]
455+ // OPTIMIZED: Direct field access with branch prediction
455456 let a = get_a ! ( instr) ;
456457 let b = get_b ! ( instr) ;
457458
@@ -460,11 +461,10 @@ pub fn luavm_execute(vm: &mut LuaVM) -> LuaResult<LuaValue> {
460461
461462 // Read upvalue value directly
462463 let uv = vm. object_pool . get_upvalue_unchecked ( upvalue_id) ;
463- let value = match & uv. state {
464- UpvalueState :: Open { stack_index } => {
465- * vm. register_stack . as_ptr ( ) . add ( * stack_index)
466- }
467- UpvalueState :: Closed ( val) => * val,
464+ let value = if uv. is_open {
465+ * vm. register_stack . get_unchecked ( uv. stack_index )
466+ } else {
467+ uv. closed_value
468468 } ;
469469
470470 * vm. register_stack . as_mut_ptr ( ) . add ( base_ptr + a) = value;
@@ -473,6 +473,7 @@ pub fn luavm_execute(vm: &mut LuaVM) -> LuaResult<LuaValue> {
473473 }
474474 OpCode :: SetUpval => {
475475 // INLINED SETUPVAL: UpValue[B] := R[A]
476+ // OPTIMIZED: Direct field access with branch prediction
476477 let a = get_a ! ( instr) ;
477478 let b = get_b ! ( instr) ;
478479
@@ -485,12 +486,11 @@ pub fn luavm_execute(vm: &mut LuaVM) -> LuaResult<LuaValue> {
485486
486487 // Write upvalue value directly
487488 let uv = vm. object_pool . get_upvalue_mut_unchecked ( upvalue_id) ;
488- match & mut uv. state {
489- UpvalueState :: Open { stack_index } => {
490- * vm. register_stack . as_mut_ptr ( ) . add ( * stack_index) = value;
491- }
492- UpvalueState :: Closed ( val) => * val = value,
493- } ;
489+ if uv. is_open {
490+ * vm. register_stack . get_unchecked_mut ( uv. stack_index ) = value;
491+ } else {
492+ uv. closed_value = value;
493+ }
494494
495495 // GC write barrier for upvalue
496496 vm. gc_barrier_upvalue ( upvalue_id, & value) ;
0 commit comments