perf: 模块树改用 Arc 持有,消除同步调用路径深层模块克隆

This commit is contained in:
2026-06-24 22:52:25 +08:00
parent 12bfe551cf
commit d7bc6a9b4e
3 changed files with 9 additions and 6 deletions
+1 -1
View File
@@ -166,7 +166,7 @@ pub struct Vm {
pub(crate) session_bytes_allocated: usize,
pub object_prototype: P<JsObject>,
pub math_rng_state: u64,
pub(crate) sub_modules: Vec<CompiledModule>,
pub(crate) sub_modules: Arc<Vec<CompiledModule>>,
pub(crate) sub_module_constants: Vec<Vec<JsValue>>,
pub(crate) saved_bytecode_stack: Vec<Vec<opcode::Instr>>,
pub(crate) saved_constants_stack: Vec<Vec<JsValue>>,
+6 -3
View File
@@ -1,3 +1,5 @@
use std::sync::Arc;
use oxide_bytecode::module::CompiledModule;
use oxide_bytecode::opcode;
@@ -30,7 +32,8 @@ impl Vm {
}
self.native_call_depth += 1;
let sub = self.sub_modules[sub_idx].clone();
let subs = Arc::clone(&self.sub_modules);
let sub = &subs[sub_idx];
let converted_constants = self.convert_constants(&sub.constants).map_err(String::from)?;
let saved = Box::new(InlineSyncState {
@@ -55,7 +58,7 @@ impl Vm {
self.regs = [JsValue::undefined(); 256];
self.pc = 0;
self.bytecode = sub.bytecode;
self.bytecode = sub.bytecode.clone();
self.constants = converted_constants;
self.active_reg_limit = sub.n_registers.max(1);
self.root_reg_limit = self.active_reg_limit;
@@ -140,7 +143,7 @@ impl Vm {
pub fn run(&mut self, module: &CompiledModule) -> Result<JsValue, String> {
self.clear_execution_state();
self.sub_modules = module.sub_modules.clone();
self.sub_modules = Arc::new(module.sub_modules.clone());
self.constants = self.convert_constants(&module.constants).map_err(String::from)?;
self.sub_module_constants = vec![Vec::new(); self.sub_modules.len()];
self.bytecode = module.bytecode.clone();
+2 -2
View File
@@ -38,7 +38,7 @@ impl Vm {
session_bytes_allocated: 0,
object_prototype: obj_proto,
math_rng_state: 0,
sub_modules: Vec::new(),
sub_modules: Arc::new(Vec::new()),
sub_module_constants: Vec::new(),
saved_bytecode_stack: Vec::new(),
saved_constants_stack: Vec::new(),
@@ -85,7 +85,7 @@ impl Vm {
session_bytes_allocated: 0,
object_prototype: obj_proto,
math_rng_state: 0,
sub_modules: Vec::new(),
sub_modules: Arc::new(Vec::new()),
sub_module_constants: Vec::new(),
saved_bytecode_stack: Vec::new(),
saved_constants_stack: Vec::new(),