From 061092a4ba74d5a7e3c3272de69512219bb86b28 Mon Sep 17 00:00:00 2001 From: Vinicius Stock Date: Thu, 3 Sep 2020 14:00:43 -0400 Subject: [PATCH 1/2] Reduce allocations in expanded_version and expanded_key --- activesupport/lib/active_support/cache.rb | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb index b54ae1157d..cfb9ae5bac 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -617,13 +617,7 @@ module ActiveSupport # Deletes multiples entries in the cache implementation. Subclasses MAY # implement this method. def delete_multi_entries(entries, **options) - entries.inject(0) do |sum, key| - if delete_entry(key, **options) - sum + 1 - else - sum - end - end + entries.count { |key| delete_entry(key, **options) } end # Merges the default options with ones specific to a method call. @@ -687,7 +681,7 @@ module ActiveSupport expanded_key(key.first) end when Hash - key.collect { |k, v| "#{k}=#{v}" }.sort + key.collect { |k, v| "#{k}=#{v}" }.sort! else key end.to_param @@ -700,7 +694,7 @@ module ActiveSupport def expanded_version(key) case when key.respond_to?(:cache_version) then key.cache_version.to_param - when key.is_a?(Array) then key.map { |element| expanded_version(element) }.compact.to_param + when key.is_a?(Array) then key.map { |element| expanded_version(element) }.compact!.to_param when key.respond_to?(:to_a) then expanded_version(key.to_a) end end From 483aaaddfa56e13807d515d44dd90bb2e0aff3af Mon Sep 17 00:00:00 2001 From: Vinicius Stock Date: Thu, 3 Sep 2020 16:08:24 -0400 Subject: [PATCH 2/2] Prevent returning nil when compacting array --- activesupport/lib/active_support/cache.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb index cfb9ae5bac..d3f4370972 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -694,7 +694,7 @@ module ActiveSupport def expanded_version(key) case when key.respond_to?(:cache_version) then key.cache_version.to_param - when key.is_a?(Array) then key.map { |element| expanded_version(element) }.compact!.to_param + when key.is_a?(Array) then key.map { |element| expanded_version(element) }.tap(&:compact!).to_param when key.respond_to?(:to_a) then expanded_version(key.to_a) end end