[Haskell-cafe] Performance Issue
Thomas Schilling
nominolo at googlemail.com
Sat May 22 09:00:25 EDT 2010
Actually, in this case it would be safe to do CSS. Because
a) the function is strict in both arguments so GHC creates a worker
which only uses unboxed types
b) this cannot cause any space leaks (it contains no pointers)
The generated Core does look pretty weird, though:
$wnewton =
\ (ww_s115 :: Double#) (ww1_s119 :: Int#) ->
case ww1_s119 of ds_Xr8 {
__DEFAULT ->
case ^_r11D
(case $wnewton ww_s115 (-# ds_Xr8 1)
of ww2_s11d { __DEFAULT ->
D# ww2_s11d -- box the result of $wnewton
})
lvl_r11B
of _ { D# x_avk -> -- unbox it again
case $wnewton ww_s115 (-# ds_Xr8 1)
of ww2_s11d { __DEFAULT ->
/##
(+## x_avk ww_s115) (*## 2.0 ww2_s11d)
}
};
0 -> ww_s115
}
On 22 May 2010 13:30, Ozgur Akgun <ozgurakgun at gmail.com> wrote:
> Richard,
>
> I found this very annoying when I first realised GHC doesn't do any CSE,
> given that the result of pure functions only depend on their parameters.
>
> Even though CSE usually sounds good, when you ask, they go and find obscure
> examples in which it causes great trouble :)
>
> I think, there should at least be a compiler flag or something similar to
> enforce CSE on some structures. But currently, as others pointed out, you
> need to bind and do your sub expression elimination yourself.
>
> Best,
>
> On 18 May 2010 17:30, Richard Warburton <richard.warburton at gmail.com> wrote:
>>
>> A colleague of mine pointed out that ghc wasn't performing as he
>> expected when optimising some code. I wonder if anyone could offer
>> any insight as to why its not noting this common subexpression:
>>
>> main = print $ newton 4 24
>>
>> newton a 0 = a
>> newton a n = ((newton a (n-1))^2 + a)/(2*(newton a (n-1)))
>>
>> Compiled with 'ghc -O3 --make perf.hs', results in:
>>
>> real 0m5.544s
>> user 0m5.492s
>> sys 0m0.008s
>>
>> However if we factor out the repeated call to the newton method:
>>
>> main = print $ newton2 4 24
>>
>> newton2 a 0 = a
>> newton2 a n = (x^2 + a)/(2*x)
>> where
>> x = newton2 a (n-1)
>>
>> real 0m0.004s
>> user 0m0.004s
>> sys 0m0.004s
>>
>> It looks to me like Referential transparency should make this a sound
>> optimisation to apply, but ghc isn't doing it even on -O3.
>>
>> regards,
>>
>> Richard
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
>
> --
> Ozgur Akgun
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
--
Push the envelope. Watch it bend.
More information about the Haskell-Cafe
mailing list