optimize(value): less clone
This commit is contained in:
@@ -87,13 +87,13 @@ impl<'jit: 'vm, 'vm> AttrSet<'jit, 'vm> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn eq_impl(&self, other: &AttrSet<'jit, 'vm>, vm: &'vm VM<'jit>) -> bool {
|
||||
pub fn eq_impl(&self, other: &AttrSet<'jit, 'vm>) -> bool {
|
||||
self.data.iter().len() == other.data.iter().len()
|
||||
&& std::iter::zip(
|
||||
self.data.iter().sorted_by_key(|(k, _)| **k),
|
||||
self.data.iter().sorted_by_key(|(k, _)| **k),
|
||||
)
|
||||
.all(|((_, v1), (_, v2))| v1.eq_impl(v2, vm))
|
||||
.all(|((_, v1), (_, v2))| v1.eq_impl(v2))
|
||||
}
|
||||
|
||||
pub fn to_public(&self, vm: &'vm VM, seen: &mut HashSet<Value<'jit, 'vm>>) -> p::Value {
|
||||
|
||||
@@ -56,14 +56,12 @@ impl Hash for Value<'_, '_> {
|
||||
}
|
||||
|
||||
impl<'jit: 'vm, 'vm> Value<'jit, 'vm> {
|
||||
fn eq_impl(&self, other: &Self, vm: &'vm VM<'jit>) -> bool {
|
||||
fn eq_impl(&self, other: &Self) -> bool {
|
||||
use Value::*;
|
||||
match (self, other) {
|
||||
(Const(a), Const(b)) => a.eq(b),
|
||||
(AttrSet(a), AttrSet(b)) => a.eq_impl(b, vm),
|
||||
(AttrSet(a), AttrSet(b)) => a.eq_impl(b),
|
||||
(List(a), List(b)) => a.eq(b),
|
||||
(Builtins(_), AttrSet(attrs)) => attrs.has_attr(vm.new_sym("builtins")),
|
||||
(AttrSet(attrs), Builtins(_)) => attrs.has_attr(vm.new_sym("builtins")),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
@@ -213,41 +211,48 @@ impl<'jit, 'vm> Value<'jit, 'vm> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn and(self, other: Self) -> Self {
|
||||
pub fn and(&mut self, other: Self) {
|
||||
use Const::*;
|
||||
match (self, other) {
|
||||
(VmConst(Bool(a)), VmConst(Bool(b))) => VmConst(Bool(a && b)),
|
||||
(x @ Value::Catchable(_), _) | (_, x @ Value::Catchable(_)) => x,
|
||||
*self = match (&*self, other) {
|
||||
(VmConst(Bool(a)), VmConst(Bool(b))) => VmConst(Bool(*a && b)),
|
||||
(Value::Catchable(_), _) => return,
|
||||
(_, x @ Value::Catchable(_)) => x,
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn or(self, other: Self) -> Self {
|
||||
pub fn or(&mut self, other: Self) {
|
||||
use Const::*;
|
||||
match (self, other) {
|
||||
(VmConst(Bool(a)), VmConst(Bool(b))) => VmConst(Bool(a || b)),
|
||||
(x @ Value::Catchable(_), _) | (_, x @ Value::Catchable(_)) => x,
|
||||
*self = match (&*self, other) {
|
||||
(VmConst(Bool(a)), VmConst(Bool(b))) => VmConst(Bool(*a || b)),
|
||||
(Value::Catchable(_), _) => return,
|
||||
(_, x @ Value::Catchable(_)) => x,
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn eq(self, other: Self, vm: &'vm VM<'jit>) -> Self {
|
||||
pub fn eq(&mut self, other: Self) {
|
||||
use Const::Bool;
|
||||
match (self, other) {
|
||||
(x @ Value::Catchable(_), _) | (_, x @ Value::Catchable(_)) => x,
|
||||
(s, other) => VmConst(Bool(s.eq_impl(&other, vm))),
|
||||
}
|
||||
*self = match (&*self, other) {
|
||||
(Value::Catchable(_), _) => return,
|
||||
(_, x @ Value::Catchable(_)) => x,
|
||||
(s, other) => VmConst(Bool(s.eq_impl(&other))),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn lt(self, other: Self) -> Self {
|
||||
pub fn lt(&mut self, other: Self) {
|
||||
use Const::*;
|
||||
VmConst(Bool(match (self, other) {
|
||||
(VmConst(Int(a)), VmConst(Int(b))) => a < b,
|
||||
(VmConst(Int(a)), VmConst(Float(b))) => (a as f64) < b,
|
||||
(VmConst(Float(a)), VmConst(Int(b))) => a < b as f64,
|
||||
(VmConst(Float(a)), VmConst(Float(b))) => a < b,
|
||||
(VmConst(String(a)), VmConst(String(b))) => a < b,
|
||||
(x @ Value::Catchable(_), _) | (_, x @ Value::Catchable(_)) => return x,
|
||||
*self = VmConst(Bool(match (&*self, other) {
|
||||
(VmConst(Int(a)), VmConst(Int(b))) => *a < b,
|
||||
(VmConst(Int(a)), VmConst(Float(b))) => (*a as f64) < b,
|
||||
(VmConst(Float(a)), VmConst(Int(b))) => *a < b as f64,
|
||||
(VmConst(Float(a)), VmConst(Float(b))) => *a < b,
|
||||
(VmConst(String(a)), VmConst(String(b))) => a < &b,
|
||||
(Value::Catchable(_), _) => return,
|
||||
(_, x @ Value::Catchable(_)) => {
|
||||
*self = x;
|
||||
return;
|
||||
},
|
||||
_ => todo!(),
|
||||
}))
|
||||
}
|
||||
@@ -262,11 +267,11 @@ impl<'jit, 'vm> Value<'jit, 'vm> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add(self, other: Self) -> Self {
|
||||
pub fn add(&mut self, other: Self) {
|
||||
use Const::*;
|
||||
match (self, other) {
|
||||
*self = match (&*self, other) {
|
||||
(VmConst(Int(a)), VmConst(Int(b))) => VmConst(Int(a + b)),
|
||||
(VmConst(Int(a)), VmConst(Float(b))) => VmConst(Float(a as f64 + b)),
|
||||
(VmConst(Int(a)), VmConst(Float(b))) => VmConst(Float(*a as f64 + b)),
|
||||
(VmConst(Float(a)), VmConst(Int(b))) => VmConst(Float(a + b as f64)),
|
||||
(VmConst(Float(a)), VmConst(Float(b))) => VmConst(Float(a + b)),
|
||||
(VmConst(String(a)), VmConst(String(b))) => {
|
||||
@@ -274,37 +279,41 @@ impl<'jit, 'vm> Value<'jit, 'vm> {
|
||||
string.push_str(b.as_str());
|
||||
VmConst(String(string))
|
||||
}
|
||||
(x @ Value::Catchable(_), _) | (_, x @ Value::Catchable(_)) => x,
|
||||
(Value::Catchable(_), _) => return,
|
||||
(_, x @ Value::Catchable(_)) => x,
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mul(self, other: Self) -> Self {
|
||||
pub fn mul(&mut self, other: Self) {
|
||||
use Const::*;
|
||||
match (self, other) {
|
||||
*self = match (&*self, other) {
|
||||
(VmConst(Int(a)), VmConst(Int(b))) => VmConst(Int(a * b)),
|
||||
(VmConst(Int(a)), VmConst(Float(b))) => VmConst(Float(a as f64 * b)),
|
||||
(VmConst(Int(a)), VmConst(Float(b))) => VmConst(Float(*a as f64 * b)),
|
||||
(VmConst(Float(a)), VmConst(Int(b))) => VmConst(Float(a * b as f64)),
|
||||
(VmConst(Float(a)), VmConst(Float(b))) => VmConst(Float(a * b)),
|
||||
(x @ Value::Catchable(_), _) | (_, x @ Value::Catchable(_)) => x,
|
||||
(Value::Catchable(_), _) => return,
|
||||
(_, x @ Value::Catchable(_)) => x,
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn div(self, other: Self) -> Result<Self> {
|
||||
pub fn div(&mut self, other: Self) -> Result<()> {
|
||||
use Const::*;
|
||||
Ok(match (self, other) {
|
||||
*self = match (&*self, other) {
|
||||
(_, VmConst(Int(0))) => return Err(Error::EvalError("division by zero".to_string())),
|
||||
(_, VmConst(Float(0.))) => {
|
||||
return Err(Error::EvalError("division by zero".to_string()));
|
||||
}
|
||||
(VmConst(Int(a)), VmConst(Int(b))) => VmConst(Int(a / b)),
|
||||
(VmConst(Int(a)), VmConst(Float(b))) => VmConst(Float(a as f64 / b)),
|
||||
(VmConst(Int(a)), VmConst(Float(b))) => VmConst(Float(*a as f64 / b)),
|
||||
(VmConst(Float(a)), VmConst(Int(b))) => VmConst(Float(a / b as f64)),
|
||||
(VmConst(Float(a)), VmConst(Float(b))) => VmConst(Float(a / b)),
|
||||
(x @ Value::Catchable(_), _) | (_, x @ Value::Catchable(_)) => x,
|
||||
(Value::Catchable(_), _) => return Ok(()),
|
||||
(_, x @ Value::Catchable(_)) => x,
|
||||
_ => todo!(),
|
||||
})
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn concat_string(&mut self, mut other: Self) -> &mut Self {
|
||||
@@ -329,13 +338,16 @@ impl<'jit, 'vm> Value<'jit, 'vm> {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn concat(self, other: Self) -> Self {
|
||||
pub fn concat(&mut self, other: Self) {
|
||||
if let x @ Value::Catchable(_) = other {
|
||||
*self = x;
|
||||
return;
|
||||
}
|
||||
match (self, other) {
|
||||
(Value::List(mut a), Value::List(b)) => {
|
||||
Rc::make_mut(&mut a).concat(b.as_ref());
|
||||
Value::List(a)
|
||||
(Value::List(a), Value::List(b)) => {
|
||||
Rc::make_mut(a).concat(b.as_ref());
|
||||
}
|
||||
(x @ Value::Catchable(_), _) | (_, x @ Value::Catchable(_)) => x,
|
||||
(Value::Catchable(_), _) => (),
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
@@ -352,13 +364,16 @@ impl<'jit, 'vm> Value<'jit, 'vm> {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn update(self, other: Self) -> Self {
|
||||
pub fn update(&mut self, other: Self) {
|
||||
if let x @ Value::Catchable(_) = other {
|
||||
*self = x;
|
||||
return;
|
||||
}
|
||||
match (self, other) {
|
||||
(Value::AttrSet(mut a), Value::AttrSet(b)) => {
|
||||
Rc::make_mut(&mut a).update(b.as_ref());
|
||||
Value::AttrSet(a)
|
||||
(Value::AttrSet(a), Value::AttrSet(b)) => {
|
||||
Rc::make_mut(a).update(b.as_ref());
|
||||
}
|
||||
(x @ Value::Catchable(_), _) | (_, x @ Value::Catchable(_)) => x,
|
||||
(Value::Catchable(_), _) => (),
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user