Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

ActionController caching small String#split optimization #5570

Merged
merged 1 commit into from

3 participants

@avakhov

It's a little bit better to split on 2 parts.

require 'benchmark'

n = 50000
Benchmark.bm(7) do |x|
  x.report("rails:") { n.times {"http://rubyonrails.org/documentation".split(%r{://}).last } }
  x.report("my:") { n.times { "http://rubyonrails.org/documentation".split(%r{://}, 2).last } }
end

on my laptop:

              user     system      total        real
rails:    0.170000   0.000000   0.170000 (  0.171520)
my:       0.090000   0.000000   0.090000 (  0.096497)
actionpack/lib/action_controller/caching/actions.rb
@@ -170,14 +170,14 @@ def initialize(controller, options = {}, infer_extension = true)
options.reverse_merge!(:format => @extension) if options.is_a?(Hash)
end
- path = controller.url_for(options).split(%r{://}).last
+ path = controller.url_for(options).split(%r{://}, 2).last
@lest
lest added a note

Regular expression could be replaced with a string:

path = controller.url_for(options).split('://', 2).last
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@avakhov

Changed regexp to string and rebased.

@jeremy jeremy merged commit 64ccb59 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 24, 2012
  1. @avakhov
This page is out of date. Refresh to see the latest.
Showing with 2 additions and 2 deletions.
  1. +2 −2 actionpack/lib/action_controller/caching/actions.rb
View
4 actionpack/lib/action_controller/caching/actions.rb
@@ -170,14 +170,14 @@ def initialize(controller, options = {}, infer_extension = true)
options.reverse_merge!(:format => @extension) if options.is_a?(Hash)
end
- path = controller.url_for(options).split(%r{://}).last
+ path = controller.url_for(options).split('://', 2).last
@path = normalize!(path)
end
private
def normalize!(path)
path << 'index' if path[-1] == ?/
- path << ".#{extension}" if extension and !path.split('?').first.ends_with?(".#{extension}")
+ path << ".#{extension}" if extension and !path.split('?', 2).first.ends_with?(".#{extension}")
URI.parser.unescape(path)
end
end
Something went wrong with that request. Please try again.