Repository navigation
Simplify attribute type decoration - #39882
Conversation
Using `decorate_matching_attribute_type` is overkill to do that.
|
|
|
We could extract a method to override during the attribute decoration phase instead: diff --git a/activerecord/lib/active_record/attribute_decorators.rb b/activerecord/lib/active_record/attribute_decorators.rb
index 0b66043d2a..32aed3329d 100644
--- a/activerecord/lib/active_record/attribute_decorators.rb
+++ b/activerecord/lib/active_record/attribute_decorators.rb
@@ -49,10 +49,14 @@ def decorate_matching_attribute_types(matcher, decorator_name, &block)
def load_schema!
super
attribute_types.each do |name, type|
- decorated_type = attribute_type_decorations.apply(name, type)
+ decorated_type = apply_attribute_type_decorations(name, type)
define_attribute(name, decorated_type)
end
end
+
+ def apply_attribute_type_decorations(name, type)
+ attribute_type_decorations.apply(name, type)
+ end
end
class TypeDecorator # :nodoc:
diff --git a/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb b/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb
index f97412b1cd..5a294305e7 100644
--- a/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb
+++ b/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb
@@ -64,7 +64,7 @@ def map_avoiding_infinite_recursion(value)
end
module ClassMethods # :nodoc:
- def define_attribute(name, cast_type, **)
+ def apply_attribute_type_decorations(name, cast_type)
if create_time_zone_conversion_attribute?(name, cast_type)
cast_type = TimeZoneConverter.new(cast_type)
end
diff --git a/activerecord/lib/active_record/locking/optimistic.rb b/activerecord/lib/active_record/locking/optimistic.rb
index 8770909086..80082f4575 100644
--- a/activerecord/lib/active_record/locking/optimistic.rb
+++ b/activerecord/lib/active_record/locking/optimistic.rb
@@ -165,7 +165,7 @@ def update_counters(id, counters)
super
end
- def define_attribute(name, cast_type, **) # :nodoc:
+ def apply_attribute_type_decorations(name, cast_type) # :nodoc:
if lock_optimistically && name == locking_column
cast_type = LockingType.new(cast_type)
end |
The internal attribute decoration is almost covered by the `attribute` with block, we don't need to have much code and extra class attribute.
7d9d785 to
d784043
Compare
|
The purpose of the simplification is to remove much extra code, extra class attribute, and extra hook point, I've extracted the first commit as an easier review part (I supposed no one disagree the smaller part). If you prefer the extraction, I can pick the diff first before the removing. |
|
As the code ( rails/activerecord/lib/active_record/attribute_decorators.rb Lines 49 to 55 in dcf545d Let me know if still have any concerns. |
|
My concern isn't how often the decoration matcher runs, but how many times the type is decorated. diff --git a/activerecord/test/cases/attributes_test.rb b/activerecord/test/cases/attributes_test.rb
index 15475d12d8..14f2ddd4d6 100644
--- a/activerecord/test/cases/attributes_test.rb
+++ b/activerecord/test/cases/attributes_test.rb
@@ -87,6 +87,7 @@ class CustomPropertiesTest < ActiveRecord::TestCase
attribute :starts_at, :datetime, precision: 3, default: -> { Time.now.utc }
end
+ assert_instance_of ActiveSupport::Type::DateTime, klass.type_for_attribute(:starts_at).__getobj__
assert_instance_of ActiveSupport::TimeWithZone, klass.new.starts_at
end
endThe second commit resolves ☝️, but the same problem remains for user-defined timezone attributes that don't specify a type, which was the motivating example for #39830. Do you not consider this double-typecasting at runtime to be a problem? |
|
I agree that the |
I've get your point. I'll address that in a later commit.
As I said above, the purpose of the simplification is to remove much extra code, extra class attribute, and extra hook point. About runtime performance, as long as the number of invocations of decoration matchers is the same (i.e. not having user-defined attributes on the database columns), removing the extra I am missing something about the performance? rails/activerecord/lib/active_record/attribute_decorators.rb Lines 49 to 55 in dcf545d |
|
Yes, removing that stuff will definitely make defining attributes faster. By "runtime performance" I meant when the application is serving requests, rather than when it's initialising; I was thinking of the overhead of the double-typecasting. |
832f0d8 to
f83f14b
Compare
…mp attribute with implicit type If type is given as a symbol, it will lookup a fresh type object via `Type.lookup`, but if type is omitted, `type_for_attribute` will lookup the database type which is already decorated by `define_attribute`. To avoid the double decoration, skip extra decoration if its type is already decorated.
f83f14b to
e036602
Compare
|
I've addressed the double decoration e036602, it no longer have runtime overhead. |
This is an alternate take on rails#39882, based on top of rails#38871. Co-authored-by: Ryuta Kamizono <[email protected]>
| if type && !type.is_a?(Proc) | ||
| cast_type = _lookup_cast_type(attr_name, type, options) | ||
| end |
There was a problem hiding this comment.
It looks like this type.is_a?(Proc) check is here to avoid decorating with Type::Serialized twice, but there are now multiple ways that the type could be specified as a proc. How about checking the type directly:
| if type && !type.is_a?(Proc) | |
| cast_type = _lookup_cast_type(attr_name, type, options) | |
| end | |
| cast_type = _lookup_cast_type(attr_name, type, options) if type | |
| cast_type = cast_type.subtype if cast_type.is_a?(Type::Serialized) |
There was a problem hiding this comment.
Originally there is no official way to decorate with Type::Serialized twice.
Personally I'd like to remove this part entirely, but existing tests expects serialize can overlay on the user-defined attribute. This part is for passing the tests.
rails/activerecord/test/cases/json_attribute_test.rb
Lines 13 to 14 in 57daae2
rails/activerecord/test/cases/json_shared_test_cases.rb
Lines 237 to 239 in 57daae2
There was a problem hiding this comment.
Personally I'd like to remove this part entirely, but existing tests expects
serializecan overlay on the user-defined attribute. This part is for passing the tests.
In other words, we need to allow serialize on user-defined attribute with symbol for passing tests, but I'd not like to allow serialize on user-defined attribute with block which is internally used feature.
There was a problem hiding this comment.
Right, my suggestion is another way to make those tests pass. I find cast_type.is_a?(Type::Serialized) easier to understand than type.is_a?(Proc).
But it's just a preference, there's no practical benefit. I think I've delayed this PR enough. Thanks for your patience 🙇
`AttributeDecorators` was removed in rails#39882 in favor of calling the `attribute` macro with a block. However, this technique overwrote the original attribute type, which is still needed by some decorators. Thus those decorators had to bypass the encapsulation of the `Attributes` module to retrieve the original attribute type. This commit makes the `attribute` macro retain the original attribute type when a new type is not specified.
Originally #39882 is intended to remove `decorate_matching_attribute_type` but leave `decorate_attribute_type` at that point, but in the disscussion in #39882, to demonstrate removing `AttributeDecorators` module, I've removed all decoration methods. I wrote user-defined type decoration as inline, I wasn't sure that is valid (meaningful) usage (that is a reason why I wasn't in a hurry to remove that method), as I said at #39882 (comment). In #39897 and #39902, `enum` also need to support user-defined type decoration, so I've decided to add back `decorate_attribute_type` as before #39882.
Using
decorate_matching_attribute_typeis overkill to do that.