<div dir="ltr">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.<div><br></div><div>Here is an example that measures <b>\n -> n+1</b>:</div><div><br></div><div><font face="courier new, monospace">    import Weigh</font></div><div><font face="courier new, monospace">    </font></div><div><font face="courier new, monospace">    main :: IO ()</font></div><div><font face="courier new, monospace">    main = mainWith (func "+1" plusOne 5)</font></div><div><br><font face="courier new, monospace">    plusOne :: Int -> Int</font><br><font face="courier new, monospace">    plusOne = \n -> n + 1</font><br><br><font face="arial, sans-serif">which prints</font></div><div><font face="arial, sans-serif"><br></font></div><div><div><font face="courier new, monospace">    Case  Bytes  GCs  Check</font></div><div><font face="courier new, monospace">    +1       16    0  OK   </font></div><div style="font-family: arial, sans-serif;"><br></div></div><div style="font-family: arial, sans-serif;"><font size="2">when run (64 bit machine; allocating 1 Int = 16 bytes).</font></div><div style="font-family: arial, sans-serif;"><font size="2"><br></font></div><div><font size="2"><font face="arial, sans-serif">So my question is, what about the thunk allocated by <b>(+)</b></font><font face="arial, sans-serif">? My rough understanding of the heap is this:</font><br><br><font face="courier new, monospace">Code          Heap</font></font></div><div><font size="2"><font face="courier new, monospace">----          ----<br>               1        1         1         1    1</font></font></div><div><font size="2"><font face="courier new, monospace">plusOne 1     +-------+----------+----+    +----+---+</font></font></div><div><font size="2"><font face="courier new, monospace">              | THUNK | reserved |  --|--> | I# | 5 |<br></font></font></div><div><font size="2"><font face="courier new, monospace">              +-------+----------+----+    +----+---+</font></font></div><div><font size="2"><font face="courier new, monospace"><br></font></font></div><div><font size="2"><font face="courier new, monospace">               1       1          1         1    1</font></font></div><div><font size="2"><font face="courier new, monospace">deepseq it    +-------+----------+----+    +----+---+<br>              | IND   |          |  --|--> | I# | 5 |<br>              +-------+-----|----+----+    +----+---+</font></font></div><div><font size="2"><font face="courier new, monospace">                            |               1    1</font></font></div><div><font size="2"><font face="courier new, monospace">                            `------------> +----+---+</font></font></div><div><font size="2"><font face="courier new, monospace">                                           | I# | 6 |</font></font></div><div><font size="2"><font face="courier new, monospace">                                           +----+---+</font></font></div><div><font size="2"><font face="courier new, monospace"><br></font></font></div><div><font size="2"><font face="courier new, monospace">               1     1</font></font></div><div><font size="2"><font face="courier new, monospace">GC            +----+---+<br>              | I# | 6 | </font></font></div><div><font size="2"><font face="courier new, monospace">              +----+---+</font></font></div><div><font size="2"><font face="courier new, monospace"><br></font></font></div><div><font size="2" face="arial, sans-serif">So haven't we allocated 5 bytes, not two? Three for the GC'd thunk, and two for the new Int?</font></div><div><font size="2" face="arial, sans-serif"><br></font></div><div><font size="2" face="arial, sans-serif">Any clarification or pointers (heh) to some GHC reading would be greatly appreciated!</font></div><div><font size="2" face="arial, sans-serif"><br></font></div><div><font size="2" face="arial, sans-serif">Thanks,</font></div><div><font size="2" face="arial, sans-serif">Mitchell</font></div></div>