some object allocation reduction for new AR objects
Benchmark: ```ruby require 'objspace' require 'active_record' ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:" ActiveRecord::Base.connection.instance_eval do create_table(:articles) { |t| t.string :name } end class Article < ActiveRecord::Base; end a = Article.create name: "foo" a = Article.find a.id N = 10 ObjectSpace::AllocationTracer.trace do N.times { Article.find a.id } end ObjectSpace::AllocationTracer.allocated_count_table table.sort_by { |_,x| x }.each do |k,v| p k => (v / N) end ```
This commit is contained in:
parent
512122d984
commit
c363fff29f
@ -23,8 +23,11 @@ def build_attributes_from_values(values, additional_types)
|
||||
end
|
||||
|
||||
def add_uninitialized_attributes(attributes)
|
||||
types.except(*attributes.keys).each do |name, type|
|
||||
attributes[name] = Attribute.uninitialized(name, type)
|
||||
types.each_key do |name|
|
||||
next if attributes.key? name
|
||||
type = types[name]
|
||||
attributes[name] =
|
||||
Attribute.uninitialized(name, type)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -37,7 +37,8 @@ module Querying
|
||||
# Post.find_by_sql ["SELECT body FROM comments WHERE author = :user_id OR approved_by = :user_id", { :user_id => user_id }]
|
||||
def find_by_sql(sql, binds = [])
|
||||
result_set = connection.select_all(sanitize_sql(sql), "#{name} Load", binds)
|
||||
column_types = result_set.column_types.except(*columns_hash.keys)
|
||||
column_types = result_set.column_types.dup
|
||||
columns_hash.each_key { |k| column_types.delete k }
|
||||
result_set.map { |record| instantiate(record, column_types) }
|
||||
end
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user