From 6da99b4e99c90c63015f0d1cb4bf10983ea26a36 Mon Sep 17 00:00:00 2001 From: schneems Date: Thu, 20 Sep 2018 23:09:10 -0500 Subject: Decrease memory allocations in cache.rb The `merged_options` method is private and the output is never mutated. Using this info we can get rid of the `dup` behavior. We can also eliminate the `merge` call in the case where all the options being merged are the same. Returned results are frozen as an extra layer of protection against mutation. Before ``` Total allocated: 741749 bytes (6642 objects) ``` After ``` Total allocated: 734039 bytes (6648 objects) ``` Diff ``` (741749 - 734039) / 741749.0 => ~ 1.0 % ``` If you don't feel comfortable modifying this method, we could rename it and only use it internally. --- activesupport/lib/active_support/cache.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb index 8e516de4c9..1d8a90ed0e 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -596,9 +596,13 @@ module ActiveSupport # Merges the default options with ones specific to a method call. def merged_options(call_options) if call_options - options.merge(call_options) + if options.empty? + call_options + else + options.merge(call_options) + end else - options.dup + options end end -- cgit v1.2.3