YARV Looking Promising, James's C is Not
I participated in the ICFP programming contest last weekend with a group of friends. We had a great time with the event and learned a ton. I thought I should share two interesting insights with others that might appreciate them.
First, YARV looks very promising for some general speed increases in Ruby. If you are not familiar with YARV, that's the virtual machine that will run Ruby 1.9. During the contest, we ran into some performance issues with our Ruby solution and after we had optimized all we could think of, we decided to try running our entry on the experimental YARV VM to see if it was faster there. Good news: it was a lot faster.
Please do not take these numbers as anything more than very non-scientific observations, but we did notice a huge speed increase on YARV. We were reliably waiting around 15 minutes for one section of our program to run on Ruby 1.8.4, but when we introduced YARV the same section generally ran in just under seven minutes. You heard me right there, it was over twice as fast. I think that's very promising news for the future of Ruby.
The not so good news is that it still just wasn't fast enough.
Of course we want to be able to use Ruby for as much as possible, but it is important to admit that it's just not fit for every job. The programming contest involved the creation of a small VM that ran many, many instructions from contest provided data files. In order to get that to a reasonable level of performance, you really needed some C.
The good news is that Ruby will easily allow you to drop down to C and integrate that code with your script. The bad news is that James's C is so rusty, that was a nightmare. Thank goodness one of my partners was more capable. He certainly carried us through.
I don't need C very often any more. I think it has literally been about a year since I last felt the need. However, there are jobs Ruby is a bit too slow to handle and when they come up, C is your best friend. I'm definitely brushing up on my C skills before next year's contest.
Curt Sampson November 24th, 2006 Reply Link
I've run into performance issues recently with a largish Ruby library, too. As well as brushing up on your C skills, you might want to start digging into the Ruby source code to see how it works, especially for classes like
String. There are internal APIs for various classes in Ruby (notably
String) that, if you have to call them from C, will be much faster than invoking the equivalent Ruby method from C. And if you're not too worried about compatibility with versions of Ruby other than the one you're currently using, you can probably even dig right into the non-public interfaces or the class data itself if necessary (though this can be quite dangerous).