[Haskell-cafe] following up on space leak

Uwe Hollerbach uhollerbach at gmail.com
Sat Jul 4 03:39:35 EDT 2009

Good evening, all, following up on my question regarding space leaks,
I seem to have stumbled across something very promising. I said I was
using this tiny function "lastOrNil" to get the last value in a list,
or the empty (scheme) list if the haskell list was empty. The uses of
it were all of the form

    lastOrNil (mapM <something> <some list>)

so I wrote a different function mapML to do this directly:

> mapML fn lst = mapMLA (List []) fn lst
>   where mapMLA r _ [] = return r
>         mapMLA ro fn (x:xs) =
>            do rn <- fn x
>               mapMLA rn fn xs

This isn't an accumulator, it's a replacer (or, if you like, the
"accumulation" is "drop the old one on the floor"), it starts out with
the scheme empty list that I want as the default, and it never even
builds the list which it'll just dump an instant later. Shazam! Memory
usage dropped by roughly an order of magnitude in my little Collatz
benchmark, and incidentally runtime improved by 25% or so as well. The
horror! :-)

Having tasted blood, I will of course be continuing to benchmark...
but not tonight.


More information about the Haskell-Cafe mailing list