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

@@ -1,3 +1,5 @@
use std::hash::Hash;
use derive_more::{IsVariant, Unwrap};
use ecow::EcoString;
@@ -7,6 +9,20 @@ pub enum Const {
Int(i64),
Float(f64),
String(EcoString),
Null
}
impl Hash for Const {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
use Const::*;
match self {
Int(x) => x.hash(state),
Float(x) => x.to_bits().hash(state),
Bool(x) => x.hash(state),
String(x) => x.hash(state),
x @ Null => x.hash(state),
}
}
}
impl From<bool> for Const {

View File

@@ -89,7 +89,7 @@ impl<'vm> Func<'vm> {
}
}
vm.eval(self.func.opcodes.clone(), env)
vm.eval(self.func.opcodes.iter().copied(), env)
}
}

View File

@@ -458,7 +458,7 @@ impl<'vm> Thunk<'vm> {
_Thunk::SuspendedFrom(self as *const Thunk),
)
.unwrap_code();
let value = vm.eval(opcodes.clone(), env.get().unwrap().clone())?;
let value = vm.eval(opcodes.iter().copied(), env.get().unwrap().clone())?;
let _ = std::mem::replace(
&mut *self.thunk.borrow_mut(),
_Thunk::Value(value.clone().into()),

View File

@@ -13,6 +13,7 @@ pub enum Const {
Int(i64),
Float(f64),
String(EcoString),
Null
}
impl Display for Const {
@@ -23,6 +24,7 @@ impl Display for Const {
Int(i) => write!(f, "{i}"),
Float(float) => write!(f, "{float}"),
String(s) => write!(f, "{s}"),
Null => write!(f, "null")
}
}
}
@@ -35,6 +37,7 @@ impl From<i::Const> for Const {
Int(int) => Const::Int(int),
Float(float) => Const::Float(float),
String(string) => Const::String(string),
Null => Const::Null
}
}
}