18

Because I can see it coming: this is a different question than What optimizations can GHC be expected to perform reliably? because I'm not asking for the most reliable optimizations, just the most clever/powerful.

I'm specifically looking for non-intuitive optimizations that GHC makes that can have serious impacts on performance and demonstrate the power of compiler optimizations related to lazy evaluation or purity. And direct explanations about how to get at them.

The best answers will have:

  • An explanation of the optimization and why it is so clever or powerful
  • Why the optimization improves performance
  • How GHC recognizes when it can use this optimization
  • What the optimization actually transforms the code into
  • Why this optimization requires lazy evaluation or purity
Community
  • 1
  • 1
reem
  • 7,186
  • 4
  • 20
  • 30
  • I've added lots of specificity on the kinds of answers I want and exactly what I'm looking for. What else should I add to make this less broad? – reem Dec 30 '13 at 21:30
  • 3
    However many limitations you add, this is by its very nature a free-for-all open ended question which has no one particular answer. – deceze Dec 30 '13 at 21:44
  • @deceze: I faved this question, but I am not voting for reopen, because of the exact reason you mention. – Sebastian Mach Dec 30 '13 at 21:45
  • @deceze: But this is true of several other questions, like the one I linked to, which remained open. – reem Dec 30 '13 at 21:53
  • @sortfiend StackOverflow (and other SE sites) are generally unfriendly to "big list" questions. See e.g. [this meta question](http://meta.stackexchange.com/a/98366) for why. – Daniel Wagner Dec 30 '13 at 22:09
  • @DanielWagner Maybe if I rephrase to asking how to get at the cleverest/best GHC optimizations? – reem Dec 30 '13 at 22:36
  • Changed the question to make it less "listy" and more specifically applicable. – reem Dec 30 '13 at 23:28
  • @phresnel I changed the structure of the question, so it is less of a free-for-all. – reem Dec 31 '13 at 20:46
  • I think it is still too broad. "Best" is highly subjective and depends on a particular code-section and its full context. – Sebastian Mach Jan 02 '14 at 11:29

1 Answers1

3

Stream fusion is probably the biggest one. It turns something like sum . map (+1) . filter (>5), which nominally allocates two new lists, into a simple loop that operates in constant space.

Dirk Holsopple
  • 8,731
  • 1
  • 24
  • 37
  • 5
    However, that's not a `ghc` optimization, but actually a library optimization implemented as a set of rewrite rules. The compiler has no built-in support for optimizing lists using fusion. – Gabriella Gonzalez Dec 30 '13 at 22:31