• G
    PERF: Partially recover some performance when preloading. · e03c9066
    Guo Xiang Tan 提交于
    Benchmark Script:
    ```
    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)
    
    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    40.000  i/100ms
    -------------------------------------------------
                 preload    407.962  (± 1.5%) i/s -      4.080k
    ```
    
    After:
    ```
    alculating -------------------------------------
                 preload    43.000  i/100ms
    -------------------------------------------------
                 preload    427.567  (± 1.6%) i/s -      4.300k
    ```
    e03c9066
association.rb 4.0 KB