[Haskell-cafe] Stack overflow

Krzysztof Skrzętnicki gtener at gmail.com
Wed May 27 12:32:29 EDT 2009

Hello Cafe

I'm currently writing an app with heavy use of message passing. To see
which messages takes most of the bandwidth I wrote the following code:

data Counter = CNT !Int !Int !Int !Int

cntMsg (CNT a b c d) (MoveOther _ _) = (CNT a+1 b c d)
cntMsg (CNT a b c d) (MoveSelf _) = (CNT a b+1 c d)
cntMsg (CNT a b c d) (NilMsg) = (CNT a b c+1 d)
cntMsg (CNT a b c d) (RoundEnd) = (CNT a b c d+1)

emptyCnt = CNT 0 0 0 0
showCnt (CNT a b c d) = printf "CNT MoveOther=%d MoveSelf=%d NilMsg=%d
RoundEnd=%d" a b c d

The code for modifying the counter:
(\ msg -> atomicModifyIORef ioref (\ cnt -> (cntMsg cnt msg,())))

Running it without increased stack blows it. With 200M stack I get
after a second or so:

CNT MoveOther=2125764 MoveSelf=0 NilMsg=0 RoundEnd=2916

The datatype itself is strict. So where is the thunk actually accumulating?

Best regards

Christopher Skrzętnicki

More information about the Haskell-Cafe mailing list