aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_dispatch/routing/route_set.rb
diff options
context:
space:
mode:
authorBenjamin Quorning <bquorning@zendesk.com>2015-08-02 13:21:27 +0200
committerBenjamin Quorning <bquorning@zendesk.com>2015-08-02 13:21:27 +0200
commit113f8a6cc91e41d3d009f0c1941eb1db2dba1463 (patch)
tree692f75bd775a583b2cab62b3e11b026fb6bd672b /actionpack/lib/action_dispatch/routing/route_set.rb
parent13beb92a1ee189b64d3121213b7b8fcbf25a60ea (diff)
downloadrails-113f8a6cc91e41d3d009f0c1941eb1db2dba1463.tar.gz
rails-113f8a6cc91e41d3d009f0c1941eb1db2dba1463.tar.bz2
rails-113f8a6cc91e41d3d009f0c1941eb1db2dba1463.zip
Use #start_with? and #[] for speed
While the readability may be slightly worse, the speed improvement is significant: Twice as fast when there's no leading "/" to remove, and over 4 times as fast when there is a leading "/". Benchmark: require 'benchmark/ips' def match(controller) if controller if m = controller.match(/\A\/(?<controller_without_leading_slash>.*)/) m[:controller_without_leading_slash] else controller end end end def start_with(controller) if controller if controller.start_with?('/'.freeze) controller[1..-1] else controller end end end Benchmark.ips do |x| x.report("match") { match("no_leading_slash") } x.report("start_with") { start_with("no_leading_slash") } x.compare! end Benchmark.ips do |x| x.report("match") { match("/a_leading_slash") } x.report("start_with") { start_with("/a_leading_slash") } x.compare! end Result (Ruby 2.2.2): Calculating ------------------------------------- match 70.324k i/100ms start_with 111.264k i/100ms ------------------------------------------------- match 1.468M (± 7.1%) i/s - 7.314M start_with 3.787M (± 3.5%) i/s - 18.915M Comparison: start_with: 3787389.4 i/s match: 1467636.4 i/s - 2.58x slower Calculating ------------------------------------- match 36.694k i/100ms start_with 86.071k i/100ms ------------------------------------------------- match 532.795k (± 4.7%) i/s - 2.679M start_with 2.518M (± 5.8%) i/s - 12.566M Comparison: start_with: 2518366.8 i/s match: 532794.5 i/s - 4.73x slower
Diffstat (limited to 'actionpack/lib/action_dispatch/routing/route_set.rb')
-rw-r--r--actionpack/lib/action_dispatch/routing/route_set.rb4
1 files changed, 2 insertions, 2 deletions
diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb
index a006a146ed..c968381c26 100644
--- a/actionpack/lib/action_dispatch/routing/route_set.rb
+++ b/actionpack/lib/action_dispatch/routing/route_set.rb
@@ -676,8 +676,8 @@ module ActionDispatch
# Remove leading slashes from controllers
def normalize_controller!
if controller
- if m = controller.match(/\A\/(?<controller_without_leading_slash>.*)/)
- @options[:controller] = m[:controller_without_leading_slash]
+ if controller.start_with?("/".freeze)
+ @options[:controller] = controller[1..-1]
else
@options[:controller] = controller
end