aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/result_test.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/test/cases/result_test.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/test/cases/result_test.rb')
-rw-r--r--activerecord/test/cases/result_test.rb5
1 files changed, 5 insertions, 0 deletions
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