diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2018-06-25 15:52:59 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2018-06-25 16:00:04 -0700 |
commit | 7d58fa87ef27712fc3dcd5a098d751a67563c9de (patch) | |
tree | bad3f305b734ccab8297fc34493857b7dea76aac /activestorage/test/fixtures/files | |
parent | 9858811825cff98ee2adf81b1a9937118505fa08 (diff) | |
download | rails-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 'activestorage/test/fixtures/files')
0 files changed, 0 insertions, 0 deletions