Skip to content

Commit

Permalink
More optimizations on respond_to after a profile and benching:
Browse files Browse the repository at this point in the history
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
  • Loading branch information
wycats committed Dec 27, 2008
1 parent f4f8923 commit 4f043a4
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions actionpack/lib/action_controller/mime_responds.rb
Expand Up @@ -147,13 +147,9 @@ def any(*args, &block)
def self.generate_method_for_mime(mime)
sym = mime.is_a?(Symbol) ? mime : mime.to_sym
const = sym.to_s.upcase
class_eval <<-RUBY
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{sym}(&block) # def html(&block)
if Mime::SET.include?(Mime::#{const}) # if Mime::Set.include?(Mime::HTML)
custom(Mime::#{const}, &block) # custom(Mime::HTML, &block)
else # else
super # super
end # end
custom(Mime::#{const}, &block) # custom(Mime::HTML, &block)
end # end
RUBY
end
Expand Down

6 comments on commit 4f043a4

@dkubb
Copy link
Contributor

@dkubb dkubb commented on 4f043a4 Dec 28, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@HamptonMakes
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a wycats in my railz.

Help pls.

@adkron
Copy link
Contributor

@adkron adkron commented on 4f043a4 Dec 29, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member

@lifo lifo commented on 4f043a4 Dec 29, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not always. http://gist.github.com/11326

@NZKoz
Copy link
Member

@NZKoz NZKoz commented on 4f043a4 Dec 29, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Contributor

@adkron adkron commented on 4f043a4 Dec 29, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Please sign in to comment.