public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
More optimizations on respond_to after a profile and benching:

App with simple respond_to:
  def index
    respond_to do |format|
      format.html
      format.xml
      format.json
    end
  end

On JRuby (after complete hotspot warmup) -- 8% improvement:
  550 requests per second after this commit
  510 requests per second with old method_missing technique

On MRI (8% improvement):
  430 requests per second after this commit
  400 requests per second with old method_missing technique
wycats (author)
Sat Dec 27 00:06:57 -0800 2008
commit  4f043a48381c142e308824e3b7e15435a61bbb53
tree    f994f692d15a0d743ad5c2d942e2944fe3ff9140
parent  f4f8923cf0ef5bd31f9e98cecf4603d0c4bde296
...
147
148
149
150
 
151
152
153
154
155
156
 
157
158
159
...
147
148
149
 
150
151
 
 
 
 
 
152
153
154
155
0
@@ -147,13 +147,9 @@ module ActionController #:nodoc:
0
       def self.generate_method_for_mime(mime)
0
         sym = mime.is_a?(Symbol) ? mime : mime.to_sym
0
         const = sym.to_s.upcase
0
-        class_eval <<-RUBY
0
+        class_eval <<-RUBY, __FILE__, __LINE__ + 1
0
           def #{sym}(&block)                          # def html(&block)
0
-            if Mime::SET.include?(Mime::#{const})     #   if Mime::Set.include?(Mime::HTML)
0
-              custom(Mime::#{const}, &block)          #     custom(Mime::HTML, &block)
0
-            else                                      #   else
0
-              super                                   #     super
0
-            end                                       #   end
0
+            custom(Mime::#{const}, &block)            #   custom(Mime::HTML, &block)
0
           end                                         # end
0
         RUBY
0
       end

Comments

dkubb Sat Dec 27 16:46:11 -0800 2008

Another thing you might want to benchmark is using something like:

def #{sym} custom(Mime::#{const}) { |format| yield(format) } end

In DM we benchmarked this as faster than using &block explicitly.

hcatlin Sun Dec 28 13:55:57 -0800 2008

There is a wycats in my railz.

Help pls.

adkron Mon Dec 29 04:20:38 -0800 2008

dkubb has a great idea. Anytime that you use &block ruby makes a new Proc from the parameter, even when no block is given. This can slow you down.

lifo Mon Dec 29 08:36:39 -0800 2008
NZKoz Mon Dec 29 10:11:21 -0800 2008

We actually already do that optimisation with active record (for count, all, etc) and it did make a difference there, but as pratik mentions it’s not quite so simple.

There’s also some subtle differences between the two with some uses (have a look at the core list around september).

If the benchmarks show it faster here, let’s go for it. but it’s not ‘free performance’

adkron Mon Dec 29 12:07:39 -0800 2008

Hmm, going to have to do a little research to figure out when it is better and when it is not.