mirror of
https://github.com/pjh456/OxideJS.git
synced 2026-10-25 19:21:11 +08:00
perf: 模块树改用 Arc 持有,消除同步调用路径深层模块克隆
This commit is contained in:
@@ -166,7 +166,7 @@ pub struct Vm {
|
|||||||
pub(crate) session_bytes_allocated: usize,
|
pub(crate) session_bytes_allocated: usize,
|
||||||
pub object_prototype: P<JsObject>,
|
pub object_prototype: P<JsObject>,
|
||||||
pub math_rng_state: u64,
|
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) sub_module_constants: Vec<Vec<JsValue>>,
|
||||||
pub(crate) saved_bytecode_stack: Vec<Vec<opcode::Instr>>,
|
pub(crate) saved_bytecode_stack: Vec<Vec<opcode::Instr>>,
|
||||||
pub(crate) saved_constants_stack: Vec<Vec<JsValue>>,
|
pub(crate) saved_constants_stack: Vec<Vec<JsValue>>,
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use oxide_bytecode::module::CompiledModule;
|
use oxide_bytecode::module::CompiledModule;
|
||||||
use oxide_bytecode::opcode;
|
use oxide_bytecode::opcode;
|
||||||
|
|
||||||
@@ -30,7 +32,8 @@ impl Vm {
|
|||||||
}
|
}
|
||||||
self.native_call_depth += 1;
|
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 converted_constants = self.convert_constants(&sub.constants).map_err(String::from)?;
|
||||||
|
|
||||||
let saved = Box::new(InlineSyncState {
|
let saved = Box::new(InlineSyncState {
|
||||||
@@ -55,7 +58,7 @@ impl Vm {
|
|||||||
|
|
||||||
self.regs = [JsValue::undefined(); 256];
|
self.regs = [JsValue::undefined(); 256];
|
||||||
self.pc = 0;
|
self.pc = 0;
|
||||||
self.bytecode = sub.bytecode;
|
self.bytecode = sub.bytecode.clone();
|
||||||
self.constants = converted_constants;
|
self.constants = converted_constants;
|
||||||
self.active_reg_limit = sub.n_registers.max(1);
|
self.active_reg_limit = sub.n_registers.max(1);
|
||||||
self.root_reg_limit = self.active_reg_limit;
|
self.root_reg_limit = self.active_reg_limit;
|
||||||
@@ -140,7 +143,7 @@ impl Vm {
|
|||||||
|
|
||||||
pub fn run(&mut self, module: &CompiledModule) -> Result<JsValue, String> {
|
pub fn run(&mut self, module: &CompiledModule) -> Result<JsValue, String> {
|
||||||
self.clear_execution_state();
|
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.constants = self.convert_constants(&module.constants).map_err(String::from)?;
|
||||||
self.sub_module_constants = vec![Vec::new(); self.sub_modules.len()];
|
self.sub_module_constants = vec![Vec::new(); self.sub_modules.len()];
|
||||||
self.bytecode = module.bytecode.clone();
|
self.bytecode = module.bytecode.clone();
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ impl Vm {
|
|||||||
session_bytes_allocated: 0,
|
session_bytes_allocated: 0,
|
||||||
object_prototype: obj_proto,
|
object_prototype: obj_proto,
|
||||||
math_rng_state: 0,
|
math_rng_state: 0,
|
||||||
sub_modules: Vec::new(),
|
sub_modules: Arc::new(Vec::new()),
|
||||||
sub_module_constants: Vec::new(),
|
sub_module_constants: Vec::new(),
|
||||||
saved_bytecode_stack: Vec::new(),
|
saved_bytecode_stack: Vec::new(),
|
||||||
saved_constants_stack: Vec::new(),
|
saved_constants_stack: Vec::new(),
|
||||||
@@ -85,7 +85,7 @@ impl Vm {
|
|||||||
session_bytes_allocated: 0,
|
session_bytes_allocated: 0,
|
||||||
object_prototype: obj_proto,
|
object_prototype: obj_proto,
|
||||||
math_rng_state: 0,
|
math_rng_state: 0,
|
||||||
sub_modules: Vec::new(),
|
sub_modules: Arc::new(Vec::new()),
|
||||||
sub_module_constants: Vec::new(),
|
sub_module_constants: Vec::new(),
|
||||||
saved_bytecode_stack: Vec::new(),
|
saved_bytecode_stack: Vec::new(),
|
||||||
saved_constants_stack: Vec::new(),
|
saved_constants_stack: Vec::new(),
|
||||||
|
|||||||
Reference in New Issue
Block a user