equivalent of EXPLAIN PLAN with GHC?

Thomas DuBuisson thomas.dubuisson at gmail.com
Sun Aug 2 03:12:03 EDT 2009


Josh,

In general you'll find the haskell-cafe (haskell-cafe at haskell.org) to
be a more lively place for this type of discussion, but since we're
here I might as well mention that memory use of a Haskell function is
one of the hardest things to gain an understanding about.

> main = print (show (sum [x | x <- [3..999], x `mod` 3 == 0 || x `mod` 5 == 0]))
> I want
> to know if the list was ever actually constructed in memory.

For such a simple program I suggest you test with lists of various lengths:

---------------- main with n == 999 -------------------
[tommd at Mavlo Test]$ ghc --make -O2 opt.hs
[1 of 1] Compiling Main             ( opt.hs, opt.o )
Linking opt ...
[tommd at Mavlo Test]$ ./opt +RTS -sstderr
./opt +RTS -sstderr
"233168"
          94,604 bytes allocated in the heap
             700 bytes copied during GC
          17,144 bytes maximum residency (1 sample(s))
          23,816 bytes maximum slop
               1 MB total memory in use (0 MB lost due to fragmentation)

  Generation 0:     0 collections,     0 parallel,  0.00s,  0.00s elapsed
  Generation 1:     1 collections,     0 parallel,  0.00s,  0.00s elapsed

  INIT  time    0.00s  (  0.00s elapsed)
  MUT   time    0.00s  (  0.00s elapsed)
  GC    time    0.00s  (  0.00s elapsed)
  EXIT  time    0.00s  (  0.00s elapsed)
  Total time    0.00s  (  0.00s elapsed)

  %GC time       0.0%  (14.0% elapsed)

  Alloc rate    47,325,662 bytes per MUT second

  Productivity   0.0% of total user, 0.0% of total elapsed
----------------------------------------------------------------


---------------- main with n == 10000000 -------------------
[tommd at Mavlo Test]$ ./opt +RTS -sstderr
./opt +RTS -sstderr
"23333341666668"
     906,451,272 bytes allocated in the heap
         128,992 bytes copied during GC
          18,104 bytes maximum residency (1 sample(s))
          18,760 bytes maximum slop
               1 MB total memory in use (0 MB lost due to fragmentation)

  Generation 0:  1742 collections,     0 parallel,  0.01s,  0.01s elapsed
  Generation 1:     1 collections,     0 parallel,  0.00s,  0.00s elapsed

  INIT  time    0.00s  (  0.00s elapsed)
  MUT   time    0.99s  (  1.02s elapsed)
  GC    time    0.01s  (  0.01s elapsed)
  EXIT  time    0.00s  (  0.00s elapsed)
  Total time    1.00s  (  1.03s elapsed)

  %GC time       0.6%  (1.0% elapsed)

  Alloc rate    912,980,911 bytes per MUT second

  Productivity  99.4% of total user, 96.7% of total elapsed
-----------------------------------------------------------

So obviously there was a lot more data moving around but no
significant difference in maximum memory use - a good sign!

> The C it produced
> with '-fvia-C -C' was totally unreadable

Deprecating the via-C compilation path has been discussed - hopefully
no one ever suggested it as a useful path for human inspection.

Thomas


More information about the Glasgow-haskell-users mailing list