GHC: Ticket #2251: Missing conversion rules for realToFrac causing slowdowns in Int->Double conversions
http://ghc.haskell.org/trac/ghc/ticket/2251
<p>
GHC.Real and GHC.Float currently have:
</p>
<pre class="wiki">-- | general coercion from integral types
fromIntegral :: (Integral a, Num b) => a -> b
fromIntegral = fromInteger . toInteger
{-# RULES
"fromIntegral/Int->Int" fromIntegral = id :: Int -> Int
#-}
-- | general coercion to fractional types
realToFrac :: (Real a, Fractional b) => a -> b
realToFrac = fromRational . toRational
{-# RULES
"realToFrac/Int->Int" realToFrac = id :: Int -> Int
#-}
</pre><p>
But these avoid some primops that can make these conversions
more efficient.
</p>
<pre class="wiki">
{-# RULES
"realToFrac/Int->Double" realToFrac = i2d :: Int -> Double
#-}
int2Double :: Int -> Double
int2Double (I# x) = D# (int2Double# x)
</pre><p>
A rule to use the in2Double# primop over realToFrac directly, improves
the running time of this program:
</p>
<pre class="wiki">import Data.Array.Parallel.Unlifted
main = do
let c = replicateU n (2::Double)
a = mapU realToFrac (enumFromToU 0 (n-1) ) :: UArr Double
print (sumU (zipWithU (*) c a))
</pre><p>
From 113 seconds, to 0.194s! A massive speedup.
</p>
<p>
We should fill out the rules here with at least those for which GHC
has primitives:
</p>
<pre class="wiki">
{-# RULES
"realToFrac/Int->Double" realToFrac = int2Double :: Int -> Double
"realToFrac/Int->Float" realToFrac = int2Float :: Int -> Float
#-}
</pre><p>
to the realToFrac and fromIntegral rules in GHC.Float
</p>
en-usGHChttp://ghc.haskell.org/trac/ghc/chrome/site/ghc_logo.png
http://ghc.haskell.org/trac/ghc/ticket/2251
Trac 1.2.2.dev0simonpjWed, 30 Apr 2008 09:09:52 GMTdifficulty set
http://ghc.haskell.org/trac/ghc/ticket/2251#comment:1
http://ghc.haskell.org/trac/ghc/ticket/2251#comment:1
<ul>
<li><strong>difficulty</strong>
set to <em>Unknown</em>
</li>
</ul>
<p>
Thanks Don. Would you care to offer a patch? (Don't forget to include comments that explain how big the performance improvement is; I find that giving a little program in that comment (as you do above) is a great way to capture the idea.)
</p>
<p>
I gather that when you say "...at least those for which GHC has primitives" you mean that there are some important ones missing. If so, propose away.
</p>
<p>
Simon
</p>
TicketdonsWed, 30 Apr 2008 18:00:40 GMTstatus changed; resolution set
http://ghc.haskell.org/trac/ghc/ticket/2251#comment:2
http://ghc.haskell.org/trac/ghc/ticket/2251#comment:2
<ul>
<li><strong>status</strong>
changed from <em>new</em> to <em>closed</em>
</li>
<li><strong>resolution</strong>
set to <em>fixed</em>
</li>
</ul>
<p>
I've pushed a patch adding the rules for Int->Double and Int->Float.
</p>
<p>
See:
</p>
<blockquote>
<p>
<a class="ext-link" href="http://www.haskell.org/pipermail/cvs-libraries/2008-April/008493.html"><span class="icon"></span>http://www.haskell.org/pipermail/cvs-libraries/2008-April/008493.html</a>
</p>
</blockquote>
TicketsimonpjWed, 30 Apr 2008 21:45:28 GMT
http://ghc.haskell.org/trac/ghc/ticket/2251#comment:3
http://ghc.haskell.org/trac/ghc/ticket/2251#comment:3
<p>
Thanks! I've found that comments in source files are more often read than those in patch messages, so I've transferred some of your comments into the source file itself.
</p>
<p>
Simon
</p>
TicketsimonmarTue, 30 Sep 2008 15:40:36 GMTarchitecture changed
http://ghc.haskell.org/trac/ghc/ticket/2251#comment:4
http://ghc.haskell.org/trac/ghc/ticket/2251#comment:4
<ul>
<li><strong>architecture</strong>
changed from <em>Unknown</em> to <em>Unknown/Multiple</em>
</li>
</ul>
TicketsimonmarTue, 30 Sep 2008 15:51:47 GMTos changed
http://ghc.haskell.org/trac/ghc/ticket/2251#comment:5
http://ghc.haskell.org/trac/ghc/ticket/2251#comment:5
<ul>
<li><strong>os</strong>
changed from <em>Unknown</em> to <em>Unknown/Multiple</em>
</li>
</ul>
Ticket