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

PERF: avoid allocating column names where possible

When requesting columns names from database adapters AR:Result
would dup/freeze column names, this prefers using fstrings which
cuts down on repeat allocations

Attributes that are retained keep these fstrings around for the long
term

Note, this has the highest impact on "short" result sets, eg: Topic.first where you can void allocating the number of columns * String.
This commit is contained in:
Sam 2018-06-06 09:50:58 +10:00
parent 32aa7cdd8f
commit a46dcb7454
3 changed files with 3 additions and 3 deletions

View file

@ -103,7 +103,7 @@ module ActiveModel
def self.set_name_cache(name, value) def self.set_name_cache(name, value)
const_name = "ATTR_#{name}" const_name = "ATTR_#{name}"
unless const_defined? const_name unless const_defined? const_name
const_set const_name, value.dup.freeze const_set const_name, -value
end end
end end
} }

View file

@ -26,7 +26,7 @@ module ActiveRecord
def self.set_name_cache(name, value) def self.set_name_cache(name, value)
const_name = "ATTR_#{name}" const_name = "ATTR_#{name}"
unless const_defined? const_name unless const_defined? const_name
const_set const_name, value.dup.freeze const_set const_name, -value
end end
end end
} }

View file

@ -125,7 +125,7 @@ module ActiveRecord
begin begin
# 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 { |c| c.dup.freeze } columns = @columns.map(&:-@)
@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