Skip to content

Commit 8d1b9fd

Browse files
committed
Micro-optimize Journey normalize_path
If we assume the vast majority of paths don't need to be normalized, we can save a lot of costly operations by simply validating our assumption.
1 parent dfc5e80 commit 8d1b9fd

File tree

1 file changed

+8
-1
lines changed
  • actionpack/lib/action_dispatch/journey/router

1 file changed

+8
-1
lines changed

actionpack/lib/action_dispatch/journey/router/utils.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@ class Utils # :nodoc:
1717
# normalize_path("") # => "/"
1818
# normalize_path("/%ab") # => "/%AB"
1919
def self.normalize_path(path)
20-
path ||= ""
20+
return "/".dup unless path
21+
22+
# Fast path for the overwhelming majority of paths that don't need to be normalized
23+
if path == "/" || (path.start_with?("/") && !path.end_with?("/") && !path.match?(%r{%|//}))
24+
return path.dup
25+
end
26+
27+
# Slow path
2128
encoding = path.encoding
2229
path = +"/#{path}"
2330
path.squeeze!("/")

0 commit comments

Comments
 (0)