Map fusion: Making Haskell 225% faster
Posted by Eric Kidd Sat, 10 Feb 2007 09:55:00 GMT
Or, how to optimize MapReduce, and when folds are faster than loops
Purely functional programming might actually be worth the pain, if you care about large-scale optimization.
Lately, I’ve been studying how to speed up parallel algorithms. Many parallel algorithms, such as Google’s MapReduce, have two parts:
- First, you transform the data by mapping one or more functions over each value.
- Next, you repeatedly merge the transformed data, “reducing” it down to a final result.
Unfortunately, there’s a couple of nasty performance problems lurking here. We really want to combine all those steps into a single pass, so that we can eliminate temporary working data. But we don’t always want to do this optimization by hand—it would be better if the compiler could do it for us.
As it turns out, Haskell is an amazing testbed for this kind of optimization. Let’s build a simple model, show where it breaks, and then crank the performance way up.
Trees, and the performance problems they cause
We’ll use single-threaded trees for our testbed. They’re simple enough to demonstrate the basic idea, and they can be generalized to parallel systems. (If you want know how, check out the papers at the end of this article.)
A tree is either empty, or it is a node with a left child, a value and a right child:
Here’s a sample tree containing three values:
We can use treeMap to apply a function to every value in a
tree, creating a new tree:
Using treeMap, we can build various functions that manipulate
trees:
What if we want to add up all the values in a tree? Well, we could write a simple recursive sum function:
But for reasons that will soon become clear, it’s much better to refactor
the recursive part of treeSum into a reusable
treeFold function (“fold” is Haskell’s name for “reduce”):
Now we can double all the values in a tree, add 1 to each, and sum up the result:
But there’s a very serious problem with this code. Imagine that we’re
working with a million-node tree. The two calls to treeMap
(buried inside treeIncr and treeDouble) will each
create a new million-node tree. Obviously, this will kill our performance,
and it will make our garbage collector cry.
Fortunately, we can do a lot better than this, thanks to some funky GHC extensions.
Read more...