Why JRuby
Preface
There are many different implementations of Ruby 1, and each has their own set of disadvantages and advantages.
Why JRuby?
JRuby has many advantages, including
Real Threads?
As explained in the Restless Programmer post linked earlier, JRuby runs on the Java Virtual Machine (JVM) 5, allowing it to provide 1:1 system threads 6.
While Matz’s Ruby Interpreter (MRI) provides threads, they’re constrained by the MRI implementation of a Global Interpreter Lock 7.
Why does this matter?
Let’s consider this thought experiment:
You have many large .csv files, each about 1GB large. You’d like to process each of them using a simple process:
- Split each file into workable pieces
- Read each piece concurrently
So, you end up writing something like this [8]
|
|
|
Waits for threads to finish, returns each thread in turn when done |
But the process is taking forever! So, you open up your ActivityMonitor (or Task Manager) to see what’s going on. Maybe the program hung!
Nope. You see that your ruby process, of which there’s only one entry in your ActivityMonitor, is running at 100%. It even has more than one thread! But then, on your processor view, you notice that only one core is being maxxed out.
What gives? MRI Ruby is doing its best to provide the perception of a program running in multiple threads. In fact, each process is running in multiple threads; however, you can think of the MRI implementation as running these threads within the confines of a single parent thread.
You’ve probably guessed by now that JRuby is an implementation which is able to bypass this MRI limitation.
In JRuby, each thread runs as a thread on the JVM, where it’s usually mapped to a native operating system thread after some fancy footwork by the JVM. 6.
So in JRuby, running a similar convoluted example, we’d see something like this:
Ok, I admit, the examples in the screenshots aren’t exactly real-world use-cases, but you get the point: JRuby tangibly grants you more power. Now, all you have to do is not fuck it up! Check out the list below for reading material to get you quick-started on this last point, of not screwing up threads, state, etc. in JRuby.
Mo
[8]: Keep in mind, this is not a great example, as we read the entire file into memory just for demonstrating a long running process.