[Haskell-cafe] Ridiculously slow FFI, or cairo binding?

Jean-Marie Gaillourdet jmg at gaillourdet.net
Wed Nov 2 14:24:36 CET 2011

Hi Eugene,

did you try using the SPECIALIZE pragma? It is part of the Haskell 98 and Haskell 2010 specifications. 

On 02.11.2011, at 12:14, Eugene Kirpichov wrote:

> Yay!!!
> I made a small change in Types.chs and got my original cairo-binding-based program to be just as blazing fast. The only problem I have with this is that I used multiparameter type classes.
> Dear gtk2hs team! Is it possible to incorporate my changes? I'm pretty sure people will be happy by an order-of-magnitude speedup. Probably the stuff could be wrapped in #define's for those who aren't using GHC and can't use multiparameter type classes?
> I am pretty sure I could have done the same with rewrite rules, but I tried for a while and to no avail.
> FAILED SOLUTION: rewrite rules
> cFloatConv :: (RealFloat a, RealFloat b) => a -> b
> cFloatConv  = realToFrac
> {-# NOINLINE cFloatConv #-}
> {-# RULES "cFloatConv/float2Double" cFloatConv = float2Double #-}
> {-# RULES "cFloatConv/double2Float" cFloatConv = double2Float #-}
> {-# RULES "cFloatConv/self"         cFloatConv = id           #-}

See [1] in GHC User Guide.

cFloatConv :: (RealFloat a, RealFloat b) => a -> b
cFloatConv = realToFrac -- or try fromRational . toRational

{-# SPECIALIZE cFloatConv :: Float -> Double #-}
{-# SPECIALIZE cFloatConv :: Double -> Float #-}

I did not try to compile or even benchmark this code. But I think it might help in your case.


[1]: http://www.haskell.org/ghc/docs/latest/html/users_guide/pragmas.html#specialize-pragma

More information about the Haskell-Cafe mailing list