Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't use 'rescue' unecessarily (making url_for faster) #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

jdelStrother
Copy link

Hi - I was profiling a slow page with a lot of url_fors, and found the non_ssl_host method showing up in my profile. Throwing exceptions & catching them is relatively slow - how about the attached commit?

If you want the benchmarks -

require 'bundler/setup'
require 'action_dispatch'
require 'active_support/core_ext'
require 'benchmark/ips'
require_relative 'lib/ssl_requirement'

routes = ActionDispatch::Routing::RouteSet.new
routes.default_url_options[:host] = 'test.host'
routes.draw do
  match ':controller(/:action(/:id(.:format)))'
end

routes.url_for(controller: 'c', action: 'a')

Benchmark.ips do |x|
  x.report("with exception+rescue") { routes.url_for(controller: 'c', action: 'a') }
end

# patch up SslRequirement to avoid the throw/rescue 
module SslRequirement
  self.ssl_host = nil
  self.non_ssl_host = nil
  def self.ssl_host
    determine_host(@@ssl_host)
  end
  def self.non_ssl_host
    determine_host(@@non_ssl_host)
  end
end
Benchmark.ips do |x|
  x.report("without exception+rescue") { routes.url_for(controller: 'c', action: 'a') }
end
Calculating -------------------------------------
with exception+rescue
                          1038 i/100ms
-------------------------------------------------
with exception+rescue
                        11491.7 (±4.3%) i/s -      58128 in   5.068276s
Calculating -------------------------------------
without exception+rescue
                          1208 i/100ms
-------------------------------------------------
without exception+rescue
                        13492.2 (±4.6%) i/s -      67648 in   5.025101s

Jonathan del Strother added 3 commits July 25, 2013 15:11
Throwing exceptions & catching them is relatively slow - we can make url_for measurably faster by avoiding that
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant