@@ -1134,26 +1134,21 @@ impl LuaVM {
11341134 /// Fast path for calling CFunction metamethods with 2 arguments
11351135 /// Used by __index, __newindex, etc. Avoids Vec allocation.
11361136 /// Returns the first return value.
1137+ /// OPTIMIZED: Skip expensive get_function lookup by using a fixed offset from current base
11371138 #[ inline( always) ]
11381139 pub fn call_cfunc_metamethod_2 (
11391140 & mut self ,
11401141 cfunc : crate :: lua_value:: CFunction ,
11411142 arg1 : LuaValue ,
11421143 arg2 : LuaValue ,
11431144 ) -> LuaResult < Option < LuaValue > > {
1144- // Calculate new base position - use current frame's top area
1145+ // Fast path: use a fixed offset from current base (256 slots is enough for most cases)
1146+ // This avoids the expensive object_pool.get_function lookup
11451147 let new_base = if self . frame_count > 0 {
11461148 let current_frame = & self . frames [ self . frame_count - 1 ] ;
1147- let caller_base = current_frame. base_ptr as usize ;
1148- let caller_max_stack = if let Some ( func_id) = current_frame. get_function_id ( ) {
1149- self . object_pool
1150- . get_function ( func_id)
1151- . map ( |f| f. chunk . max_stack_size )
1152- . unwrap_or ( 256 )
1153- } else {
1154- 256
1155- } ;
1156- caller_base + caller_max_stack
1149+ // Use top as the base for nested calls, since all args are already there
1150+ // Adding 256 ensures we don't overwrite the caller's stack
1151+ ( current_frame. base_ptr as usize ) + 256
11571152 } else {
11581153 0
11591154 } ;
@@ -1162,9 +1157,12 @@ impl LuaVM {
11621157 self . ensure_stack_capacity ( new_base + stack_size) ;
11631158
11641159 // Set up arguments directly (no Vec allocation)
1165- self . register_stack [ new_base] = LuaValue :: cfunction ( cfunc) ;
1166- self . register_stack [ new_base + 1 ] = arg1;
1167- self . register_stack [ new_base + 2 ] = arg2;
1160+ unsafe {
1161+ let base = self . register_stack . as_mut_ptr ( ) . add ( new_base) ;
1162+ * base = LuaValue :: cfunction ( cfunc) ;
1163+ * base. add ( 1 ) = arg1;
1164+ * base. add ( 2 ) = arg2;
1165+ }
11681166
11691167 // Create C function frame
11701168 let temp_frame = LuaCallFrame :: new_c_function ( new_base, stack_size) ;
@@ -1188,6 +1186,7 @@ impl LuaVM {
11881186
11891187 /// Fast path for calling CFunction metamethods with 1 argument
11901188 /// Used by __len, __unm, __bnot, etc. Avoids Vec allocation.
1189+ /// OPTIMIZED: Skip expensive get_function lookup
11911190 #[ inline( always) ]
11921191 pub fn call_cfunc_metamethod_1 (
11931192 & mut self ,
@@ -1196,25 +1195,19 @@ impl LuaVM {
11961195 ) -> LuaResult < Option < LuaValue > > {
11971196 let new_base = if self . frame_count > 0 {
11981197 let current_frame = & self . frames [ self . frame_count - 1 ] ;
1199- let caller_base = current_frame. base_ptr as usize ;
1200- let caller_max_stack = if let Some ( func_id) = current_frame. get_function_id ( ) {
1201- self . object_pool
1202- . get_function ( func_id)
1203- . map ( |f| f. chunk . max_stack_size )
1204- . unwrap_or ( 256 )
1205- } else {
1206- 256
1207- } ;
1208- caller_base + caller_max_stack
1198+ ( current_frame. base_ptr as usize ) + 256
12091199 } else {
12101200 0
12111201 } ;
12121202
12131203 let stack_size = 2 ; // func + 1 arg
12141204 self . ensure_stack_capacity ( new_base + stack_size) ;
12151205
1216- self . register_stack [ new_base] = LuaValue :: cfunction ( cfunc) ;
1217- self . register_stack [ new_base + 1 ] = arg1;
1206+ unsafe {
1207+ let base = self . register_stack . as_mut_ptr ( ) . add ( new_base) ;
1208+ * base = LuaValue :: cfunction ( cfunc) ;
1209+ * base. add ( 1 ) = arg1;
1210+ }
12181211
12191212 let temp_frame = LuaCallFrame :: new_c_function ( new_base, stack_size) ;
12201213 self . push_frame ( temp_frame) ;
@@ -1236,6 +1229,7 @@ impl LuaVM {
12361229
12371230 /// Fast path for calling CFunction metamethods with 3 arguments
12381231 /// Used by __newindex. Avoids Vec allocation.
1232+ /// OPTIMIZED: Skip expensive get_function lookup
12391233 #[ inline( always) ]
12401234 pub fn call_cfunc_metamethod_3 (
12411235 & mut self ,
@@ -1246,27 +1240,21 @@ impl LuaVM {
12461240 ) -> LuaResult < Option < LuaValue > > {
12471241 let new_base = if self . frame_count > 0 {
12481242 let current_frame = & self . frames [ self . frame_count - 1 ] ;
1249- let caller_base = current_frame. base_ptr as usize ;
1250- let caller_max_stack = if let Some ( func_id) = current_frame. get_function_id ( ) {
1251- self . object_pool
1252- . get_function ( func_id)
1253- . map ( |f| f. chunk . max_stack_size )
1254- . unwrap_or ( 256 )
1255- } else {
1256- 256
1257- } ;
1258- caller_base + caller_max_stack
1243+ ( current_frame. base_ptr as usize ) + 256
12591244 } else {
12601245 0
12611246 } ;
12621247
12631248 let stack_size = 4 ; // func + 3 args
12641249 self . ensure_stack_capacity ( new_base + stack_size) ;
12651250
1266- self . register_stack [ new_base] = LuaValue :: cfunction ( cfunc) ;
1267- self . register_stack [ new_base + 1 ] = arg1;
1268- self . register_stack [ new_base + 2 ] = arg2;
1269- self . register_stack [ new_base + 3 ] = arg3;
1251+ unsafe {
1252+ let base = self . register_stack . as_mut_ptr ( ) . add ( new_base) ;
1253+ * base = LuaValue :: cfunction ( cfunc) ;
1254+ * base. add ( 1 ) = arg1;
1255+ * base. add ( 2 ) = arg2;
1256+ * base. add ( 3 ) = arg3;
1257+ }
12701258
12711259 let temp_frame = LuaCallFrame :: new_c_function ( new_base, stack_size) ;
12721260 self . push_frame ( temp_frame) ;
@@ -2408,8 +2396,8 @@ impl LuaVM {
24082396 self . pop_frame_discard ( ) ;
24092397 }
24102398
2411- // Return error - the actual message is stored in vm.error_message
2412- let msg = self . error_message . clone ( ) ;
2399+ // Return error - take the message to avoid allocation
2400+ let msg = std :: mem :: take ( & mut self . error_message ) ;
24132401 let error_str = self . create_string ( & msg) ;
24142402
24152403 Ok ( ( false , vec ! [ error_str] ) )
@@ -2480,19 +2468,10 @@ impl LuaVM {
24802468 LuaValueKind :: CFunction => {
24812469 let cfunc = func. as_cfunction ( ) . unwrap ( ) ;
24822470
2483- // Calculate new base position
2471+ // OPTIMIZED: Use fixed offset instead of expensive get_function lookup
24842472 let new_base = if self . frame_count > 0 {
24852473 let current_frame = & self . frames [ self . frame_count - 1 ] ;
2486- let caller_base = current_frame. base_ptr as usize ;
2487- let caller_max_stack = if let Some ( func_id) = current_frame. get_function_id ( ) {
2488- self . object_pool
2489- . get_function ( func_id)
2490- . map ( |f| f. chunk . max_stack_size )
2491- . unwrap_or ( 256 )
2492- } else {
2493- 256
2494- } ;
2495- caller_base + caller_max_stack
2474+ ( current_frame. base_ptr as usize ) + 256
24962475 } else {
24972476 0
24982477 } ;
@@ -2543,20 +2522,10 @@ impl LuaVM {
25432522 )
25442523 } ;
25452524
2546- // Calculate new base
2525+ // OPTIMIZED: Use fixed offset instead of expensive get_function lookup
25472526 let new_base = if self . frame_count > 0 {
25482527 let current_frame = & self . frames [ self . frame_count - 1 ] ;
2549- let caller_base = current_frame. base_ptr as usize ;
2550- let caller_max_stack =
2551- if let Some ( caller_func_id) = current_frame. get_function_id ( ) {
2552- self . object_pool
2553- . get_function ( caller_func_id)
2554- . map ( |f| f. chunk . max_stack_size )
2555- . unwrap_or ( 256 )
2556- } else {
2557- 256
2558- } ;
2559- caller_base + caller_max_stack
2528+ ( current_frame. base_ptr as usize ) + 256
25602529 } else {
25612530 0
25622531 } ;
0 commit comments