[Haskell-cafe] space leaks and optimizations

David Leimbach leimy2k at gmail.com
Fri Jan 8 10:17:58 EST 2010

> 2) While each step is predictable, the overall behavior of a lazy
> program can be rather surprising. So one must be very careful. GHC
> provides two ways to control the evaluation order, seq and bang
> patterns, but I am not sure which of these (if any) is the right tool.
> Consider the following example (from the Real World Haskell book):
> mean :: [Double] -> Double
> mean xs = sum / fromIntegral num where
>    (num,sum) = foldl' f (0,0) xs :: (Int, Double)
>    f (n,s) x = (n+1, s+x)
> Although it uses foldl', there is a space leak because Haskell tuples
> are not strict. There are two possible fixes:
>    f (!n,!s) x = (n+1, s+x)
> or
>    f (n,s) x = let n1=n+1; s1=s+x in n1 `seq` s1 `seq` (n1,s1)
> The first one is short, but less natural than the second one, where the
> offending thunks are suppressed on the spot. Unfortunately, the syntax
> is awkward; it would be nice to write something like
>    f (n,s) x = (!n+1, !n+1)
> Well, I am becoming too grumpy, perhaps the bang patterns are fine. More
> important question: are there established practices to *avoid* space
> leaks rather than fixing them afterwards?
I believe the expectation is to learn to not be surprised in the areas where
lazy or non-strict evaluation can be overused, and to learn all the
advantages of non-strict evaluation vs strict, and the power it gives, such
that an imperative programmer doesn't feel surprised or angry when things go

I blogged about writing a long running service in Haskell that ran into
problems with the lazy State monad, and lazy Data.Map, and I discussed how I
had to force evaluations of everything to get the program under control.
 This wasn't for a hobby, this was for a production system.  I believe I've
a much better handle on strict vs non-strict than when I started the
project, but I felt pretty lost for a while  in the process of doing it.

I was even using the Maybe monad with it's MonadPlus implementation to avoid
using case statements around deconstruction, which I'm sure exacerbated some
of my problem.  However, since Haskell will evaluate the outer-most stuff
first, the trick seems to be to find the point at which you *really* need
the values computed, then tell Haskell to get on with it.  You kind of have
to have an explicit sequence point where all things need to be resolved, and
you have to be able to see those in your code.  Sometimes you can get away
with only doing small pieces at a time.

I had about the worst situation I've ever seen for data growth in my code.
 I had a pile of non-strict expressions, that were all dependencies for the
next, running forever, and never getting evaluated except at asynchronous
and user-controlled moments.  If these expressions had been evaluated
strictly, they would have taken up constant space, but since they were all
thunks, I got linear data growth over time, until I blew up.

Some advice I've gotten since then was to think about using case for
strictness rather than explicitly using seq.  Turns out case's pattern
matching is pretty strict, and that you can often get by with that.  I
haven't spent a lot of time with core output, but my understanding is that
it's all let and case.

> 3) The standard library was not designed with space efficiency in mind;
> for example, there is sum but no sum'.

Actually I think that the standard library was designed to be consistent
with the way the language is documented to behave.  That is to say that it's
non-strict by default everywhere it's possible to be so.
 Control.Monad.State selects Control.Monad.State.Lazy by default instead of
Control.Monad.State.Strict, but both exist.

Yes, in some cases there's no strict equivalent provided, but is writing a
strict sum really a big problem?  I think there's stricter folds included
because they're not quite as trivial, but once you have a strict fold isn't
strict sum pretty easy?  I suppose the type of the contained element in the
list could make a big difference in whether the strict fold is strict
enough, but where do you stop providing strict versions of functions for
people?  It seems a line must be drawn somewhere, and the right solution is
to properly educate Haskell programmer about both the power and drawbacks of
non-strict evaluation, and when it's really necessary to turn things off.
 Giving examples is fine, but one must learn to see the patterns where there
is a problem that could brew.

Real World Haskell teaches us about the profiling tools that helped me
uncover my problems.

> Best regards,
> Alexei
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100108/b5218dfd/attachment.html

More information about the Haskell-Cafe mailing list