Skip to content

Commit 14c6e04

Browse files
committed
Do not allocate the first character when checking for relative paths
This is the same optimization applied in rails@a63ae91 which I proposed in rails#47714 Here's the benchmark: require "bundler/inline" ROOT_STRING = '/' TEST_PATH = "/some/path" gemfile(true) do source "https://rubygems.org" gem "benchmark-ips" end Benchmark.ips do |x| x.report("path[0]") do TEST_PATH[0] != ROOT_STRING end x.report("path.start_with?") do TEST_PATH.start_with?(ROOT_STRING) end x.compare! end Warming up -------------------------------------- path[0] 942.044k i/100ms path.start_with? 1.556M i/100ms Calculating ------------------------------------- path[0] 9.463M (± 0.9%) i/s - 48.044M in 5.077358s path.start_with? 15.611M (± 0.2%) i/s - 79.352M in 5.083056s Comparison: path.start_with?: 15611192.8 i/s path[0]: 9463245.0 i/s - 1.65x slower
1 parent fedaf03 commit 14c6e04

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

actionpack/lib/action_dispatch/routing/redirection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def inspect
6868

6969
private
7070
def relative_path?(path)
71-
path && !path.empty? && path[0] != "/"
71+
path && !path.empty? && !path.start_with?("/")
7272
end
7373

7474
def escape(params)

0 commit comments

Comments
 (0)