[Haskell-cafe] Performance Issue

Thomas Schilling nominolo at googlemail.com
Sat May 22 14:42:43 EDT 2010


On 22 May 2010 16:06, Daniel Fischer <daniel.is.fischer at web.de> wrote:
> On Saturday 22 May 2010 16:48:27, Daniel Fischer wrote:
>> The boxing is due to the use of (^).
>> If you write x*x instead of x^2, it can use the primop *## and needn't
>> box it.
>> As a side effect, the original time leak probably wouldn't have occured
>> with x*x instead of x^2 because one would've made it
>>    let x = newton a (n-1) in (x*x +a) / (2*x)
>> instead of writing out newton a (n-1) thrice anyway, wouldn't one?
>>
>
> Even if. With
>
> newton :: Double -> Int -> Double
> newton a 0 = a
> newton a n =
>    (((newton a (n-1)) * (newton a (n-1)) ) + a)/(2*(newton a (n-1)))
>
> (and optimisations of course), GHC does share newton a (n-1).
>
> Lesson: Writing x^2 is a baad thing.

Interesting.  Clearly GHC needs a better partial evaluator! :)  (^) is
not inlined because it's recursive (or rather it's worker is) and
there also is no SPECIALISE pragma for Double -> Integer -> Double.
Yes, it's Integer, not Int, because the literal "2" defaults to
Integer.

It doesn't seem to be possible to add SPECIALISE pragmas for non-local
functions.  If I copy over the definition of (^) no pragma is needed.
GHC creates an worker for Double# -> Integer -> Double# and that seems
to be sufficient to make CSE work.



-- 
Push the envelope.  Watch it bend.


More information about the Haskell-Cafe mailing list