[Haskell-cafe] Efficient Factoring Out of Constants

Eugene Kirpichov ekirpichov at gmail.com
Sat Jan 17 15:45:18 EST 2009


A very short time ago Simon Marlow (if I recall correctly) commented
on this topic: he told that this transformation usually improves
efficiency pretty much, but sometimes it leads to some problems and it
shouldn't be done by the compiler automatically. Search the recent
messages for 'map' and one of them will have an answer :)

2009/1/17 Phil <pbeadling at mail2web.com>:
> On 17/01/2009 16:55, "Luke Palmer" <lrpalmer at gmail.com> wrote:
> Wow.  I strongly suggest you forget about efficiency completely and become a
> proficient high-level haskeller, and then dive back in.  Laziness changes
> many runtime properties, and renders your old ways of thinking about
> efficiency almost useless.
>
> If you are interested, though, you can use the ghc-core tool on hackage to
> look at the core (lowish-level intermediate language) and even the generated
> assembly for minimal cases.  It's dense, but interesting if you have the
> time to study it.
>
> Others will know more about this specific speculation than I.
>
> Luke
>
> [Phil]
> Heh heh – totally accept what your saying, I am obsessing over details here.
>  I've ran some empirical tests to get some crude insight (just using the
> linux's time program),  I expected the differences to be small for the
> amount of data I was passing around, but I was surprised.  I modified the
> code ever so slightly to use data structures to pass around the data.  Thus
> got rid of the getSum and getStateInfo functions.  In the first unfactored
> example everything was passed around in one structure.  In the second
> example I had two structures; one for constant data and one for state date.
>  Thus doesn't really change the problem it just means that in the unfactored
> example mcSimulate takes one parameter (holding data constant and variable
> state data) and in the factored example it takes two parameters.  The rest
> of the code remained more-or-less identical to my original post.
>
> Running both programs at the same time on an otherwise unloaded CPU gave a
> very consistent result over numerous trials that for 1 million calls to
> mcSimulate, the unfactored example took approx 1m44s and the factored
> example took 1m38 – which is a fairly significant difference.
>
> So whilst I can't offer any exact explanation, it is clear that factoring
> out even a few parameters taking up little memory produces a significant
> performance increase.  This would suggest that my 'JMP' analysis is not
> right and that the compiler is able to optimize the factored version better.
>
> If anyone else fancies offering up their 2 cents on what the compiler is
> doing, I'd still be interested, but the empirical evidence alone is enough
> for me to be swayed to factoring all static parameters in an iteration out
> of the iteration and into a wrapper.
>
> Phil.
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>


More information about the Haskell-Cafe mailing list