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 d29d459897 Avoid redundant time.getutc call if it is already utc time object
Currently `type.serialize` and `connection.{quote|type_cast}` for a time
object always does `time.getutc` call regardless of whether it is
already utc time object or not, that duplicated proccess
(`connection.type_cast(type.serialize(time))`) allocates extra/useless
time objects for each type casting.

This avoids that redundant `time.getutc` call if it is already utc time
object. In the case of a model has timestamps (`created_at` and
`updated_at`), it avoids 6,000 time objects allocation for 1,000 times
`model.save`.

```ruby
ObjectSpace::AllocationTracer.setup(%i{path line type})

pp ObjectSpace::AllocationTracer.trace {
  1_000.times { User.create }
}.select { |k, _| k[0].end_with?("quoting.rb", "time_value.rb") }
```

Before (c104bfe424):

```
{["~/rails/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb",
  203,
  :T_ARRAY]=>[1004, 0, 778, 0, 1, 0],
 ["~/rails/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb",
  220,
  :T_STRING]=>[2, 0, 2, 1, 1, 0],
 ["~/rails/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb",
  209,
  :T_ARRAY]=>[8, 0, 8, 1, 1, 0],
 ["~/rails/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb",
  57,
  :T_ARRAY]=>[4, 0, 4, 1, 1, 0],
 ["~/rails/activemodel/lib/active_model/type/helpers/time_value.rb",
  17,
  :T_DATA]=>[4000, 0, 3096, 0, 1, 0],
 ["~/rails/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb",
  120,
  :T_DATA]=>[2000, 0, 1548, 0, 1, 0],
 ["~/rails/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb",
  126,
  :T_STRING]=>[4000, 0, 3096, 0, 1, 0]}
```

After (this change):

```
{["~/rails/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb",
  203,
  :T_ARRAY]=>[1004, 0, 823, 0, 1, 0],
 ["~/rails/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb",
  220,
  :T_STRING]=>[2, 0, 2, 1, 1, 0],
 ["~/rails/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb",
  209,
  :T_ARRAY]=>[8, 0, 8, 1, 1, 0],
 ["~/rails/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb",
  57,
  :T_ARRAY]=>[4, 0, 4, 1, 1, 0],
 ["~/rails/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb",
  126,
  :T_STRING]=>[2000, 0, 1638, 0, 1, 0]}
```
2019-06-18 01:03:21 +09:00
..
attribute Active Model: Use private attr_reader 2018-02-26 06:16:24 +09:00
attribute_set Active Model: Use private attr_reader 2018-02-26 06:16:24 +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 Avoid redundant time.getutc call if it is already utc time object 2019-06-18 01:03:21 +09:00
validations Enable Layout/EmptyLinesAroundAccessModifier cop 2019-06-13 12:00:45 +09:00
attribute.rb update_columns raises if the column is unknown 2018-10-30 12:52:23 -06:00
attribute_assignment.rb Enable Layout/EmptyLinesAroundAccessModifier cop 2019-06-13 12:00:45 +09:00
attribute_methods.rb Update comment in attribute_method_matchers_matching 2019-04-23 14:24:40 +09:00
attribute_mutation_tracker.rb PERF: 2x ~ 30x faster dirty tracking 2019-04-11 16:30:40 +09:00
attribute_set.rb Enable Layout/EmptyLinesAroundAccessModifier cop 2019-06-13 12:00:45 +09:00
attributes.rb Enable Layout/EmptyLinesAroundAccessModifier cop 2019-06-13 12:00:45 +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 Improve wording of comments 2019-04-13 11:12:39 +09:00
error.rb Enable Layout/EmptyLinesAroundAccessModifier cop 2019-06-13 12:00:45 +09:00
errors.rb Enable Layout/EmptyLinesAroundAccessModifier cop 2019-06-13 12:00:45 +09: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 Enable Layout/EmptyLinesAroundAccessModifier cop 2019-06-13 12:00:45 +09:00
nested_error.rb Nested attribute error's attribute name to use different key: 2019-03-31 22:59:12 +08:00
railtie.rb Rename i18n_full_message config option to i18n_customize_full_message 2019-03-29 21:38:48 +05:30
secure_password.rb Refactor has_secure_password to extract dedicated attribute module 2019-04-05 01:55:00 +09:00
serialization.rb Enable Layout/EmptyLinesAroundAccessModifier cop 2019-06-13 12:00:45 +09:00
translation.rb Use frozen string literal in activemodel/ 2017-07-16 20:11:16 +03:00
type.rb Fix doc typo [ci skip] 2017-12-12 16:34:19 -05:00
validations.rb Enable Layout/EmptyLinesAroundAccessModifier cop 2019-06-13 12:00:45 +09:00
validator.rb Enable Layout/EmptyLinesAroundAccessModifier cop 2019-06-13 12:00:45 +09:00
version.rb Use frozen string literal in activemodel/ 2017-07-16 20:11:16 +03:00