[Haskell-cafe] GHC magic optimization ?

Joachim Breitner mail at joachim-breitner.de
Fri Dec 4 05:43:28 EST 2009


Am Freitag, den 04.12.2009, 10:36 +0000 schrieb Neil Brown:
> But let's say you have:
> g x y = f x y * f x y
> Now the compiler (i.e. at compile-time) can do some magic.  It can
> spot the common expression and know the result of f x y must be the
> same both times, so it can convert to:
> g x y = let z = f x y in z * z
> Now, the Haskell run-time will evaluate f x y once, store the result
> in z, and use it twice.  That's how it can use commonalities in your
> code and avoid multiple evaluations of the same function call, which I
> *think* was your question. 

Note that although the compiler _could_ do this transformation, it does
not actually do it because of some unwanted subtleties:

(I was a bit disappointed when I found out about this, after first
hearing how much great optimization a haskell compiler _could_ do, but
that’s reality.)


Joachim "nomeata" Breitner
  mail: mail at joachim-breitner.de | ICQ# 74513189 | GPG-Key: 4743206C
  JID: nomeata at joachim-breitner.de | http://www.joachim-breitner.de/
  Debian Developer: nomeata at debian.org
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Dies ist ein digital signierter Nachrichtenteil
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20091204/5a395e09/attachment.bin

More information about the Haskell-Cafe mailing list