From c363fff29f060e6a2effe1e4bb2c4dd4cd805d6e Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Sat, 27 Sep 2014 11:00:58 +0400 Subject: 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 ``` --- activerecord/lib/active_record/attribute_set/builder.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'activerecord/lib/active_record/attribute_set/builder.rb') diff --git a/activerecord/lib/active_record/attribute_set/builder.rb b/activerecord/lib/active_record/attribute_set/builder.rb index 1e146a07da..d4a787f2fe 100644 --- a/activerecord/lib/active_record/attribute_set/builder.rb +++ b/activerecord/lib/active_record/attribute_set/builder.rb @@ -23,8 +23,11 @@ module ActiveRecord 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 -- cgit v1.2.3