From f3c19368b1e0895c108fa771a396a0a9b46d8fae Mon Sep 17 00:00:00 2001 From: Alex Schmitt Date: Thu, 3 Feb 2022 11:52:49 -0800 Subject: [PATCH] Tweak array construction in `normalize_keys` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This results in a performance increase, and reduced object allocation, when normalizing keys - ```ruby report = Benchmark.ips do |x| x.report("concat") do ex = [] ex.concat ['foo'] ex.concat ['bar', 'baz'] ex.concat ['quix'] ex end x.report("splat") do [ *['foo'], *['bar', 'baz'], *['quix'] ] end x.compare! Warming up -------------------------------------- concat 129.103k i/100ms splat 243.963k i/100ms Calculating ------------------------------------- concat 1.176M (± 9.7%) i/s - 5.810M in 5.009251s splat 2.435M (±11.8%) i/s - 11.954M in 5.019257s Comparison: splat: 2434746.6 i/s concat: 1176432.3 i/s - 2.07x (± 0.00) slower ``` --- lib/i18n.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/i18n.rb b/lib/i18n.rb index 1b32f470..e197e2b1 100644 --- a/lib/i18n.rb +++ b/lib/i18n.rb @@ -338,11 +338,11 @@ def with_locale(tmp_locale = nil) def normalize_keys(locale, key, scope, separator = nil) separator ||= I18n.default_separator - keys = [] - keys.concat normalize_key(locale, separator) - keys.concat normalize_key(scope, separator) - keys.concat normalize_key(key, separator) - keys + [ + *normalize_key(locale, separator), + *normalize_key(scope, separator), + *normalize_key(key, separator) + ] end # Returns true when the passed locale, which can be either a String or a