Skip to content

Commit

Permalink
Merge pull request rails#53337 from Shopify/opt-attr-dup
Browse files Browse the repository at this point in the history
ActiveModel::Attribute: elide dup for immutable types
  • Loading branch information
byroot authored Oct 16, 2024
2 parents 5465c7d + d7a3a05 commit dcbd0fa
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions activemodel/lib/active_model/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ def with_type(type)
end
end

def deep_dup # :nodoc:
if @type.mutable?
dup
else
self # If the underlying type is immutable we can get away with not duping
end
end

def type_cast(*)
raise NotImplementedError
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ def with_type(type)
self.class.new(name, user_provided_value, type, original_attribute)
end

# Can't elide dup when a default is provided.
# See Attribute#deep_dup
alias_method :deep_dup, :dup

def marshal_dump
result = [
name,
Expand Down
4 changes: 4 additions & 0 deletions activemodel/lib/active_model/type/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def to_immutable_string
)
end

def mutable? # :nodoc:
true
end

private
def cast_value(value)
case value
Expand Down

0 comments on commit dcbd0fa

Please sign in to comment.