[Haskell-cafe] Time performance with inlining and space performance with newtype?

Sumit Sahrawat, Maths & Computing, IIT (BHU) sumit.sahrawat.apm13 at iitbhu.ac.in
Fri Oct 9 13:01:26 UTC 2015


I can only answer the questions to the best of my understanding, here goes:

On 9 October 2015 at 17:00, Rob Stewart <robstewart57 at gmail.com> wrote:
>
> Here are my questions: what exactly is the call overhead of a
> non-inlned Haskell function? What additional runtime work needs to
> happen when calling to a non-inlined function? Specifically what other
> GHC optimisations are possible only if function inlining happens
> first? Can anyone provide a simple example where 1) a function is not
> inlined, versus 2) when that function is inlined, which demonstrates
> two things: 1) a GHC report of the additional optimisations that were
> applied because of inlining and 2) shorter program runtime as a result
> of inlining.


An example of optimizations becoming available after inlining:

listAdd1 = map (+1)
listAdd2 = map (+2)

listAdd3 = listAdd2 . listAdd1

-- From the RHS of listAdd3
   listAdd2 . listAdd1
== map (+2) . map (+1)
== map ((+2) . (+1))         -- List fusion. Also works for types other
than lists (see the talk [1] for more)
== map (\x -> (+2) ((+1) x)) -- Inline the composition dot-operator (.)
== map (\x -> (+2) (x + 1))
== map (\x -> x + 3)
== map (+3)

-- The order and the final result might not be the same, but it will be
operationally equivalent.
-- This prevents having to iterate over the whole list twice.


On 9 October 2015 at 17:00, Rob Stewart <robstewart57 at gmail.com> wrote:
>
> As Don Stewart nicely explains on SO
> http://stackoverflow.com/a/5889784/1526266 , we should always use
> newtype when we have only a single constructor which has a single
> field. His example is this:
>
>     data Book = Book Int Int
>     newtype Book = Book (Int, Int)
>
> With newtype, the `Book` construct is guaranteed to be erased at
> compile time, to have the same representation as `(Int,Int)`. How to I
> measure the memory space costs of having `Book Int Int` hanging around
> at runtime, as opposed to just `(Int,Int)`? How many bytes does it
> cost to store `Book 4 5`? How many bytes does it cost to store `(4,5)`
> ? Can I ask GHC to tell me how many bytes the storage of each object
> would require? Or more generally, would looking at GHC Core for all my
> data structures inform me of the memory space costs for each of them?
>

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.

[1]: http://www.techcast.com/events/bigtechday6/odeon-1130/?q=odeon-1130
[2]: http://amzn.com/1449335942

-- 
Regards

Sumit Sahrawat
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20151009/abc7fdc7/attachment.html>


More information about the Haskell-Cafe mailing list