From ffc9ed3d3b59dd76cfe3fc1ff6913bd1d2aaf462 Mon Sep 17 00:00:00 2001 From: Eugene Kenny Date: Sun, 21 Oct 2018 13:58:45 +0200 Subject: 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. --- activerecord/lib/active_record/attribute_methods/read.rb | 7 +++---- activerecord/lib/active_record/attribute_methods/write.rb | 7 +++---- 2 files changed, 6 insertions(+), 8 deletions(-) (limited to 'activerecord/lib/active_record/attribute_methods') 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 -- cgit v1.2.3