[Haskell-cafe] Efficient Factoring Out of Constants

Phil pbeadling at mail2web.com
Sat Jan 17 15:30:19 EST 2009

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.


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.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090117/75543ea7/attachment.htm

More information about the Haskell-Cafe mailing list