Skip to content

Commit 71ffcaf

Browse files
committed
Speedup AttributeSet#deep_dup
`Hash#deep_dup` is slower because it has to check if the keys are mutable. In this case we know keys are all frozen strings so we can use `transform_values` to skip dealing with keys. Benchmark: https://gist.github.com/casperisfine/ae56bec1e7eecbff3a696b367e2bafa2 Before: ``` ruby 3.4.0preview2 (2024-10-07 master 32c733f57b) +YJIT +PRISM [arm64-darwin23] Warming up -------------------------------------- ActiveRecord 6.113k i/100ms Calculating ------------------------------------- ActiveRecord 61.941k (± 1.6%) i/s (16.14 μs/i) - 311.763k in 5.034628s ``` After: ``` ruby 3.4.0preview2 (2024-10-07 master 32c733f57b) +YJIT +PRISM [arm64-darwin23] Warming up -------------------------------------- ActiveRecord 7.323k i/100ms Calculating ------------------------------------- ActiveRecord 73.195k (± 1.0%) i/s (13.66 μs/i) - 366.150k in 5.002894s ```
1 parent aeb0828 commit 71ffcaf

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

activemodel/lib/active_model/attribute_set.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def freeze
7171
end
7272

7373
def deep_dup
74-
AttributeSet.new(attributes.deep_dup)
74+
AttributeSet.new(attributes.transform_values(&:deep_dup))
7575
end
7676

7777
def initialize_dup(_)

0 commit comments

Comments
 (0)