From 94f383762c7fd9a6fff5a15071239f5b3f2293ee Mon Sep 17 00:00:00 2001 From: microproofs Date: Thu, 7 Mar 2024 20:25:20 -0500 Subject: [PATCH] step up the optimizations a bit more by inlining a small function --- crates/uplc/src/optimize.rs | 1 + crates/uplc/src/optimize/shrinker.rs | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/crates/uplc/src/optimize.rs b/crates/uplc/src/optimize.rs index c54ad697..94608ba1 100644 --- a/crates/uplc/src/optimize.rs +++ b/crates/uplc/src/optimize.rs @@ -5,6 +5,7 @@ pub mod shrinker; pub fn aiken_optimize_and_intern(program: Program) -> Program { program + .inline_constr_ops() .builtin_force_reducer() .lambda_reducer() .inline_reducer() diff --git a/crates/uplc/src/optimize/shrinker.rs b/crates/uplc/src/optimize/shrinker.rs index 0eca13e0..721a6558 100644 --- a/crates/uplc/src/optimize/shrinker.rs +++ b/crates/uplc/src/optimize/shrinker.rs @@ -7,6 +7,7 @@ use pallas::ledger::primitives::babbage::{BigInt, PlutusData}; use crate::{ ast::{Constant, Data, Name, NamedDeBruijn, Program, Term, Type}, + builder::{CONSTR_FIELDS_EXPOSER, CONSTR_INDEX_EXPOSER}, builtins::DefaultFunction, }; @@ -1089,7 +1090,7 @@ impl Program { pub fn force_delay_reducer(self) -> Self { self.traverse_uplc_with(&mut |_id, term, _arg_stack, _scope| { if let Term::Force(f) = term { - let f = Rc::make_mut(f); + let f = f.as_ref(); if let Term::Delay(body) = f { *term = body.as_ref().clone(); @@ -1108,6 +1109,26 @@ impl Program { }) } + pub fn inline_constr_ops(self) -> Self { + self.traverse_uplc_with(&mut |_, term, _, _| { + if let Term::Apply { function, argument } = term { + if let Term::Var(name) = function.as_ref() { + if name.text == CONSTR_FIELDS_EXPOSER { + *term = Term::snd_pair().apply(Term::Apply { + function: Term::unconstr_data().into(), + argument: argument.clone(), + }) + } else if name.text == CONSTR_INDEX_EXPOSER { + *term = Term::fst_pair().apply(Term::Apply { + function: Term::unconstr_data().into(), + argument: argument.clone(), + }) + } + } + } + }) + } + pub fn cast_data_reducer(self) -> Self { let mut applied_ids = vec![];