From ba0c760e9dc30a4babf9b76860153aa05d16e9eb Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 30 Apr 2019 18:02:08 +0200 Subject: Mutate thunk contents directly if sole owner --- dhall/src/normalize.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'dhall/src') diff --git a/dhall/src/normalize.rs b/dhall/src/normalize.rs index 7fef48e..c89e147 100644 --- a/dhall/src/normalize.rs +++ b/dhall/src/normalize.rs @@ -1032,9 +1032,15 @@ mod thunk { self.clone() } + // Normalizes contents to normal form; faster than `normalize_nf` if + // no one else shares this thunk pub(crate) fn normalize_mut(&mut self) { - // TODO: optimize if sole owner - self.normalize_nf(); + match Rc::get_mut(&mut self.0) { + // Mutate directly if sole owner + Some(refcell) => RefCell::get_mut(refcell).normalize_nf(), + // Otherwise mutate through the refcell + None => self.0.borrow_mut().normalize_nf(), + } } // WARNING: avoid normalizing any thunk while holding on to this ref -- cgit v1.2.3