aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/attribute_methods
diff options
context:
space:
mode:
authorEugene Kenny <elkenny@gmail.com>2018-10-21 13:58:45 +0200
committerEugene Kenny <elkenny@gmail.com>2018-10-21 13:58:45 +0200
commitffc9ed3d3b59dd76cfe3fc1ff6913bd1d2aaf462 (patch)
tree4af949d4f1c09d9944d25770cf38c7c7f2214b3f /activerecord/lib/active_record/attribute_methods
parent26a202a91b5a4754be5e3e137c390786ec977e56 (diff)
downloadrails-ffc9ed3d3b59dd76cfe3fc1ff6913bd1d2aaf462.tar.gz
rails-ffc9ed3d3b59dd76cfe3fc1ff6913bd1d2aaf462.tar.bz2
rails-ffc9ed3d3b59dd76cfe3fc1ff6913bd1d2aaf462.zip
Reduce string allocations in read/write_attribute
When `attr_name` is passed as a symbol, it's currently converted to a string by `attribute_alias?`, and potentially also `attribute_alias`, as well as by the `read_attribute`/`write_attribute` method itself. By converting `attr_name` to a string up front, the extra allocations related to attribute aliases can be avoided.
Diffstat (limited to 'activerecord/lib/active_record/attribute_methods')
-rw-r--r--activerecord/lib/active_record/attribute_methods/read.rb7
-rw-r--r--activerecord/lib/active_record/attribute_methods/write.rb7
2 files changed, 6 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/attribute_methods/read.rb b/activerecord/lib/active_record/attribute_methods/read.rb
index ff0e863529..6e1275e990 100644
--- a/activerecord/lib/active_record/attribute_methods/read.rb
+++ b/activerecord/lib/active_record/attribute_methods/read.rb
@@ -29,10 +29,9 @@ module ActiveRecord
# it has been typecast (for example, "2004-12-12" in a date column is cast
# to a date object, like Date.new(2004, 12, 12)).
def read_attribute(attr_name, &block)
- name = if self.class.attribute_alias?(attr_name)
- self.class.attribute_alias(attr_name).to_s
- else
- attr_name.to_s
+ name = attr_name.to_s
+ if self.class.attribute_alias?(name)
+ name = self.class.attribute_alias(name)
end
primary_key = self.class.primary_key
diff --git a/activerecord/lib/active_record/attribute_methods/write.rb b/activerecord/lib/active_record/attribute_methods/write.rb
index 7f246eed46..455e67e19b 100644
--- a/activerecord/lib/active_record/attribute_methods/write.rb
+++ b/activerecord/lib/active_record/attribute_methods/write.rb
@@ -33,10 +33,9 @@ module ActiveRecord
# specified +value+. Empty strings for Integer and Float columns are
# turned into +nil+.
def write_attribute(attr_name, value)
- name = if self.class.attribute_alias?(attr_name)
- self.class.attribute_alias(attr_name).to_s
- else
- attr_name.to_s
+ name = attr_name.to_s
+ if self.class.attribute_alias?(name)
+ name = self.class.attribute_alias(name)
end
primary_key = self.class.primary_key