1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Do not recompute length

We can get a speed gain by moving the length calculation and assignment out of the loop.
This commit is contained in:
schneems 2018-08-30 09:57:01 -05:00
parent cc81cd359c
commit 897c377bad

View file

@ -140,6 +140,8 @@ module ActiveRecord
# We freeze the strings to prevent them getting duped when # We freeze the strings to prevent them getting duped when
# used as keys in ActiveRecord::Base's @attributes hash # used as keys in ActiveRecord::Base's @attributes hash
columns = @columns.map(&:-@) columns = @columns.map(&:-@)
length = columns.length
@rows.map { |row| @rows.map { |row|
# In the past we used Hash[columns.zip(row)] # In the past we used Hash[columns.zip(row)]
# though elegant, the verbose way is much more efficient # though elegant, the verbose way is much more efficient
@ -148,8 +150,6 @@ module ActiveRecord
hash = {} hash = {}
index = 0 index = 0
length = columns.length
while index < length while index < length
hash[columns[index]] = row[index] hash[columns[index]] = row[index]
index += 1 index += 1