From 7d58fa87ef27712fc3dcd5a098d751a67563c9de Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 25 Jun 2018 15:52:59 -0700 Subject: [PATCH 1/3] Speed up homogeneous AR lists / reduce allocations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit speeds up allocating homogeneous lists of AR objects. We can know if the result set contains an STI column before initializing every AR object, so this change pulls the "does this result set contain an STI column?" test up, then uses a specialized instantiation function. This way we only have to check for an STI column once rather than N times. This change also introduces a new initialization function that is meant for use when allocating AR objects that come from the database. Doing this allows us to eliminate one hash allocation per AR instance. Here is a benchmark: ```ruby require 'active_record' require 'benchmark/ips' ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:" ActiveRecord::Migration.verbose = false ActiveRecord::Schema.define do create_table :users, force: true do |t| t.string :name t.timestamps null: false end end class User < ActiveRecord::Base; end 2000.times do User.create!(name: "Gorby") end Benchmark.ips do |x| x.report("find") do User.limit(2000).to_a end end ``` Results: Before: ``` [aaron@TC activerecord (master)]$ be ruby -I lib:~/git/allocation_tracer/lib speed.rb Warming up -------------------------------------- find 5.000 i/100ms Calculating ------------------------------------- find 56.192 (± 3.6%) i/s - 285.000 in 5.080940s ``` After: ``` [aaron@TC activerecord (homogeneous-allocation)]$ be ruby -I lib:~/git/allocation_tracer/lib speed.rb Warming up -------------------------------------- find 7.000 i/100ms Calculating ------------------------------------- find 72.204 (± 2.8%) i/s - 364.000 in 5.044592s ``` --- activerecord/lib/active_record/core.rb | 20 +++++++++++++++++++ activerecord/lib/active_record/persistence.rb | 8 +++++++- activerecord/lib/active_record/querying.rb | 7 ++++++- activerecord/lib/active_record/result.rb | 5 +++++ activerecord/test/cases/result_test.rb | 5 +++++ 5 files changed, 43 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb index e1a0b2ecf8..9c9fc77a0d 100644 --- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -349,6 +349,26 @@ module ActiveRecord self end + ## + # Initializer used for instantiating objects that have been read from the + # database. +attributes+ should be an attributes object, and unlike the + # `initialize` method, no assignment calls are made per attribute. + # + # :nodoc: + def init_from_db(attributes) + init_internals + + @new_record = false + @attributes = attributes + + yield self if block_given? + + _run_find_callbacks + _run_initialize_callbacks + + self + end + ## # :method: clone # Identical to Ruby's clone method. This is a "shallow" copy. Be warned that your attributes are not copied. diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index a0d5f1ee9f..72d92ef504 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -67,8 +67,14 @@ module ActiveRecord # how this "single-table" inheritance mapping is implemented. def instantiate(attributes, column_types = {}, &block) klass = discriminate_class_for_record(attributes) + instantiate_instance_of(klass, attributes, column_types, &block) + end + + # Given a class, an attributes hash, +instantiate+ returns a new instance + # of the class. Accepts only keys as strings. + def instantiate_instance_of(klass, attributes, column_types = {}, &block) attributes = klass.attributes_builder.build_from_database(attributes, column_types) - klass.allocate.init_with("attributes" => attributes, "new_record" => false, &block) + klass.allocate.init_from_db(attributes, &block) end # Updates an object (or multiple objects) and saves it to the database, if validations pass. diff --git a/activerecord/lib/active_record/querying.rb b/activerecord/lib/active_record/querying.rb index d33d36ac02..c84f3d0fbb 100644 --- a/activerecord/lib/active_record/querying.rb +++ b/activerecord/lib/active_record/querying.rb @@ -49,7 +49,12 @@ module ActiveRecord } message_bus.instrument("instantiation.active_record", payload) do - result_set.map { |record| instantiate(record, column_types, &block) } + if result_set.includes_column?(inheritance_column) + result_set.map { |record| instantiate(record, column_types, &block) } + else + # Instantiate a homogeneous set + result_set.map { |record| instantiate_instance_of(self, record, column_types, &block) } + end end end diff --git a/activerecord/lib/active_record/result.rb b/activerecord/lib/active_record/result.rb index ffef229be2..3549e8a3fa 100644 --- a/activerecord/lib/active_record/result.rb +++ b/activerecord/lib/active_record/result.rb @@ -43,6 +43,11 @@ module ActiveRecord @column_types = column_types end + # Does this result set include the column named +name+ ? + def includes_column?(name) + @columns.include? name + end + # Returns the number of elements in the rows array. def length @rows.length diff --git a/activerecord/test/cases/result_test.rb b/activerecord/test/cases/result_test.rb index db52c108ac..68fcafb682 100644 --- a/activerecord/test/cases/result_test.rb +++ b/activerecord/test/cases/result_test.rb @@ -12,6 +12,11 @@ module ActiveRecord ]) end + test "includes_column?" do + assert result.includes_column?("col_1") + assert_not result.includes_column?("foo") + end + test "length" do assert_equal 3, result.length end From a01de4deab59f57874965a69e9a4c83112b3cb47 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 26 Jun 2018 10:25:19 -0700 Subject: [PATCH 2/3] define attribute methods in `init_from_db` Now that `allocate` is removed, we need to define attribute methods in all "init" methods. --- activerecord/lib/active_record/core.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb index 20d9e7a9ca..df795df52e 100644 --- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -356,6 +356,8 @@ module ActiveRecord @new_record = false @attributes = attributes + self.class.define_attribute_methods + yield self if block_given? _run_find_callbacks From 1cec4e1bbaba786aa4ea70a0e2b6ad6f15ec1e68 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 26 Jun 2018 11:53:59 -0700 Subject: [PATCH 3/3] Fix documentation based on feedback --- activerecord/lib/active_record/persistence.rb | 8 ++++++-- activerecord/lib/active_record/result.rb | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 72d92ef504..155d67fd8f 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -70,8 +70,12 @@ module ActiveRecord instantiate_instance_of(klass, attributes, column_types, &block) end - # Given a class, an attributes hash, +instantiate+ returns a new instance - # of the class. Accepts only keys as strings. + # Given a class, an attributes hash, +instantiate_instance_of+ returns a + # new instance of the class. Accepts only keys as strings. + # + # This is private, don't call it. :) + # + # :nodoc: def instantiate_instance_of(klass, attributes, column_types = {}, &block) attributes = klass.attributes_builder.build_from_database(attributes, column_types) klass.allocate.init_from_db(attributes, &block) diff --git a/activerecord/lib/active_record/result.rb b/activerecord/lib/active_record/result.rb index 3549e8a3fa..7f1c2fd7eb 100644 --- a/activerecord/lib/active_record/result.rb +++ b/activerecord/lib/active_record/result.rb @@ -43,7 +43,7 @@ module ActiveRecord @column_types = column_types end - # Does this result set include the column named +name+ ? + # Returns true if this result set includes the column named +name+ def includes_column?(name) @columns.include? name end