Skip to content

Commit 79ba069

Browse files
committed
More less and lazy allocation for assign_attributes and _assign_attributes
https://gist.github.com/kamipo/296b9dd97f690d7960fdfe8de3ca08c7 Before: ``` ================================= All symbols ================================== Warming up -------------------------------------- assign_attributes 4.425k i/100ms Calculating ------------------------------------- assign_attributes 44.983k (± 2.8%) i/s - 225.675k in 5.020906s ==================================== Mixed ===================================== Warming up -------------------------------------- assign_attributes 4.546k i/100ms Calculating ------------------------------------- assign_attributes 46.103k (± 5.2%) i/s - 231.846k in 5.046489s ================================= All strings ================================== Warming up -------------------------------------- assign_attributes 4.511k i/100ms Calculating ------------------------------------- assign_attributes 47.931k (± 7.4%) i/s - 239.083k in 5.023971s ``` After: ``` ================================= All symbols ================================== Warming up -------------------------------------- assign_attributes 4.831k i/100ms Calculating ------------------------------------- assign_attributes 48.206k (± 8.5%) i/s - 241.550k in 5.058372s ==================================== Mixed ===================================== Warming up -------------------------------------- assign_attributes 4.960k i/100ms Calculating ------------------------------------- assign_attributes 50.628k (± 3.5%) i/s - 252.960k in 5.002779s ================================= All strings ================================== Warming up -------------------------------------- assign_attributes 5.006k i/100ms Calculating ------------------------------------- assign_attributes 52.242k (± 5.5%) i/s - 260.312k in 5.001695s ```
1 parent 9028ba0 commit 79ba069

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

activerecord/lib/active_record/attribute_assignment.rb

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,24 @@ module ActiveRecord
66
module AttributeAssignment
77
include ActiveModel::AttributeAssignment
88

9-
def assign_attributes(attributes)
10-
super(attributes.dup)
11-
end
12-
13-
alias attributes= assign_attributes
14-
159
private
1610
def _assign_attributes(attributes)
17-
multi_parameter_attributes = {}
18-
nested_parameter_attributes = {}
11+
multi_parameter_attributes = nested_parameter_attributes = nil
1912

2013
attributes.each do |k, v|
2114
key = k.to_s
2215

2316
if key.include?("(")
24-
multi_parameter_attributes[key] = attributes.delete(k)
17+
(multi_parameter_attributes ||= {})[key] = v
2518
elsif v.is_a?(Hash)
26-
nested_parameter_attributes[key] = attributes.delete(k)
19+
(nested_parameter_attributes ||= {})[key] = v
20+
else
21+
_assign_attribute(key, v)
2722
end
2823
end
29-
super(attributes)
3024

31-
assign_nested_parameter_attributes(nested_parameter_attributes) unless nested_parameter_attributes.empty?
32-
assign_multiparameter_attributes(multi_parameter_attributes) unless multi_parameter_attributes.empty?
25+
assign_nested_parameter_attributes(nested_parameter_attributes) if nested_parameter_attributes
26+
assign_multiparameter_attributes(multi_parameter_attributes) if multi_parameter_attributes
3327
end
3428

3529
# Assign any deferred nested attributes after the base attributes have been set.

0 commit comments

Comments
 (0)