RubyFlow The Ruby and Rails community linklog

Parallel Each with Peach

Peach is a cute Ruby library that provides threaded parallel versions of the “each”, “map”, and “delete_if” iterators (as peach, pmap and pdelete_if respectively.) It’s like a lighter, green threaded attempt at a MapReduce for Ruby.

Comments

Testing how posting code works out..

require 'peach' [1,2,3,4].peach{|x| f(x)} #Spawns 4 threads, => [1,2,3,4] [1,2,3,4].pmap{|x| f(x)} #Spawns 4 threads, => [f(1),f(2),f(3),f(4)] [1,2,3,4].pdelete_if{|x| x > 2} #Spawns 4 threads, => [3,4] [1,2,3,4].peach(2){|x| f(x)} #Spawns 2 threads, => [1,2,3,4] [1,2,3,4].pmap(2){|x| f(x)} #Spawns 2 threads, => [f(1),f(2),f(3),f(4)] [1,2,3,4].pdelete_if(2){|x| x > 2} #Spawns 2 threads, => [3,4]

Needs some work, but we’ll get there..

Yes, but because Thread creation is more expensive than method dispatch, it doesn’t give you any benefit for the benchmark cases.

Author here. On MRI Peach does add overhead to the methods and certainly offers no compelling advantages. On JRuby, where the men are men and threads are threads, the real parallel execution offers the sort of speed up one would expect. As long as you restrict the number of threads spawned to a sensible number for the problem set and use it on a ruby implementation with real multithreading, peach will improve performance. Switching to JRuby also helps make your code go blazing fast.

With this, some simple parallel execution becomes really convenient. If you have any suggestions, comments, or questions do let me know at ben (at) pixelmachine.org.

Post a comment

You can use basic HTML markup (e.g. <a>) or Markdown.

As you are not logged in, you will be
directed via GitHub to signup or sign in