aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2018-08-31 00:06:12 +0900
committerRyuta Kamizono <kamipo@gmail.com>2018-08-31 00:06:12 +0900
commit5e86ee1195eaf2242a5fdd35fce3790260e5e826 (patch)
tree56a10de6a669ed61c0806c2338776a71a6c0819a /activerecord
parent56ca81a9111d1a47349299e2b973160885bad767 (diff)
downloadrails-5e86ee1195eaf2242a5fdd35fce3790260e5e826.tar.gz
rails-5e86ee1195eaf2242a5fdd35fce3790260e5e826.tar.bz2
rails-5e86ee1195eaf2242a5fdd35fce3790260e5e826.zip
Refactor `attributes_for_{create,update}` to avoid an extra allocation
Use `delete_if` instead of `reject` to avoid an extra allocation.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/attribute_methods.rb6
-rw-r--r--activerecord/lib/active_record/persistence.rb2
2 files changed, 4 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb
index f657a74595..701f19a6ae 100644
--- a/activerecord/lib/active_record/attribute_methods.rb
+++ b/activerecord/lib/active_record/attribute_methods.rb
@@ -459,7 +459,8 @@ module ActiveRecord
# Filters the primary keys and readonly attributes from the attribute names.
def attributes_for_update(attribute_names)
- attribute_names.reject do |name|
+ attribute_names &= self.class.column_names
+ attribute_names.delete_if do |name|
readonly_attribute?(name)
end
end
@@ -467,7 +468,8 @@ module ActiveRecord
# Filters out the primary keys, from the attribute names, when the primary
# key is to be generated (e.g. the id attribute has no value).
def attributes_for_create(attribute_names)
- attribute_names.reject do |name|
+ attribute_names &= self.class.column_names
+ attribute_names.delete_if do |name|
pk_attribute?(name) && id.nil?
end
end
diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb
index 215b59f182..24af56cf0b 100644
--- a/activerecord/lib/active_record/persistence.rb
+++ b/activerecord/lib/active_record/persistence.rb
@@ -718,7 +718,6 @@ module ActiveRecord
# Updates the associated record with values matching those of the instance attributes.
# Returns the number of affected rows.
def _update_record(attribute_names = self.attribute_names)
- attribute_names &= self.class.column_names
attribute_names = attributes_for_update(attribute_names)
if attribute_names.empty?
@@ -737,7 +736,6 @@ module ActiveRecord
# Creates a record with values matching those of the instance attributes
# and returns its id.
def _create_record(attribute_names = self.attribute_names)
- attribute_names &= self.class.column_names
attribute_names = attributes_for_create(attribute_names)
new_id = self.class._insert_record(