perf: 字节码调用压帧窗口按调用点存活上界拷贝减少整窗搬移

RegAlloc 改写后补第三次 liveness,为每条 CALL/NEW/SUPER_CALL 计算调用点
存活上界(最大存活物理槽号 + 1,254/255 由帧单独保存不计入),编码进
ext 高 8 位(nargs 低 8 位不变)。三处手写压帧窗口 = min(调用方活动寄存
器, 存活上界),restore_frame 按窗口回拷;CallFrame 新增
caller_active_reg_limit 保存调用方真实活动上限,恢复时不随截断窗口缩水。
生成器/异步函数体调用点保持全量窗口。spread 调用与运行时发起压帧
(accessor/内联)走全量路径不受影响。
This commit is contained in:
2026-08-15 09:56:22 +08:00
parent ff129d626f
commit 2239eeac24
13 changed files with 448 additions and 11 deletions
+18 -2
View File
@@ -87,7 +87,16 @@ impl Vm {
}
}
}
self.push_bytecode_frame(callee, this, &args, None, None, JsValue::undefined(), FrameContinuation::None)
self.push_bytecode_frame(
callee,
this,
&args,
None,
None,
JsValue::undefined(),
FrameContinuation::None,
0,
)
}
}
}
@@ -389,6 +398,8 @@ impl Vm {
let ext = self.bytecode[self.pc];
self.pc += 1;
let arg_count = (ext & 0xFF) as usize;
// ext 高 8 位 = 调用点存活上界(0 = 未编码/全量),压帧窗口按此截断。
let call_window = (ext >> 8) as u8;
let Some(frame) = self.frames.last() else {
self.raise_error_kind("ReferenceError", "super() used outside class constructor")?;
@@ -483,7 +494,8 @@ impl Vm {
let sub_n_args = self.sub_modules[sub_idx].n_args as usize;
let sub_n_registers = self.sub_modules[sub_idx].n_registers;
let sub_param_base = self.sub_modules[sub_idx].param_base as usize;
let caller_reg_limit = self.active_reg_limit.max(1);
let caller_active_reg_limit = self.active_reg_limit.max(1);
let caller_reg_limit = self.call_window_limit(caller_active_reg_limit, call_window);
let saved_reg_offset = self.save_stack.len() as u32;
self.save_stack.extend_from_slice(&self.regs[..caller_reg_limit as usize]);
let saved_this = self.regs[254];
@@ -517,6 +529,7 @@ impl Vm {
return_addr: self.pc,
function_name,
caller_reg_limit,
caller_active_reg_limit,
saved_reg_offset,
spill_offset: self.spill_stack.len() as u32,
arguments_base: args_base,
@@ -786,6 +799,7 @@ impl Vm {
None,
JsValue::undefined(),
FrameContinuation::None,
0,
)?;
return Ok(true);
}
@@ -892,6 +906,7 @@ impl Vm {
Some(new_obj_val),
constructor,
FrameContinuation::None,
0,
)?;
Ok(true)
} else {
@@ -979,6 +994,7 @@ impl Vm {
Some(derived_this),
new_target,
FrameContinuation::None,
0,
)?;
Ok(true)
} else {
+1
View File
@@ -667,6 +667,7 @@ mod tests {
return_addr: 0,
function_name: 0,
caller_reg_limit: 1,
caller_active_reg_limit: 1,
saved_reg_offset: 0,
spill_offset: 0,
arguments_base: 0,
+2
View File
@@ -272,6 +272,7 @@ impl SuspendedFrame {
return_addr: f.return_addr,
function_name: f.function_name,
caller_reg_limit: f.caller_reg_limit,
caller_active_reg_limit: f.caller_active_reg_limit,
saved_reg_offset: f.saved_reg_offset,
spill_offset: f.spill_offset,
arguments_base: f.arguments_base,
@@ -331,6 +332,7 @@ mod tests {
return_addr: 7,
function_name: 1,
caller_reg_limit: 2,
caller_active_reg_limit: 2,
saved_reg_offset: 3,
spill_offset: 4,
arguments_base: 5,
+28 -2
View File
@@ -170,7 +170,13 @@ pub enum FrameContinuation {
pub struct CallFrame {
pub return_addr: usize,
pub function_name: u32,
/// 压帧保存的调用方寄存器窗口长度(= save_stack 中本帧段大小)。
/// 普通字节码调用按调用点存活上界截断;运行时发起(accessor/内联)为
/// 调用方 `active_reg_limit` 全量。
pub caller_reg_limit: u8,
/// 压帧时刻调用方 `active_reg_limit`(真实值):帧恢复时据此还原,与
/// `caller_reg_limit`(可能被存活上界截断的窗口)解耦。
pub caller_active_reg_limit: u8,
pub saved_reg_offset: u32,
/// 记录本帧 spill 栈起始长度,作为 SPILL/UNSPILL 的帧边界基址(push 时快照)。
pub spill_offset: u32,
@@ -1139,10 +1145,24 @@ impl Vm {
self.call_bytecode_function_inline(callee, callee_obj, receiver, args)
}
/// 压帧窗口上界:调用方活动寄存器与调用点存活上界取 min。
///
/// # 边界与前提
/// - `call_window` 为 CALL ext 高 8 位编码的存活上界;0 = 未编码(回退全量)。
/// - 窗口至少为 1(reg 0 恒为调用结果槽)。
pub(crate) fn call_window_limit(&self, caller_active_reg_limit: u8, call_window: u8) -> u8 {
if call_window == 0 {
caller_active_reg_limit
} else {
caller_active_reg_limit.min(call_window)
}
.max(1)
}
#[expect(clippy::too_many_arguments)]
pub(crate) fn push_bytecode_frame(
&mut self, callee: JsValue, this_value: JsValue, args: &[JsValue], construct_result_reg: Option<u8>,
constructed_this: Option<JsValue>, new_target: JsValue, continuation: FrameContinuation,
constructed_this: Option<JsValue>, new_target: JsValue, continuation: FrameContinuation, call_window: u8,
) -> Result<(), String> {
vm_trace!(
"push_bytecode_frame: depth={}, args={}, continuation={:?}",
@@ -1174,7 +1194,11 @@ impl Vm {
let sub_n_registers = self.sub_modules[sub_idx].n_registers;
let sub_param_base = self.sub_modules[sub_idx].param_base as usize;
let sub_is_arrow = self.sub_modules[sub_idx].is_arrow;
let caller_reg_limit = self.active_reg_limit.max(1);
// 窗口 = min(调用方活动寄存器, 存活上界);call_window=0 表示调用方全量
// (运行时发起路径 / 未编码的旧模块)。恢复按窗口回拷,active_reg_limit
// 仍还原为调用方真实值(caller_active_reg_limit)。
let caller_active_reg_limit = self.active_reg_limit.max(1);
let caller_reg_limit = self.call_window_limit(caller_active_reg_limit, call_window);
let saved_reg_offset = self.save_stack.len() as u32;
self.save_stack.extend_from_slice(&self.regs[..caller_reg_limit as usize]);
let saved_this = self.regs[254];
@@ -1205,6 +1229,7 @@ impl Vm {
return_addr: self.pc,
function_name,
caller_reg_limit,
caller_active_reg_limit,
saved_reg_offset,
spill_offset: self.spill_stack.len() as u32,
arguments_base: args_base,
@@ -2213,6 +2238,7 @@ mod tests {
return_addr: 1,
function_name: 0,
caller_reg_limit: 2,
caller_active_reg_limit: 2,
saved_reg_offset: 0,
spill_offset: 0,
arguments_base: 0,
+3
View File
@@ -25,6 +25,8 @@ impl Vm {
let ext = self.bytecode[self.pc];
self.pc += 1;
let arg_count = (ext & 0xFF) as usize;
// ext 高 8 位 = 调用点存活上界(0 = 未编码/全量),压帧窗口按此截断。
let call_window = (ext >> 8) as u8;
crate::vm_debug!("CALL rd={} this={} args={} depth={}", rd, this_reg, arg_count, self.frames.len());
if obj.native_fn().is_some() {
@@ -65,6 +67,7 @@ impl Vm {
None,
JsValue::undefined(),
FrameContinuation::None,
call_window,
)?;
return Ok(true);
}
+5 -1
View File
@@ -43,6 +43,8 @@ impl Vm {
let ext = self.bytecode[self.pc];
self.pc += 1;
let arg_count = (ext & 0xFF) as usize;
// ext 高 8 位 = 调用点存活上界(0 = 未编码/全量),压帧窗口按此截断。
let call_window = (ext >> 8) as u8;
let proto_ptr = &*self.object_prototype as *const JsObject as *mut JsObject;
let new_obj = self.alloc_object(JsObject::new_empty(
@@ -113,7 +115,8 @@ impl Vm {
let sub_n_args = self.sub_modules[sub_idx].n_args as usize;
let sub_n_registers = self.sub_modules[sub_idx].n_registers;
let sub_param_base = self.sub_modules[sub_idx].param_base as usize;
let caller_reg_limit = self.active_reg_limit.max(1);
let caller_active_reg_limit = self.active_reg_limit.max(1);
let caller_reg_limit = self.call_window_limit(caller_active_reg_limit, call_window);
let saved_reg_offset = self.save_stack.len() as u32;
self.save_stack.extend_from_slice(&self.regs[..caller_reg_limit as usize]);
let saved_this = self.regs[254];
@@ -149,6 +152,7 @@ impl Vm {
.map(|name| self.kernel_core.perm_interner().intern(name).0)
.unwrap_or(0),
caller_reg_limit,
caller_active_reg_limit,
saved_reg_offset,
spill_offset: self.spill_stack.len() as u32,
arguments_base: args_base,
+2
View File
@@ -131,6 +131,7 @@ impl Vm {
None,
JsValue::undefined(),
FrameContinuation::AccessorGet { target_reg },
0,
)?;
self.accessor_frame_target_reg = Some(target_reg);
Ok(true)
@@ -330,6 +331,7 @@ impl Vm {
None,
JsValue::undefined(),
FrameContinuation::AccessorSet,
0,
)?;
Ok(())
}
+12 -2
View File
@@ -202,7 +202,16 @@ impl Vm {
self.saved_immutables_stack.clear();
self.cell_stack.clear();
self.inline_callee = None;
self.push_bytecode_frame(callee, this_value, args, None, None, JsValue::undefined(), FrameContinuation::None)
self.push_bytecode_frame(
callee,
this_value,
args,
None,
None,
JsValue::undefined(),
FrameContinuation::None,
0,
)
}
pub(crate) fn call_bytecode_function_inline(
@@ -331,7 +340,8 @@ impl Vm {
self.spill_stack.truncate(frame.spill_offset as usize);
self.regs[254] = frame.saved_this;
self.regs[255] = frame.saved_new_target;
self.active_reg_limit = frame.caller_reg_limit;
// active_reg_limit 还原为调用方真实值(caller_reg_limit 可能被存活上界截断)。
self.active_reg_limit = frame.caller_active_reg_limit;
self.pc = frame.return_addr;
}