Skip to content

Commit

Permalink
Speed up Hash#transform_values when empty
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sgrif committed Sep 28, 2015
1 parent 5875042 commit 3e408a2
Showing 1 changed file with 1 addition and 0 deletions.
Expand Up @@ -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)
Expand Down

1 comment on commit 3e408a2

@theldoria
Copy link

@theldoria theldoria commented on 3e408a2 Feb 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this change behaviour? If current class was derived from Hash it returns a plain Hash. It probably should be:

     result = self.class.new
     return result if empty?

Please sign in to comment.