Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid redundant
to_s
in internal attribute API
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 ```
- Loading branch information
Showing
6 changed files
with
28 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters