feat: less gc (WIP)

This commit is contained in:
2025-06-02 14:18:59 +08:00
parent d3442e87e7
commit 51f8df9cca
11 changed files with 125 additions and 179 deletions

View File

@@ -1,4 +1,5 @@
use std::cell::RefCell;
use std::rc::Rc;
use gc_arena::arena::CollectionPhase;
use gc_arena::{Arena, Collect, Gc, Mutation, Rootable};
@@ -123,7 +124,6 @@ pub fn eval<T, F: for<'gc> FnOnce(Value<'gc>, &mut GcRoot<'gc>, &Mutation<'gc>)
}
}
}
arena.collect_debt();
arena.mutate_root(|mc, root| {
assert_eq!(root.stack.len(), 1);
let ret = root.stack.pop();
@@ -158,7 +158,7 @@ fn single_op<'gc, const CAP: usize>(
Const::Int(x) => Value::Int(x),
Const::Float(x) => Value::Float(x),
Const::Bool(x) => Value::Bool(x),
Const::String(x) => Value::String(CoW::new(x.into(), mc)),
Const::String(x) => Value::String(Rc::new(x.into())),
Const::Null => Value::Null,
})?,
OpCode::LoadThunk { idx } => stack.push(Value::Thunk(Thunk::new(vm.get_thunk(idx), mc)))?,
@@ -199,7 +199,7 @@ fn single_op<'gc, const CAP: usize>(
}
OpCode::Func { idx } => {
let func = vm.get_func(idx);
stack.push(Value::Func(Gc::new(mc, Func::new(func, *env, mc))))?;
stack.push(Value::Func(Rc::new(Func::new(func, *env, mc))))?;
}
OpCode::Arg { level } => {
stack.push(env.lookup_arg(level).clone())?;
@@ -217,10 +217,10 @@ fn single_op<'gc, const CAP: usize>(
let mut rhs = stack.pop();
let lhs = stack.tos_mut();
match op {
Add => lhs.add(rhs, mc),
Add => lhs.add(rhs),
Sub => {
rhs.neg();
lhs.add(rhs, mc);
lhs.add(rhs);
}
Mul => lhs.mul(rhs),
Div => lhs.div(rhs)?,
@@ -228,26 +228,26 @@ fn single_op<'gc, const CAP: usize>(
Or => lhs.or(rhs),
Eq => Value::eq(lhs, rhs),
Lt => lhs.lt(rhs),
Con => lhs.concat(rhs, mc),
Upd => lhs.update(rhs, mc),
Con => lhs.concat(rhs),
Upd => lhs.update(rhs),
}
}
OpCode::ConcatString => {
let rhs = stack.pop();
stack.tos_mut().concat_string(rhs, mc);
stack.tos_mut().concat_string(rhs);
}
OpCode::Path => {
todo!()
}
OpCode::List { cap } => {
stack.push(Value::List(CoW::new(List::with_capacity(cap), mc)))?;
stack.push(Value::List(Rc::new(List::with_capacity(cap))))?;
}
OpCode::PushElem => {
let elem = stack.pop();
stack.tos_mut().push(elem, mc);
stack.tos_mut().push(elem);
}
OpCode::AttrSet { cap } => {
stack.push(Value::AttrSet(CoW::new(AttrSet::with_capacity(cap), mc)))?;
stack.push(Value::AttrSet(Rc::new(AttrSet::with_capacity(cap))))?;
}
OpCode::FinalizeLet => {
let mut list = stack.pop().unwrap_list();
@@ -256,17 +256,17 @@ fn single_op<'gc, const CAP: usize>(
.clone()
.into_inner();
*env = env.enter_let(map, mc);
list.make_mut(|list| list.capture(*env, mc), mc);
Rc::make_mut(&mut list).capture(*env, mc);
}
OpCode::PushStaticAttr { name } => {
let val = stack.pop();
stack.tos_mut().push_attr(name, val, mc);
stack.tos_mut().push_attr(name, val);
}
OpCode::PushDynamicAttr => {
let val = stack.pop();
let sym = stack.pop();
let sym = vm.new_sym::<&str>(&sym.unwrap_string());
stack.tos_mut().push_attr(sym, val, mc);
stack.tos_mut().push_attr(sym, val);
}
OpCode::Select { sym } => {
stack.tos_mut().select(sym, vm)?;