From 3e408a2654727b49ee0e054e27a4f4ca199f72a7 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Mon, 28 Sep 2015 10:37:28 -0400 Subject: Speed up `Hash#transform_values` when empty MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We're calling this function on an empty hash as part of copying the attribute set during dirty checking initialization. The new structure caused a performance regression on loading records from the database. This causes `User.all.to_a` to perform about 10% faster w/ 10k records. Calculating ------------------------------------- User.all - master 9.000 i/100ms User.all - sg-fix-ar-regression 8.000 i/100ms ------------------------------------------------- User.all - master 81.236 (± 7.4%) i/s - 405.000 User.all - sg-fix-ar-regression 89.716 (± 7.8%) i/s - 448.000 --- activesupport/lib/active_support/core_ext/hash/transform_values.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/activesupport/lib/active_support/core_ext/hash/transform_values.rb b/activesupport/lib/active_support/core_ext/hash/transform_values.rb index e9bcce761f..6b3402b2c9 100644 --- a/activesupport/lib/active_support/core_ext/hash/transform_values.rb +++ b/activesupport/lib/active_support/core_ext/hash/transform_values.rb @@ -6,6 +6,7 @@ class Hash # # => { a: 2, b: 4, c: 6 } def transform_values return enum_for(:transform_values) unless block_given? + return {} if empty? result = self.class.new each do |key, value| result[key] = yield(value) -- cgit v1.2.3