Skip to content

Commit c363fff

Browse files
committed
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 ```
1 parent 512122d commit c363fff

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

activerecord/lib/active_record/attribute_set/builder.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ def build_attributes_from_values(values, additional_types)
2323
end
2424

2525
def add_uninitialized_attributes(attributes)
26-
types.except(*attributes.keys).each do |name, type|
27-
attributes[name] = Attribute.uninitialized(name, type)
26+
types.each_key do |name|
27+
next if attributes.key? name
28+
type = types[name]
29+
attributes[name] =
30+
Attribute.uninitialized(name, type)
2831
end
2932
end
3033
end

activerecord/lib/active_record/querying.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ module Querying
3737
# Post.find_by_sql ["SELECT body FROM comments WHERE author = :user_id OR approved_by = :user_id", { :user_id => user_id }]
3838
def find_by_sql(sql, binds = [])
3939
result_set = connection.select_all(sanitize_sql(sql), "#{name} Load", binds)
40-
column_types = result_set.column_types.except(*columns_hash.keys)
40+
column_types = result_set.column_types.dup
41+
columns_hash.each_key { |k| column_types.delete k }
4142
result_set.map { |record| instantiate(record, column_types) }
4243
end
4344

0 commit comments

Comments
 (0)