I want to generate a tree containing various integers and find their sum. Here is the code:
{-# LANGUAGE BangPatterns #-}
import Control.Applicative
import Control.Monad.Trans.State
data Tree a = Leaf a | Branch (Tree a) a (Tree a)
new = get <* modify' (+ 1)
tree :: Integer -> Tree Integer
tree n = evalState (go n) 0 where
go 0 = Leaf <$> new
go n = Branch <$> go (n - 1) <*> new <*> go (n - 1)
sumTree = go 0 where
go !a (Leaf n) = a + n
go !a (Branch l n r) = go (go (a + n) l) r
main = print $ sumTree (tree 20)
Compiled with -O2, it leads to
348,785,728 bytes allocated in the heap
147,227,228 bytes copied during GC
34,656,860 bytes maximum residency (13 sample(s))
35,468 bytes maximum slop
72 MB total memory in use (0 MB lost due to fragmentation)
Tot time (elapsed) Avg pause Max pause
Gen 0 565 colls, 0 par 0.764s 1.024s 0.0018s 0.0071s
Gen 1 13 colls, 0 par 0.936s 1.014s 0.0780s 0.3214s
INIT time 0.000s ( 0.001s elapsed)
MUT time 0.936s ( 0.764s elapsed)
GC time 1.700s ( 2.038s elapsed)
EXIT time 0.000s ( 0.002s elapsed)
Total time 2.636s ( 2.805s elapsed)
%GC time 64.5% (72.7% elapsed)
Alloc rate 372,631,936 bytes per MUT second
Productivity 35.5% of total user, 33.4% of total elapsed
Why am I getting this space leak? How to remove it?
source
share