refactor: 扁平化对象属性存储并优化常量池与 IC 热路径

This commit is contained in:
爱特每羊队
2026-06-13 23:59:47 +08:00
parent dcc7db2575
commit bf74adf7f5
26 changed files with 307 additions and 87 deletions
+38 -2
View File
@@ -54,6 +54,7 @@ pub(crate) enum Label {
pub(crate) struct CompileCtx {
pub(crate) bytecode: Vec<opcode::Instr>,
pub(crate) constants: Vec<Constant>,
constant_map: HashMap<ConstantKey, u16>,
next_reg: u8,
pub(crate) max_regs: u8,
reserved_reg_start: u8,
@@ -85,11 +86,23 @@ pub(crate) enum FunctionBodyContext {
ClassElement,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum ConstantKey {
Number(u64),
Int(i32),
String(String),
Boolean(bool),
Null,
Undefined,
BytecodeFunc(u32),
}
impl CompileCtx {
pub(crate) fn new() -> Self {
Self {
bytecode: Vec::new(),
constants: Vec::new(),
constant_map: HashMap::new(),
next_reg: 0,
max_regs: 0,
reserved_reg_start: 0,
@@ -159,9 +172,17 @@ impl CompileCtx {
}
pub(crate) fn add_constant(&mut self, c: Constant) -> u16 {
if let Some(idx) = self.constants.iter().position(|x| x == &c) {
return idx as u16;
if let Some(key) = ConstantKey::from_constant(&c) {
if let Some(&idx) = self.constant_map.get(&key) {
return idx;
}
let idx = self.constants.len() as u16;
self.constants.push(c);
self.constant_map.insert(key, idx);
return idx;
}
let idx = self.constants.len();
self.constants.push(c);
idx as u16
@@ -290,6 +311,21 @@ impl CompileCtx {
}
}
impl ConstantKey {
fn from_constant(value: &Constant) -> Option<Self> {
match value {
Constant::Number(v) => Some(Self::Number(v.to_bits())),
Constant::Int(v) => Some(Self::Int(*v)),
Constant::String(v) => Some(Self::String(v.clone())),
Constant::Boolean(v) => Some(Self::Boolean(*v)),
Constant::Null => Some(Self::Null),
Constant::Undefined => Some(Self::Undefined),
Constant::BytecodeFunc(v) => Some(Self::BytecodeFunc(*v)),
Constant::RegExp(_, _) => None,
}
}
}
impl Compiler {
pub fn new() -> Self {
Self