[Haskell-cafe] Stack overflow while programming imperatively

Aleksey Khudyakov alexey.skladnoy at gmail.com
Sun Mar 18 19:50:50 CET 2012


On 18.03.2012 22:32, Clark Gaebel wrote:
> Hey list.
>
> I was recently fixing a space leak by dropping down to imperative
> programming in a section of my code, when it started developing space
> leaks of its own.
>
> I found the problem though - it was my "for loop": http://hpaste.org/65514
>
> Can anyone provide suggestions on why that stack overflows? It seems
> ridiculously tail recursive. I tried to do it more haskell-like with
> http://hpaste.org/65517, but it was still spending 75% of its time in
> GC.
>
Excessive laziness could be cleverly hiding. modifyIORef doesn't modify
IORef's value but builds huge chain of thunks. When you try to evaluate
it you get stack overflow. Forcing value of IORef will fix this space leak.

You could use strict version of modifyIORef:

modifyIORef' x f = do
   a <- readIORef x
   writeIORef x $! f a



More information about the Haskell-Cafe mailing list