From fd55b3e80f2955c5fe498b5f27ba24b54b9cb941 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sat, 20 Apr 2019 18:57:29 +0200 Subject: Refcount contexts to avoid clones --- dhall/src/normalize.rs | 80 +++++++++++++++++++++++++------------------------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/dhall/src/normalize.rs b/dhall/src/normalize.rs index 0381f38..488885a 100644 --- a/dhall/src/normalize.rs +++ b/dhall/src/normalize.rs @@ -1,5 +1,6 @@ #![allow(non_snake_case)] use std::collections::BTreeMap; +use std::rc::Rc; use dhall_core::context::Context; use dhall_core::*; @@ -26,7 +27,7 @@ impl<'a> Typed<'a> { } fn apply_builtin( - ctx: &NormalizationContext, + ctx: NormalizationContext, b: Builtin, args: &[WHNF], ) -> Option { @@ -72,7 +73,7 @@ fn apply_builtin( // TODO: avoid passing through Exprs let t = now_to_expr(t); EmptyListLit(Now::new( - ctx.clone(), + ctx, dhall::subexpr!({ index : Natural, value : t }), )) } @@ -200,17 +201,19 @@ enum EnvItem { } #[derive(Debug, Clone)] -struct NormalizationContext(Context); +struct NormalizationContext(Rc>); impl NormalizationContext { - fn new() -> NormalizationContext { - NormalizationContext(Context::new()) + fn new() -> Self { + NormalizationContext(Rc::new(Context::new())) } - fn insert(&self, x: &Label, e: WHNF) -> NormalizationContext { - NormalizationContext(self.0.insert(x.clone(), EnvItem::Expr(e))) + fn insert(&self, x: &Label, e: WHNF) -> Self { + NormalizationContext(Rc::new( + self.0.insert(x.clone(), EnvItem::Expr(e)), + )) } - fn skip(&self, x: &Label) -> NormalizationContext { - NormalizationContext( + fn skip(&self, x: &Label) -> Self { + NormalizationContext(Rc::new( self.0 .map(|l, e| { use EnvItem::*; @@ -221,7 +224,7 @@ impl NormalizationContext { } }) .insert(x.clone(), EnvItem::Skip(0)), - ) + )) } fn lookup(&self, var: &V