Commit 9750bf0e authored by Robert May's avatar Robert May

Lower allocations when building nav

This cuts down various string allocations in nav helpers,
which are used a lot and inside of loops. Minor performance
improvement.
parent c46476c0
......@@ -44,7 +44,7 @@ module ApplicationHelper
# current_controller?('gitlab/application') # => false
def current_controller?(*args)
args.any? do |v|
v.to_s.downcase == controller.controller_name || v.to_s.downcase == controller.controller_path
safe_downcase!(v.to_s) == controller.controller_name || safe_downcase!(v.to_s) == controller.controller_path
end
end
......@@ -59,7 +59,7 @@ module ApplicationHelper
# current_action?(:create) # => false
# current_action?(:new, :create) # => true
def current_action?(*args)
args.any? { |v| v.to_s.downcase == action_name }
args.any? { |v| safe_downcase!(v.to_s) == action_name }
end
def admin_section?
......@@ -397,6 +397,19 @@ module ApplicationHelper
end
end
# A safe alternative to String#downcase!
#
# This will make copies of frozen strings but downcase unfrozen
# strings in place, reducing allocations.
def safe_downcase!(str)
if str.frozen?
str.downcase
else
str.downcase! || str
end
end
private
def appearance
......
......@@ -72,7 +72,8 @@ module TabHelper
# Add our custom class into the html_options, which may or may not exist
# and which may or may not already have a :class key
o = options.delete(:html_options) || {}
o[:class] = [*o[:class], klass].join(' ').strip
o[:class] = [*o[:class], klass].join(' ')
o[:class].strip!
if block_given?
content_tag(:li, capture(&block), o)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment