Commit 64738843 authored by Stan Hu's avatar Stan Hu

Merge branch 'lower-allocations-nav' into 'master'

Lower allocations when building nav

See merge request gitlab-org/gitlab!51628
parents ffcd61e1 8d69183b
...@@ -44,7 +44,7 @@ module ApplicationHelper ...@@ -44,7 +44,7 @@ module ApplicationHelper
# current_controller?('gitlab/application') # => false # current_controller?('gitlab/application') # => false
def current_controller?(*args) def current_controller?(*args)
args.any? do |v| args.any? do |v|
v.to_s.downcase == controller.controller_name || v.to_s.downcase == controller.controller_path Gitlab::Utils.safe_downcase!(v.to_s) == controller.controller_name || Gitlab::Utils.safe_downcase!(v.to_s) == controller.controller_path
end end
end end
...@@ -59,7 +59,7 @@ module ApplicationHelper ...@@ -59,7 +59,7 @@ module ApplicationHelper
# current_action?(:create) # => false # current_action?(:create) # => false
# current_action?(:new, :create) # => true # current_action?(:new, :create) # => true
def current_action?(*args) def current_action?(*args)
args.any? { |v| v.to_s.downcase == action_name } args.any? { |v| Gitlab::Utils.safe_downcase!(v.to_s) == action_name }
end end
def admin_section? def admin_section?
......
...@@ -72,7 +72,8 @@ module TabHelper ...@@ -72,7 +72,8 @@ module TabHelper
# Add our custom class into the html_options, which may or may not exist # 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 # and which may or may not already have a :class key
o = options.delete(:html_options) || {} o = options.delete(:html_options) || {}
o[:class] = [*o[:class], klass].join(' ').strip o[:class] = [*o[:class], klass].join(' ')
o[:class].strip!
if block_given? if block_given?
content_tag(:li, capture(&block), o) content_tag(:li, capture(&block), o)
......
---
title: Lower allocations when building nav
merge_request: 51628
author:
type: performance
...@@ -174,6 +174,18 @@ module Gitlab ...@@ -174,6 +174,18 @@ module Gitlab
rescue IPAddr::InvalidAddressError rescue IPAddr::InvalidAddressError
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
# Converts a string to an Addressable::URI object. # Converts a string to an Addressable::URI object.
# If the string is not a valid URI, it returns nil. # If the string is not a valid URI, it returns nil.
# Param uri_string should be a String object. # Param uri_string should be a String object.
......
...@@ -392,6 +392,23 @@ RSpec.describe Gitlab::Utils do ...@@ -392,6 +392,23 @@ RSpec.describe Gitlab::Utils do
end end
end end
describe ".safe_downcase!" do
using RSpec::Parameterized::TableSyntax
where(:str, :result) do
"test".freeze | "test"
"Test".freeze | "test"
"test" | "test"
"Test" | "test"
end
with_them do
it "downcases the string" do
expect(described_class.safe_downcase!(str)).to eq(result)
end
end
end
describe '.parse_url' do describe '.parse_url' do
it 'returns Addressable::URI object' do it 'returns Addressable::URI object' do
expect(described_class.parse_url('http://gitlab.com')).to be_instance_of(Addressable::URI) expect(described_class.parse_url('http://gitlab.com')).to be_instance_of(Addressable::URI)
......
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