optimize: dedup consts

This commit is contained in:
2025-05-15 19:11:34 +08:00
parent 864be73e77
commit 1e50322af0
8 changed files with 51 additions and 31 deletions

View File

@@ -29,10 +29,11 @@ pub fn run(prog: Program, jit: JITContext<'_>) -> Result<p::Value> {
prog.funcs,
RefCell::new(prog.symbols),
RefCell::new(prog.symmap),
prog.consts,
jit
);
let env = env(&vm);
let temp = vm.eval(prog.top_level, env)?;
let temp = vm.eval(prog.top_level.into_iter(), env)?;
let temp = temp.to_public(&vm);
Ok(temp)
}
@@ -43,6 +44,7 @@ pub struct VM<'jit> {
funcs: Box<[F]>,
symbols: RefCell<Vec<EcoString>>,
symmap: RefCell<HashMap<EcoString, usize>>,
consts: Box<[Const]>,
jit: JITContext<'jit>,
}
@@ -70,7 +72,7 @@ impl<'vm, 'jit: 'vm> VM<'jit> {
}
}
pub fn eval(&'vm self, opcodes: OpCodes, env: Rc<Env<'vm>>) -> Result<Value<'vm>> {
pub fn eval(&'vm self, opcodes: impl Iterator<Item = OpCode>, env: Rc<Env<'vm>>) -> Result<Value<'vm>> {
let mut stack = Stack::<_, STACK_SIZE>::new();
let mut iter = opcodes.into_iter();
while let Some(opcode) = iter.next() {
@@ -94,7 +96,7 @@ impl<'vm, 'jit: 'vm> VM<'jit> {
) -> Result<usize> {
match opcode {
OpCode::Illegal => panic!("illegal opcode"),
OpCode::Const { value } => stack.push(Value::Const(value))?,
OpCode::Const { idx } => stack.push(Value::Const(self.consts[idx].clone()))?,
OpCode::LoadThunk { idx } => {
stack.push(Value::Thunk(Thunk::new(self.get_thunk(idx))))?
}