Skip to content

Commit 0e5f9a0

Browse files
authored
Merge pull request rails#40171 from vinistock/reduce_allocations_in_cache_methods
Reduce allocations in expanded_version and expanded_key
2 parents 3d42877 + 483aaad commit 0e5f9a0

File tree

1 file changed

+3
-9
lines changed
  • activesupport/lib/active_support

1 file changed

+3
-9
lines changed

activesupport/lib/active_support/cache.rb

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -617,13 +617,7 @@ def delete_entry(key, **options)
617617
# Deletes multiples entries in the cache implementation. Subclasses MAY
618618
# implement this method.
619619
def delete_multi_entries(entries, **options)
620-
entries.inject(0) do |sum, key|
621-
if delete_entry(key, **options)
622-
sum + 1
623-
else
624-
sum
625-
end
626-
end
620+
entries.count { |key| delete_entry(key, **options) }
627621
end
628622

629623
# Merges the default options with ones specific to a method call.
@@ -687,7 +681,7 @@ def expanded_key(key)
687681
expanded_key(key.first)
688682
end
689683
when Hash
690-
key.collect { |k, v| "#{k}=#{v}" }.sort
684+
key.collect { |k, v| "#{k}=#{v}" }.sort!
691685
else
692686
key
693687
end.to_param
@@ -700,7 +694,7 @@ def normalize_version(key, options = nil)
700694
def expanded_version(key)
701695
case
702696
when key.respond_to?(:cache_version) then key.cache_version.to_param
703-
when key.is_a?(Array) then key.map { |element| expanded_version(element) }.compact.to_param
697+
when key.is_a?(Array) then key.map { |element| expanded_version(element) }.tap(&:compact!).to_param
704698
when key.respond_to?(:to_a) then expanded_version(key.to_a)
705699
end
706700
end

0 commit comments

Comments
 (0)