• R
    Avoid quite useless loop in `find_by_sql` · 71e45582
    Ryuta Kamizono 提交于
    `column_types` is empty except PostgreSQL adapter, and
    `attribute_types.each_key { |k| column_types.delete k }` is also empty
    even if PostgreSQL adapter almost all case, so that code is quite
    useless. This improves performance for `find_by_sql` to avoid that
    useless loop as much as possible.
    
    ```ruby
    ActiveRecord::Schema.define do
      create_table :active_storage_blobs do |t|
        t.string   :key,          null: false
        t.string   :filename,     null: false
        t.string   :content_type
        t.text     :metadata
        t.string   :service_name, null: false
        t.bigint   :byte_size,    null: false
        t.string   :checksum,     null: false
        t.datetime :created_at,   null: false
    
        t.index [ :key ], unique: true
      end
    end
    
    class ActiveStorageBlob < ActiveRecord::Base
    end
    
    Benchmark.ips do |x|
      x.report("find_by") { ActiveStorageBlob.find_by(id: 1) }
    end
    ```
    
    Before:
    
    ```
    Warming up --------------------------------------
                 find_by     1.256k i/100ms
    Calculating -------------------------------------
                 find_by     12.595k (± 3.4%) i/s -     64.056k in   5.091599s
    ```
    
    After:
    
    ```
    Warming up --------------------------------------
                 find_by     1.341k i/100ms
    Calculating -------------------------------------
                 find_by     13.170k (± 3.5%) i/s -     67.050k in   5.097439s
    ```
    
    To avoid column types loop for PostgreSQL adapter, this skips returning
    additional column types if a column has already been type casted by pg
    decoders. Fortunately this fixes #36186 partly for common types.
    71e45582
calculations_test.rb 39.3 KB