[Haskell-cafe] Space Leak Help

Dominic Steinitz dominic.steinitz at blueyonder.co.uk
Sun Feb 4 04:45:12 EST 2007


On Sunday 04 February 2007 08:28, Stefan O'Rear wrote:
> On Sun, Feb 04, 2007 at 08:20:23AM +0000, Dominic Steinitz wrote:
> > Someone suggested
> >
> > pad :: Num a => [a] -> [a]
> > pad = pad' 0
> >   where pad' !l [] = [0x80] ++ ps ++ lb
> >           where pl = (64-(l+9)) `mod` 64
> >                 ps = replicate pl 0x00
> >                 lb = i2osp 8 (8*l)
> >         pad' !l (x:xs) = x : pad' (l+1) xs
> >
> > but that didn't compile
> >
> > *Main> :r
> > [2 of 2] Compiling Main             ( allInOne.hs, interpreted )
> >
> > allInOne.hs:83:14: Parse error in pattern
> > Failed, modules loaded: Codec.Utils.
> >
> > Dominic.
>
> The '!' is a GHC extension, enabled using the flag '-fbang-patterns'.
>
The test program runs to completion but still has a space leak consuming over 
25m.

> Equivalently, you can use Haskell 98's "seq" :
>
> pad :: Num a => [a] -> [a]
> pad = pad' 0
>   where pad' l [] | l `seq` False = undefined
>         pad' l [] = [0x80] ++ ps ++ lb
>           where pl = (64-(l+9)) `mod` 64
>                 ps = replicate pl 0x00
>                 lb = i2osp 8 (8*l)
>         pad' l (x:xs) = x : pad' (l+1) xs
>
> The first alternative never succeeds, but to see that the compiler
> must force the evaluation of 'l'.

dom at heisenberg:~/sha1> ./allInOne 1000001 +RTS -hc -RTS
[2845392438,1191608682,3124634993,2018558572,2630932637]
[2224569924,473682542,3131984545,4182845925,3846598897]
Stack space overflow: current size 8388608 bytes.
Use `+RTS -Ksize' to increase it.

Dominic.

PS I appreciate all the help I'm getting.



More information about the Haskell-Cafe mailing list