[Haskell-cafe] Measuring allocations with 'weigh' - what about thunks?

Mitchell Rosen mitchellwrosen at gmail.com
Wed Aug 3 15:42:29 UTC 2016


weigh (https://www.fpcomplete.com/blog/2016/05/weigh-package) is a library 
measures how much allocation evaluating a function causes. It's basically a 
nice interface to GHC.Stats.getGCStats.

Here is an example that measures *\n -> n+1*:

    import Weigh
    
    main :: IO ()
    main = mainWith (func "+1" plusOne 5)

    plusOne :: Int -> Int
    plusOne = \n -> n + 1

which prints

    Case  Bytes  GCs  Check
    +1       16    0  OK   

when run (64 bit machine; allocating 1 Int = 16 bytes).

So my question is, what about the thunk allocated by *(+)*? My rough 
understanding of the heap is this:

Code          Heap
----          ----
               1        1         1         1    1
plusOne 1     +-------+----------+----+    +----+---+
              | THUNK | reserved |  --|--> | I# | 5 |
              +-------+----------+----+    +----+---+

               1       1          1         1    1
deepseq it    +-------+----------+----+    +----+---+
              | IND   |          |  --|--> | I# | 5 |
              +-------+-----|----+----+    +----+---+
                            |               1    1
                            `------------> +----+---+
                                           | I# | 6 |
                                           +----+---+

               1     1
GC            +----+---+
              | I# | 6 | 
              +----+---+

So haven't we allocated 5 bytes, not two? Three for the GC'd thunk, and two 
for the new Int?

Any clarification or pointers (heh) to some GHC reading would be greatly 
appreciated!

Thanks,
Mitchell
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20160803/db412cc1/attachment.html>


More information about the Haskell-Cafe mailing list