From dffc250d4d1b3e26a473c6244295510aaa63188a Mon Sep 17 00:00:00 2001 From: Gabriel Gonzalez Date: Sat, 28 Mar 2020 16:32:55 -0700 Subject: [PATCH] Fix space leak in `Shell`s Fixes https://github.com/Gabriel439/Haskell-Turtle-Library/issues/380 The issue was that all of the `Shell` utilities were internally using `translate` which was inadvertently using a non-strict `Maybe` accumulator. Switching to a strict accumulator fixes the problem. This leak was introduced in version 1.5.0 --- src/Turtle/Shell.hs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Turtle/Shell.hs b/src/Turtle/Shell.hs index fd3bac6..c598330 100644 --- a/src/Turtle/Shell.hs +++ b/src/Turtle/Shell.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE BangPatterns #-} {-# LANGUAGE CPP #-} {-# LANGUAGE ExistentialQuantification #-} {-# LANGUAGE RankNTypes #-} @@ -101,21 +102,23 @@ data FoldShell a b = forall x . FoldShell (x -> a -> IO x) x (x -> IO b) -- | A @(Shell a)@ is a protected stream of @a@'s with side effects newtype Shell a = Shell { _foldShell:: forall r . FoldShell a r -> IO r } +data Maybe' a = Just' !a | Nothing' + translate :: FoldM IO a b -> FoldShell a b -translate (FoldM step begin done) = FoldShell step' Nothing done' +translate (FoldM step begin done) = FoldShell step' Nothing' done' where - step' Nothing a = do + step' Nothing' a = do x <- begin x' <- step x a - return (Just x') - step' (Just x) a = do + return $! Just' x' + step' (Just' x) a = do x' <- step x a - return (Just x') + return $! Just' x' - done' Nothing = do + done' Nothing' = do x <- begin done x - done' (Just x) = do + done' (Just' x) = do done x -- | Use a @`FoldM` `IO`@ to reduce the stream of @a@'s produced by a `Shell`