aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/timestamp.rb
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2017-01-01 06:49:46 +0900
committerRyuta Kamizono <kamipo@gmail.com>2017-01-01 07:07:45 +0900
commit2b5dacb43dd92e98e1fd240a80c2a540ed380257 (patch)
treea0084a681b534ad82f61f7bb64bd2565daa217e3 /activerecord/lib/active_record/timestamp.rb
parente632c2fa4cb60072a778ce95c952a0fa95e5b074 (diff)
downloadrails-2b5dacb43dd92e98e1fd240a80c2a540ed380257.tar.gz
rails-2b5dacb43dd92e98e1fd240a80c2a540ed380257.tar.bz2
rails-2b5dacb43dd92e98e1fd240a80c2a540ed380257.zip
Change `timestamp_attributes_for_{create,update}` from symbol to string
`timestamp_attributes_for_{create,update}` is defined as symbol but always used as string with `to_s`. This allocates extra strings. To avoid extra allocation, change the definitions from symbol to string. ```ruby pp ObjectSpace::AllocationTracer.trace { 1_000.times { |i| Post.create! } } ``` Before: ``` ["~/rails/activerecord/lib/active_record/timestamp.rb", 121]=>[1002, 0, 750, 0, 1, 18528], ["~/rails/activerecord/lib/active_record/timestamp.rb", 105]=>[1002, 0, 750, 0, 1, 7720], ["~/rails/activerecord/lib/active_record/timestamp.rb", 101]=>[1002, 0, 750, 0, 1, 7720], ["~/rails/activerecord/lib/active_record/timestamp.rb", 109]=>[1002, 0, 750, 0, 1, 13896], ["~/rails/activerecord/lib/active_record/timestamp.rb", 61]=>[4008, 0, 3000, 0, 1, 30880], ``` After: ``` ["~/rails/activerecord/lib/active_record/timestamp.rb", 120]=>[1000, 0, 756, 0, 1, 17184], ["~/rails/activerecord/lib/active_record/timestamp.rb", 104]=>[1000, 0, 756, 0, 1, 7160], ["~/rails/activerecord/lib/active_record/timestamp.rb", 100]=>[1000, 0, 756, 0, 1, 7160], ["~/rails/activerecord/lib/active_record/timestamp.rb", 108]=>[1000, 0, 756, 0, 1, 12888], ```
Diffstat (limited to 'activerecord/lib/active_record/timestamp.rb')
-rw-r--r--activerecord/lib/active_record/timestamp.rb11
1 files changed, 5 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/timestamp.rb b/activerecord/lib/active_record/timestamp.rb
index 63100e38a1..030fc35e2f 100644
--- a/activerecord/lib/active_record/timestamp.rb
+++ b/activerecord/lib/active_record/timestamp.rb
@@ -1,3 +1,4 @@
+# frozen_string_literal: true
module ActiveRecord
# = Active Record \Timestamp
#
@@ -58,7 +59,6 @@ module ActiveRecord
current_time = current_time_from_proper_timezone
all_timestamp_attributes.each do |column|
- column = column.to_s
if has_attribute?(column) && !attribute_present?(column)
write_attribute(column, current_time)
end
@@ -73,7 +73,6 @@ module ActiveRecord
current_time = current_time_from_proper_timezone
timestamp_attributes_for_update_in_model.each do |column|
- column = column.to_s
next if will_save_change_to_attribute?(column)
write_attribute(column, current_time)
end
@@ -86,11 +85,11 @@ module ActiveRecord
end
def timestamp_attributes_for_create_in_model
- timestamp_attributes_for_create.select { |c| self.class.column_names.include?(c.to_s) }
+ timestamp_attributes_for_create.select { |c| self.class.column_names.include?(c) }
end
def timestamp_attributes_for_update_in_model
- timestamp_attributes_for_update.select { |c| self.class.column_names.include?(c.to_s) }
+ timestamp_attributes_for_update.select { |c| self.class.column_names.include?(c) }
end
def all_timestamp_attributes_in_model
@@ -98,11 +97,11 @@ module ActiveRecord
end
def timestamp_attributes_for_update
- [:updated_at, :updated_on]
+ ["updated_at", "updated_on"]
end
def timestamp_attributes_for_create
- [:created_at, :created_on]
+ ["created_at", "created_on"]
end
def all_timestamp_attributes