optimize(env): single arg

This commit is contained in:
2025-05-20 09:47:30 +08:00
parent b4db46d48a
commit d0298ce2a6
8 changed files with 76 additions and 38 deletions

View File

@@ -1,6 +1,7 @@
use hashbrown::{HashMap, HashSet};
use inkwell::execution_engine::JitFunction;
use std::cell::{Cell, OnceCell, RefCell};
use std::rc::Rc;
use crate::builtins::env;
use crate::bytecode::{BinOp, Func as F, OpCode, OpCodes, Program, UnOp};
@@ -34,7 +35,7 @@ pub fn run(prog: Program, jit: JITContext<'_>) -> Result<p::Value> {
let env = env(&vm);
let mut seen = HashSet::new();
let value = vm
.eval(prog.top_level.into_iter(), env)?
.eval(prog.top_level.into_iter(), env.into())?
.to_public(&vm, &mut seen);
Ok(value)
}
@@ -82,7 +83,7 @@ impl<'vm, 'jit: 'vm> VM<'jit> {
pub fn eval(
&'vm self,
opcodes: impl Iterator<Item = OpCode>,
mut env: LetEnv<'jit, 'vm>,
mut env: Rc<LetEnv<'jit, 'vm>>,
) -> Result<Value<'jit, 'vm>> {
let mut stack = Stack::<_, STACK_SIZE>::new();
let mut iter = opcodes.into_iter();
@@ -103,7 +104,7 @@ impl<'vm, 'jit: 'vm> VM<'jit> {
&'vm self,
opcode: OpCode,
stack: &'s mut Stack<Value<'jit, 'vm>, CAP>,
env: &mut LetEnv<'jit, 'vm>,
env: &mut Rc<LetEnv<'jit, 'vm>>,
) -> Result<usize> {
match opcode {
OpCode::Illegal => panic!("illegal opcode"),
@@ -188,14 +189,11 @@ impl<'vm, 'jit: 'vm> VM<'jit> {
stack.push(Value::AttrSet(AttrSet::with_capacity(cap).into()))?;
}
OpCode::FinalizeRec => {
let env = env.clone().enter_let(
let env = env.clone().enter_attrs(
stack
.tos()?
.clone()
.unwrap_attr_set()
.as_inner()
.iter()
.map(|(k, v)| (k.clone(), v.clone())),
);
stack.tos_mut()?.as_mut().unwrap_attr_set().capture(&env);
}