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: Incorrect memoization in `ActiveRecord::Associations::Preloader… #30564

Merged
merged 1 commit into from Sep 11, 2017

Conversation

Projects
None yet
2 participants
@tgxworld
Contributor

tgxworld commented Sep 11, 2017

…::Association`.

require 'active_record'
require 'benchmark/ips'

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)

100.times do
  Topic.create!(title: 'This is a topic', users: users)
end

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

  x.report("preload") do
    User.includes(:topic).all.to_a
  end
end
Calculating -------------------------------------
             preload    25.000  i/100ms
-------------------------------------------------
             preload    251.772  (± 1.2%) i/s -      2.525k
Calculating -------------------------------------
             preload    26.000  i/100ms
-------------------------------------------------
             preload    270.392  (± 1.1%) i/s -      2.704k
PERF: Incorrect memoization in `ActiveRecord::Associations::Preloader…
…::Association`.

```
require 'active_record'
require 'benchmark/ips'

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)

100.times do
  Topic.create!(title: 'This is a topic', users: users)
end

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

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

```
Calculating -------------------------------------
             preload    25.000  i/100ms
-------------------------------------------------
             preload    251.772  (± 1.2%) i/s -      2.525k
```

```
Calculating -------------------------------------
             preload    26.000  i/100ms
-------------------------------------------------
             preload    270.392  (± 1.1%) i/s -      2.704k
```

@kamipo kamipo merged commit 80573a0 into rails:master Sep 11, 2017

2 checks passed

codeclimate All good!
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

@tgxworld tgxworld deleted the tgxworld:fix_incorrect_memoization branch Sep 11, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment