Gregg’s “Speeding up Ruby, without the Rails” Presentation

Gregg’s “Speeding up Ruby, without the Rails” Presentation

He points out that the C library stuff is going to cause blocking, because Ruby’s threading manager doesn’t know when it can stop execution. He demos this with mysqlplus, showing 10 queries with the blocking driver, and then showing mysqlplus which is the non-blocking driver. 10x speedup.

He points out dbslayer, which sits between processes and MySQL, and passes JSON out to the processes.

Starling (which uses memcached) is a good tool for job queueing. Looks like it sits on a single server, and serves up jobs when requested. He demoed putting integers into the job stack, could we put whole objects into the stack?

“What if I can’t find a C library to do what I want?” — use Ruby Inline. It lets you embed C code *directly* into Ruby.

Also, he is talking about ruby-prof, which is for profiling Ruby code.

gem install ruby-prof

Very nice stuff, but interpreting the output definitely requires some knowledge. One neat trick is that if you

require ‘ruby-prof’

You can start and stop the profiling so that you only get data from the parts of the code you’re interested in. Also, with this technique, you can easily output HTML formatted and linked code.

Interesting optimizing trick

“#{@var1} #{@var2}”

is faster than

@var + ” ” + @var2

because method calls are expensive, and the plus signs are method calls.

Another pro-tip: if you’re doing a switch-case, put the most common option at the top of the list — it cuts down on the number of compares that it will take to find the desired option.

In Rails 2.2, there’s a new function called “memoize”, which will automatically handle memoizing, so you don’t have to do a

@result ||=

Don't use .nil? calls, because method calls are slow.

if !value

or better

unless value

are better choices.

a, b = b, a # Nice trick for swapping two values

Link love for Gregg: http://www.envycasts.com His presentation style is excellent, and I guarantee you will learn at least one thing you didn't know before.

http://www.railsenvy.com/rubyconf/ This link has more info from his presentation, and source material