Merge pull request #37614 from jhawthorn/transform_values

Use transform_values to build hash_rows
This commit is contained in:
John Hawthorn 2020-01-29 15:11:54 -08:00 committed by GitHub
commit c801763628
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -145,8 +145,18 @@ def hash_rows
# used as keys in ActiveRecord::Base's @attributes hash
columns = @columns.map(&:-@)
length = columns.length
template = nil
@rows.map { |row|
if template
# We use transform_values to build subsequent rows from the
# hash of the first row. This is faster because we avoid any
# reallocs and in Ruby 2.7+ avoid hashing entirely.
index = -1
template.transform_values do
row[index += 1]
end
else
# In the past we used Hash[columns.zip(row)]
# though elegant, the verbose way is much more efficient
# both time and memory wise cause it avoids a big array allocation
@ -159,7 +169,12 @@ def hash_rows
index += 1
end
# It's possible to select the same column twice, in which case
# we can't use a template
template = hash if hash.length == length
hash
end
}
end
end