Scala 2.9 parallel collections for MandelActors

Scala 2.9 is out, and one of the new features in this release are parallel collections. These make it almost trivial to do for example a foreach over a collection in parallel. I added a ParallelCollectionsRenderer to MandelActors which looks very simple:

object ParallelCollectionsRenderer extends Renderer {
  override def render(sampler: Sampler, compute: Sample => Color, pixelBuffer: PixelBuffer) {
    for (batch <- sampler.batches.par; sample <- batch) pixelBuffer.add(sample, compute(sample))

  override def toString = "ParallelCollectionsRenderer"

It looks almost the same as the SingleThreadRenderer. The only difference is the .par that I added to the collection of sample batches. Scala does the rest under the covers; it performs the loop over the batches in parallel, and the program runs just as fast as the other multi-threaded renderers. With VisualVM I can see that on my dual-core system it creates two threads to iterate over the collection.

No need to program with actors or threads yourself anymore!


  1. So, the million dollar question… how much faster are the parallel collections on your dual core machine?

    Very interesting blog! I’ve added the feed to my reader.

    • Hi Chris,

      Theoretically it could run at twice the speed with two threads, but in practice my program runs about 50% faster on my dual-core machine, and about 100% faster on a quad core machine that I tested it on. There is some synchronization to prevent threads from writing to the same pixel of the output image at the same time, but the rendering is done in such a way that threads should rarely have to wait on each other for that. Note that this program is just one example, it isn’t proof that any program will run 50% faster on a dual core machine.

      The performance with parallel collections is the same as with actors, which I wrote about in an earlier blog post.

Comments are closed.