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 ab8b12eaf6 PERF: 35% faster attributes for readonly usage
Instantiating attributes hash from raw database values is one of the
slower part of attributes.

Why that is necessary is to detect mutations. In other words, that isn't
necessary until mutations are happened.

`LazyAttributeHash` which was introduced at 0f29c21 is to instantiate
attribute lazily until first accessing the attribute (i.e.
`Model.find(1)` isn't slow yet, but `Model.find(1).attr_name` is still
slow).

This introduces `LazyAttributeSet` to instantiate attribute more lazily,
it doesn't instantiate attribute until first assigning/dirty checking
the attribute (i.e. `Model.find(1).attr_name` is no longer slow).

It makes attributes access about 35% faster for readonly (non-mutation)
usage.

https://gist.github.com/kamipo/4002c96a02859d8fe6503e26d7be4ad8

Before:

```
IPS
Warming up --------------------------------------
    attribute access     1.000  i/100ms
Calculating -------------------------------------
    attribute access      3.444  (± 0.0%) i/s -     18.000  in   5.259030s
MEMORY
Calculating -------------------------------------
    attribute access    38.902M memsize (     0.000  retained)
                       350.044k objects (     0.000  retained)
                        15.000  strings (     0.000  retained)
```

After (with `immutable_strings_by_default = true`):

```
IPS
Warming up --------------------------------------
    attribute access     1.000  i/100ms
Calculating -------------------------------------
    attribute access      4.652  (±21.5%) i/s -     23.000  in   5.034853s
MEMORY
Calculating -------------------------------------
    attribute access    27.782M memsize (     0.000  retained)
                       170.044k objects (     0.000  retained)
                        15.000  strings (     0.000  retained)
```
2020-06-13 16:01:08 +09:00
..
attribute
attribute_set PERF: 35% faster attributes for readonly usage 2020-06-13 16:01:08 +09:00
locale
serializers Improve doc for :root option in as_json() [ci skip] 2019-05-13 15:35:50 +05:30
type Fix string type cast with boolean serialization for MySQL 2020-06-13 07:50:12 +09:00
validations Reject signed hexadecimal numbers while validating numericality 2020-04-22 12:15:57 +00:00
attribute.rb PERF: 35% faster attributes for readonly usage 2020-06-13 16:01:08 +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 PERF: 15% faster attribute access 2020-06-05 09:12:21 +09:00
attributes.rb PERF: 15% faster attribute access 2020-06-05 09:12:21 +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
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
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
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