Skip to content

Commit f1c397b

Browse files
committed
reduce call frame size
1 parent f392c46 commit f1c397b

19 files changed

+559
-360
lines changed

crates/luars/src/gc/object_pool.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
use crate::lua_value::{Chunk, LuaThread, LuaUserdata};
1616
use crate::{LuaString, LuaTable, LuaValue};
17-
use slotmap::{new_key_type, SlotMap};
17+
use slotmap::{SlotMap, new_key_type};
1818
use std::hash::Hash;
1919
use std::rc::Rc;
2020

@@ -626,7 +626,7 @@ impl ObjectPool {
626626
strings: Arena::with_capacity(256),
627627
tables: Arena::with_capacity(64),
628628
functions: Arena::with_capacity(32),
629-
upvalues: SlotMap::with_capacity_and_key(32), // SlotMap for O(1) access
629+
upvalues: SlotMap::with_capacity_and_key(32), // SlotMap for O(1) access
630630
userdata: Arena::new(),
631631
threads: Arena::with_capacity(8),
632632
string_intern: StringInternTable::with_capacity(256),
@@ -901,24 +901,24 @@ impl ObjectPool {
901901
return self.create_string("");
902902
};
903903
let s = gs.data.as_str();
904-
904+
905905
// Clamp indices
906906
let start = start.min(s.len());
907907
let end = end.min(s.len());
908-
908+
909909
if start >= end {
910910
return self.create_string("");
911911
}
912-
912+
913913
// Fast path: return original if full range
914914
if start == 0 && end == s.len() {
915915
return source_id;
916916
}
917-
917+
918918
let slice = &s[start..end];
919919
let len = slice.len();
920920
let hash = Self::hash_string(slice);
921-
921+
922922
// For short strings, check intern table first before allocating
923923
if len <= self.max_intern_length {
924924
let compare = |id: StringId| -> bool {
@@ -943,7 +943,7 @@ impl ObjectPool {
943943
Some((slice.to_string(), hash, usize::MAX))
944944
}
945945
};
946-
946+
947947
// Second phase: allocate and insert (if needed)
948948
if let Some((substring, hash, insert_idx)) = intern_result {
949949
if insert_idx != usize::MAX {

crates/luars/src/lib_registry.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ pub fn create_standard_registry() -> LibraryRegistry {
205205
/// Helper to get function arguments from VM registers
206206
pub fn get_args(vm: &LuaVM) -> Vec<LuaValue> {
207207
let frame = vm.current_frame();
208-
let base_ptr = frame.base_ptr;
209-
let top = frame.top;
208+
let base_ptr = frame.base_ptr as usize;
209+
let top = frame.top as usize;
210210

211211
// Skip register 0 (the function itself), collect from 1 to top
212212
(1..top).map(|i| vm.register_stack[base_ptr + i]).collect()
@@ -217,8 +217,8 @@ pub fn get_args(vm: &LuaVM) -> Vec<LuaValue> {
217217
#[inline(always)]
218218
pub fn get_arg(vm: &LuaVM, index: usize) -> Option<LuaValue> {
219219
let frame = vm.current_frame();
220-
let base_ptr = frame.base_ptr;
221-
let top = frame.top;
220+
let base_ptr = frame.base_ptr as usize;
221+
let top = frame.top as usize;
222222

223223
// Arguments use 1-based indexing (Lua convention)
224224
// Register 0 is the function itself
@@ -252,5 +252,5 @@ pub fn require_arg(vm: &mut LuaVM, index: usize, func_name: &str) -> LuaResult<L
252252
pub fn arg_count(vm: &LuaVM) -> usize {
253253
let frame = vm.current_frame();
254254
// Subtract 1 for the function itself
255-
frame.top.saturating_sub(1)
255+
frame.top.saturating_sub(1) as usize
256256
}

crates/luars/src/lua_async.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ impl AsyncExecutor {
9292
let func = self
9393
.async_functions
9494
.get(func_name)
95-
.ok_or_else(|| {
96-
format!("Async function '{}' not registered", func_name)
97-
})?
95+
.ok_or_else(|| format!("Async function '{}' not registered", func_name))?
9896
.clone();
9997

10098
let task_id = self.next_task_id;
@@ -192,7 +190,9 @@ pub fn create_async_wrapper(func_name: String) -> impl Fn(&mut LuaVM) -> LuaResu
192190
}
193191

194192
// 启动异步任务
195-
let task_id = vm.async_executor.spawn_task(&func_name, args, coroutine)
193+
let task_id = vm
194+
.async_executor
195+
.spawn_task(&func_name, args, coroutine)
196196
.map_err(|e| vm.error(e))?;
197197

198198
// Yield协程,返回task_id

crates/luars/src/lua_value/lua_table.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl LuaTable {
9696
// For floats: similar to integers
9797
// XOR gives good mixing without branch
9898
let raw = key.primary.wrapping_add(key.secondary);
99-
99+
100100
// Fibonacci hashing - excellent distribution for sequential IDs
101101
// Golden ratio: 2^64 / phi ≈ 0x9e3779b97f4a7c15
102102
let hash = raw.wrapping_mul(0x9e3779b97f4a7c15);

crates/luars/src/lua_value/lua_value.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@ impl LuaValue {
150150
}
151151
}
152152

153+
#[inline(always)]
154+
pub fn cfunction_ptr(f_ptr: usize) -> Self {
155+
Self {
156+
primary: TAG_CFUNCTION,
157+
secondary: f_ptr as u64,
158+
}
159+
}
160+
153161
#[inline(always)]
154162
pub fn userdata(id: UserdataId) -> Self {
155163
Self {

crates/luars/src/lua_vm/execute/arithmetic_instructions.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,11 @@ pub fn exec_mod(vm: &mut LuaVM, instr: u32, pc: &mut usize, base_ptr: usize) {
282282

283283
let float_result = l_float - (l_float / r_float).floor() * r_float;
284284
// Check if result is an integer
285-
if float_result.fract() == 0.0 && float_result.is_finite()
286-
&& float_result >= i64::MIN as f64 && float_result <= i64::MAX as f64 {
285+
if float_result.fract() == 0.0
286+
&& float_result.is_finite()
287+
&& float_result >= i64::MIN as f64
288+
&& float_result <= i64::MAX as f64
289+
{
287290
LuaValue::integer(float_result as i64)
288291
} else {
289292
LuaValue::number(float_result)
@@ -583,9 +586,13 @@ pub fn exec_modk(
583586
// Float % Float - check if result is integer
584587
if let (Some(l), Some(r)) = (left.as_number(), constant.as_number()) {
585588
let float_result = l - (l / r).floor() * r;
586-
if float_result.fract() == 0.0 && float_result.is_finite()
587-
&& float_result >= i64::MIN as f64 && float_result <= i64::MAX as f64 {
588-
*vm.register_stack.as_mut_ptr().add(base_ptr + a) = LuaValue::integer(float_result as i64);
589+
if float_result.fract() == 0.0
590+
&& float_result.is_finite()
591+
&& float_result >= i64::MIN as f64
592+
&& float_result <= i64::MAX as f64
593+
{
594+
*vm.register_stack.as_mut_ptr().add(base_ptr + a) =
595+
LuaValue::integer(float_result as i64);
589596
} else {
590597
*vm.register_stack.as_mut_ptr().add(base_ptr + a) = LuaValue::number(float_result);
591598
}

0 commit comments

Comments
 (0)