Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PERF: Restore memoization when preloading associations. #30706

Merged
merged 1 commit into from
Sep 25, 2017

Conversation

tgxworld
Copy link
Contributor

@tgxworld tgxworld commented Sep 25, 2017

Extracted from #30550

Benchmark Script

require 'active_record'
require 'benchmark/ips'
require 'ruby-prof'
require 'memory_profiler'
require 'byebug'

ActiveRecord::Base.establish_connection(ENV.fetch('DATABASE_URL'))
ActiveRecord::Migration.verbose = false

ActiveRecord::Schema.define do
  create_table :users, force: true do |t|
    t.string :name, :email
    t.integer :topic_id
    t.timestamps null: false
  end

  create_table :topics, force: true do |t|
    t.string :title
    t.timestamps null: false
  end
end

attributes = {
  name: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
  email: 'foobar@email.com'
}

class Topic < ActiveRecord::Base
  has_many :users
end

class User < ActiveRecord::Base
  belongs_to :topic
end

100.times do
  User.create!(attributes)
end

users = User.first(50)

Topic.create!(title: 'This is a topic', users: users)

Benchmark.ips do |x|
  x.config(time: 10, warmup: 5)

  x.report("preload") do
    User.includes(:topic).all.to_a
  end
end

Before

Calculating -------------------------------------
             preload    26.000  i/100ms
-------------------------------------------------
             preload    265.347  (± 3.0%) i/s -      2.652k

After

Calculating -------------------------------------
             preload    39.000  i/100ms
-------------------------------------------------
             preload    406.053  (± 1.7%) i/s -      4.095k

Benchmark Script

```
require 'active_record'
require 'benchmark/ips'
require 'ruby-prof'
require 'memory_profiler'
require 'byebug'

ActiveRecord::Base.establish_connection(ENV.fetch('DATABASE_URL'))
ActiveRecord::Migration.verbose = false

ActiveRecord::Schema.define do
  create_table :users, force: true do |t|
    t.string :name, :email
    t.integer :topic_id
    t.timestamps null: false
  end

  create_table :topics, force: true do |t|
    t.string :title
    t.timestamps null: false
  end
end

attributes = {
  name: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
  email: 'foobar@email.com'
}

class Topic < ActiveRecord::Base
  has_many :users
end

class User < ActiveRecord::Base
  belongs_to :topic
end

100.times do
  User.create!(attributes)
end

users = User.first(50)

Topic.create!(title: 'This is a topic', users: users)

Benchmark.ips do |x|
  x.config(time: 10, warmup: 5)

  x.report("preload") do
    User.includes(:topic).all.to_a
  end
end
```

Before

```
Calculating -------------------------------------
             preload    26.000  i/100ms
-------------------------------------------------
             preload    265.347  (± 3.0%) i/s -      2.652k
```

After

```
Calculating -------------------------------------
             preload    39.000  i/100ms
-------------------------------------------------
             preload    406.053  (± 1.7%) i/s -      4.095k
```
@tgxworld
Copy link
Contributor Author

r? @kamipo

@kamipo kamipo merged commit 70656c4 into rails:master Sep 25, 2017
@tgxworld tgxworld deleted the fix_preload_memoization branch September 25, 2017 14:18
kamipo added a commit that referenced this pull request Sep 26, 2017
PERF: Restore memoization when preloading associations.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants