Skip to content

Commit 2cadcb2

Browse files
committed
Don't cast stale_state to String
I'm looking at reducing the allocations and memory footprint of Active Record models, and based on a small benchmark I saw a lot of numeric strings being created from `stale_state`. I tracked this cast all the way down to 1c07b84 but unfortunately the commit message doesn't explain why it was added. I can't think of a reason why this would be needed. The benchmark is essentially just: `Post.includes(:comments).to_a` with `100` posts and `20` comments each. ``` Total allocated: 4.69 MB (45077 objects) Total retained: 3.84 MB (29623 objects) retained objects by location ----------------------------------- ... 2000 activerecord/lib/active_record/associations/belongs_to_association.rb:152 retained memory by location ----------------------------------- ... 80.00 kB activerecord/lib/active_record/associations/belongs_to_association.rb:152 ``` NB: This break the final assertion of the Rails 6.1 Marshal backward compatibility test, but I think it's an acceptable tradeoff.
1 parent 2c79c87 commit 2cadcb2

File tree

4 files changed

+5
-6
lines changed

4 files changed

+5
-6
lines changed

activerecord/lib/active_record/associations/belongs_to_association.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ def invertible_for?(record)
148148
end
149149

150150
def stale_state
151-
result = owner._read_attribute(reflection.foreign_key) { |n| owner.send(:missing_attribute, n, caller) }
152-
result && result.to_s
151+
owner._read_attribute(reflection.foreign_key) { |n| owner.send(:missing_attribute, n, caller) }
153152
end
154153
end
155154
end

activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ def raise_on_type_mismatch!(record)
4141
end
4242

4343
def stale_state
44-
foreign_key = super
45-
foreign_key && [foreign_key.to_s, owner[reflection.foreign_type].to_s]
44+
if foreign_key = super
45+
[foreign_key, owner[reflection.foreign_type]]
46+
end
4647
end
4748
end
4849
end

activerecord/lib/active_record/associations/through_association.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def construct_join_attributes(*records)
8282
def stale_state
8383
if through_reflection.belongs_to?
8484
Array(through_reflection.foreign_key).filter_map do |foreign_key_column|
85-
owner[foreign_key_column] && owner[foreign_key_column].to_s
85+
owner[foreign_key_column]
8686
end.presence
8787
end
8888
end

activerecord/test/cases/marshal_serialization_test.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ def test_deserializing_rails_6_1_marshal_with_loaded_association_cache
3333
assert_equal "Have a nice day", topic.content
3434
assert_predicate topic.association(:replies), :loaded?
3535
assert_predicate topic.replies.first.association(:topic), :loaded?
36-
assert_same topic, topic.replies.first.topic
3736
end
3837

3938
def test_deserializing_rails_7_1_marshal_basic

0 commit comments

Comments
 (0)