mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
7834363bbf
Redundant `to_s` has a few overhead. Especially private methods are not intend to be passed user input directly so it should be passed always string. Removing redundant `to_s` makes attribute methods about 10% faster. ```ruby ActiveRecord::Schema.define do create_table :users, force: true do |t| end end class User < ActiveRecord::Base def fast_read_attribute(attr_name, &block) @attributes.fetch_value(attr_name, &block) end end user = User.create! Benchmark.ips do |x| x.report("user._read_attribute('id')") { user._read_attribute("id") } x.report("user.fast_read_attribute('id')") { user.fast_read_attribute("id") } end ``` ``` Warming up -------------------------------------- user._read_attribute('id') 272.151k i/100ms user.fast_read_attribute('id') 283.518k i/100ms Calculating ------------------------------------- user._read_attribute('id') 2.699M (± 1.3%) i/s - 13.608M in 5.042846s user.fast_read_attribute('id') 2.988M (± 1.2%) i/s - 15.026M in 5.029056s ``` |
||
---|---|---|
.. | ||
active_model | ||
active_model.rb |