Skip to content

Commit

Permalink
Fix space leak in Shells (#381)
Browse files Browse the repository at this point in the history
Fixes #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
  • Loading branch information
Gabriella439 committed Mar 29, 2020
1 parent 2cfd49b commit 96b8e9d
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/Turtle/Shell.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE RankNTypes #-}
Expand Down Expand Up @@ -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`
Expand Down

0 comments on commit 96b8e9d

Please sign in to comment.