aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/core.rb
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2018-06-25 15:52:59 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2018-06-25 16:00:04 -0700
commit7d58fa87ef27712fc3dcd5a098d751a67563c9de (patch)
treebad3f305b734ccab8297fc34493857b7dea76aac /activerecord/lib/active_record/core.rb
parent9858811825cff98ee2adf81b1a9937118505fa08 (diff)
downloadrails-7d58fa87ef27712fc3dcd5a098d751a67563c9de.tar.gz
rails-7d58fa87ef27712fc3dcd5a098d751a67563c9de.tar.bz2
rails-7d58fa87ef27712fc3dcd5a098d751a67563c9de.zip
Speed up homogeneous AR lists / reduce allocations
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 ```
Diffstat (limited to 'activerecord/lib/active_record/core.rb')
-rw-r--r--activerecord/lib/active_record/core.rb20
1 files changed, 20 insertions, 0 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
@@ -350,6 +350,26 @@ module ActiveRecord
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.
# That means that modifying attributes of the clone will modify the original, since they will both point to the