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 ``` |
||
---|---|---|
.. | ||
attribute | ||
attribute_set | ||
locale | ||
serializers | ||
type | ||
validations | ||
attribute.rb | ||
attribute_assignment.rb | ||
attribute_methods.rb | ||
attribute_mutation_tracker.rb | ||
attribute_set.rb | ||
attributes.rb | ||
callbacks.rb | ||
conversion.rb | ||
dirty.rb | ||
error.rb | ||
errors.rb | ||
forbidden_attributes_protection.rb | ||
gem_version.rb | ||
lint.rb | ||
model.rb | ||
naming.rb | ||
nested_error.rb | ||
railtie.rb | ||
secure_password.rb | ||
serialization.rb | ||
translation.rb | ||
type.rb | ||
validations.rb | ||
validator.rb | ||
version.rb |