Skip to content

Commit 46efabd

Browse files
committed
Optimize AR::Timestamp#clear_timestamp_attributes
If the attribute is already `nil` we can skip a lot of coslty work. Benchmark: https://gist.github.com/casperisfine/ae56bec1e7eecbff3a696b367e2bafa2 Before: ``` ruby 3.3.4 (2024-07-09 revision be1089c8ec) +YJIT [arm64-darwin23] Warming up -------------------------------------- ActiveRecord 6.513k i/100ms Calculating ------------------------------------- ActiveRecord 66.008k (± 1.8%) i/s (15.15 μs/i) - 332.163k in 5.033757s ``` After: ``` ruby 3.3.4 (2024-07-09 revision be1089c8ec) +YJIT [arm64-darwin23] Warming up -------------------------------------- ActiveRecord 7.021k i/100ms Calculating ------------------------------------- ActiveRecord 69.880k (± 2.1%) i/s (14.31 μs/i) - 351.050k in 5.025927s ```
1 parent 07ce474 commit 46efabd

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

activerecord/lib/active_record/timestamp.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,10 @@ def max_updated_column_timestamp
169169
# Clear attributes and changed_attributes
170170
def clear_timestamp_attributes
171171
all_timestamp_attributes_in_model.each do |attribute_name|
172-
self[attribute_name] = nil
173-
clear_attribute_change(attribute_name)
172+
if self[attribute_name]
173+
self[attribute_name] = nil
174+
clear_attribute_change(attribute_name)
175+
end
174176
end
175177
end
176178
end

0 commit comments

Comments
 (0)