[Haskell-cafe] How to increment an Int in Haskell (stack overflow issue)

Don Stewart dons at galois.com
Wed Mar 25 15:59:33 EDT 2009


bauertim:
> I have a program that is currently blowing out the stack,
>    Stack space overflow: current size 8388608 bytes.
>    Use `+RTS -Ksize' to increase it.
> I am pretty sure I get to the end of the computation that
> increments various statistic counters (lazily?) and only
> when I go to print them out at the end, do things fail.
>
> I have state monad transformer (StateT) that is keeping the counters
> among other things. The counters are stored in a pair within a larger
> data structure.
>
> > data AState s a = AS { ...
> >                       asStats :: (Int,Int)
> >                       ...
> >                     }

I would always, always write this as:

    data AState s a = AS { ...
                         , asStatsX, asStatsY :: !Int
                         , ...  }

and compile with:

    ghc -O2 -funbox-strict-fields

Simple heuristic: unless otherwise specified, atomic types should always
be strict fields.

-- Don


More information about the Haskell-Cafe mailing list