<div dir="ltr">I can only answer the questions to the best of my understanding, here goes:<div><br></div><div>On 9 October 2015 at 17:00, Rob Stewart <span dir="ltr"><<a href="mailto:robstewart57@gmail.com" target="_blank">robstewart57@gmail.com</a>></span> wrote:<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Here are my questions: what exactly is the call overhead of a<br>non-inlned Haskell function? What additional runtime work needs to<br>happen when calling to a non-inlined function? Specifically what other<br>GHC optimisations are possible only if function inlining happens<br>first? Can anyone provide a simple example where 1) a function is not<br>inlined, versus 2) when that function is inlined, which demonstrates<br>two things: 1) a GHC report of the additional optimisations that were<br>applied because of inlining and 2) shorter program runtime as a result<br>of inlining.</blockquote></div><div><br></div><div>An example of optimizations becoming available after inlining:</div><div><br></div><font face="monospace, monospace">listAdd1 = map (+1)<br>listAdd2 = map (+2)<br><br>listAdd3 = listAdd2 . listAdd1<br><br>-- From the RHS of listAdd3<br> listAdd2 . listAdd1<br>== map (+2) . map (+1)<br>== map ((+2) . (+1)) -- List fusion. Also works for types other than lists (see the talk [1] for more)<br>== map (\x -> (+2) ((+1) x)) -- Inline the composition dot-operator (.)<br>== map (\x -> (+2) (x + 1))<br>== map (\x -> x + 3)<br>== map (+3)<br><br>-- The order and the final result might not be the same, but it will be operationally equivalent.<br>-- This prevents having to iterate over the whole list twice.</font><div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 9 October 2015 at 17:00, Rob Stewart <span dir="ltr"><<a href="mailto:robstewart57@gmail.com" target="_blank">robstewart57@gmail.com</a>></span> wrote:<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
As Don Stewart nicely explains on SO<br>
<a href="http://stackoverflow.com/a/5889784/1526266" rel="noreferrer" target="_blank">http://stackoverflow.com/a/5889784/1526266</a> , we should always use<br>
newtype when we have only a single constructor which has a single<br>
field. His example is this:<br>
<br>
data Book = Book Int Int<br>
newtype Book = Book (Int, Int)<br>
<br>
With newtype, the `Book` construct is guaranteed to be erased at<br>
compile time, to have the same representation as `(Int,Int)`. How to I<br>
measure the memory space costs of having `Book Int Int` hanging around<br>
at runtime, as opposed to just `(Int,Int)`? How many bytes does it<br>
cost to store `Book 4 5`? How many bytes does it cost to store `(4,5)`<br>
? Can I ask GHC to tell me how many bytes the storage of each object<br>
would require? Or more generally, would looking at GHC Core for all my<br>
data structures inform me of the memory space costs for each of them?<br>
</blockquote></div></div><div class="gmail_extra"><br></div><div class="gmail_extra"><div class="gmail_extra">I don't think it's possible to look into GHC's memory. Simon Marlow's book [2] explains laziness very well, but even he (he wrote the GHC runtime) says that there is no way to see the actual structure of memory usage.</div><div class="gmail_extra"><br></div><div class="gmail_extra">[1]: <a href="http://www.techcast.com/events/bigtechday6/odeon-1130/?q=odeon-1130">http://www.techcast.com/events/bigtechday6/odeon-1130/?q=odeon-1130</a></div><div class="gmail_extra">[2]: <span style="color:rgb(17,17,17);font-family:Arial,sans-serif;font-size:13px;line-height:19px"><a href="http://amzn.com/1449335942">http://amzn.com/1449335942</a></span></div><div class="gmail_extra"><br></div>-- <br><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div>Regards</div><div dir="ltr"><div><br></div><div>Sumit Sahrawat</div></div></div></div></div></div></div>
</div></div>