From 456b08a205b05a2cc6986d4b9007eaf369820b5d Mon Sep 17 00:00:00 2001 From: Kasey White Date: Wed, 1 Feb 2023 14:20:15 -0500 Subject: [PATCH] minor performance improvements Changed a couple cases where the inner Rc object was cloned to use the Rc object instead --- crates/uplc/src/machine.rs | 23 +++++++++++++++++++++++ crates/uplc/src/machine/runtime.rs | 16 ++++++---------- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/crates/uplc/src/machine.rs b/crates/uplc/src/machine.rs index 6ef58bf2..7c5e8a98 100644 --- a/crates/uplc/src/machine.rs +++ b/crates/uplc/src/machine.rs @@ -680,6 +680,18 @@ impl TryFrom for Type { } } +impl TryFrom<&Value> for Type { + type Error = Error; + + fn try_from(value: &Value) -> Result { + let constant: Constant = value.try_into()?; + + let constant_type = Type::from(&constant); + + Ok(constant_type) + } +} + impl TryFrom for Constant { type Error = Error; @@ -691,6 +703,17 @@ impl TryFrom for Constant { } } +impl TryFrom<&Value> for Constant { + type Error = Error; + + fn try_from(value: &Value) -> Result { + match value { + Value::Con(constant) => Ok(constant.as_ref().clone()), + rest => Err(Error::NotAConstant(rest.clone())), + } + } +} + impl From<&Constant> for Type { fn from(constant: &Constant) -> Self { match constant { diff --git a/crates/uplc/src/machine/runtime.rs b/crates/uplc/src/machine/runtime.rs index 67543978..0bdb12ed 100644 --- a/crates/uplc/src/machine/runtime.rs +++ b/crates/uplc/src/machine/runtime.rs @@ -279,7 +279,7 @@ impl DefaultFunction { if args.is_empty() { Ok(()) } else { - let first = args[0].as_ref().clone(); + let first = args[0].as_ref(); arg.expect_type(Type::List(Rc::new(first.try_into()?))) } @@ -675,9 +675,9 @@ impl DefaultFunction { DefaultFunction::AppendString => match (args[0].as_ref(), args[1].as_ref()) { (Value::Con(string1), Value::Con(string2)) => { match (string1.as_ref(), string2.as_ref()) { - (Constant::String(arg1), Constant::String(arg2)) => Ok(Value::Con( - Constant::String(format!("{arg1}{arg2}")).into(), - )), + (Constant::String(arg1), Constant::String(arg2)) => { + Ok(Value::Con(Constant::String(format!("{arg1}{arg2}")).into())) + } _ => unreachable!(), } } @@ -749,18 +749,14 @@ impl DefaultFunction { }, DefaultFunction::FstPair => match args[0].as_ref() { Value::Con(pair) => match pair.as_ref() { - Constant::ProtoPair(_, _, first, _) => { - Ok(Value::Con(first.as_ref().clone().into())) - } + Constant::ProtoPair(_, _, first, _) => Ok(Value::Con(first.clone())), _ => unreachable!(), }, _ => unreachable!(), }, DefaultFunction::SndPair => match args[0].as_ref() { Value::Con(pair) => match pair.as_ref() { - Constant::ProtoPair(_, _, _, second) => { - Ok(Value::Con(second.as_ref().clone().into())) - } + Constant::ProtoPair(_, _, _, second) => Ok(Value::Con(second.clone())), _ => unreachable!(), }, _ => unreachable!(),