1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activemodel/lib/active_model
Ryuta Kamizono 7834363bbf 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
```
2020-06-04 09:02:53 +09:00
..
attribute Active Model: Use private attr_reader 2018-02-26 06:16:24 +09:00
attribute_set Remove redundant squish for single line message 2020-05-06 15:08:06 +09:00
locale Move the validate! method to ActiveModel::Validations. 2015-02-20 16:05:26 -02:00
serializers Improve doc for :root option in as_json() [ci skip] 2019-05-13 15:35:50 +05:30
type Don't call ruby2_keywords for user supplied block 2020-06-04 02:19:30 +09:00
validations Reject signed hexadecimal numbers while validating numericality 2020-04-22 12:15:57 +00:00
attribute.rb Fix update with dirty locking column to not match latest object accidentally 2020-05-18 09:46:09 +09:00
attribute_assignment.rb Change safe guard to check for each_pair instead of stringify_keys 2020-02-07 11:36:35 -05:00
attribute_methods.rb undef_method can take varargs 2020-05-08 23:58:01 +09:00
attribute_mutation_tracker.rb PERF: 2x ~ 30x faster dirty tracking 2019-04-11 16:30:40 +09:00
attribute_set.rb Generalize FrozenError on write attribute 2020-01-29 11:23:41 +09:00
attributes.rb Avoid redundant to_s in internal attribute API 2020-06-04 09:02:53 +09:00
callbacks.rb Enable Layout/EmptyLinesAroundAccessModifier cop 2019-06-13 12:00:45 +09:00
conversion.rb Add Style/RedundantFreeze to remove redudant .freeze 2018-09-29 07:18:44 +00:00
dirty.rb Promote clear_attribute_change as attribute methods 2020-06-04 04:17:17 +09:00
error.rb Make inspect look more like regular Object#inspect 2020-05-29 21:53:35 +02:00
errors.rb Reference updated errors attribute names method in deprecation warning 2020-04-22 09:08:42 -07:00
forbidden_attributes_protection.rb Use frozen string literal in activemodel/ 2017-07-16 20:11:16 +03:00
gem_version.rb Start Rails 6.1 development 2019-04-24 15:57:14 -04:00
lint.rb Fix spec 2019-03-31 22:59:12 +08:00
model.rb Use frozen string literal in activemodel/ 2017-07-16 20:11:16 +03:00
naming.rb Passing in a Hash instance as kwargs parameters requires the "double splat" prefix 2019-09-04 12:45:27 +09:00
nested_error.rb Document on ActiveModel::Errors changes 2020-01-14 23:55:09 +08:00
railtie.rb Move the ActiveModel:Errors#full_message method to the Error class: 2019-07-16 14:28:38 +02:00
secure_password.rb Use try only when we're unsure if the receiver would respond_to the method 2019-08-01 17:58:00 +09:00
serialization.rb Optimise serializable_hash when options are empty 2020-05-22 00:04:31 +01:00
translation.rb Passing in a Hash instance as kwargs parameters requires the "double splat" prefix 2019-09-04 12:45:27 +09:00
type.rb Fix doc typo [ci skip] 2017-12-12 16:34:19 -05:00
validations.rb Prefer no allocation start/end_with? over String#[] == 2020-05-29 10:20:13 +09:00
validator.rb update from PR #36222 2020-02-12 13:31:43 -05:00
version.rb Use frozen string literal in activemodel/ 2017-07-16 20:11:16 +03:00