diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2019-06-02 19:32:27 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-06-02 19:32:27 +0900 |
commit | f7396f98be724e02d4d241fc98c4af8f225f7307 (patch) | |
tree | 7d409559e6a5bf45a2acb7a996490ee3b2cb10c5 /activerecord/lib | |
parent | 9374c5bc75da566ef6653f058eec052e80de772c (diff) | |
parent | 613060d106f2d3bf7350bab540c952b1567ad66b (diff) | |
download | rails-f7396f98be724e02d4d241fc98c4af8f225f7307.tar.gz rails-f7396f98be724e02d4d241fc98c4af8f225f7307.tar.bz2 rails-f7396f98be724e02d4d241fc98c4af8f225f7307.zip |
Merge pull request #36375 from kamipo/fast_save
Avoid making extra 5 arrays in each `save`
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/timestamp.rb | 42 |
1 files changed, 26 insertions, 16 deletions
diff --git a/activerecord/lib/active_record/timestamp.rb b/activerecord/lib/active_record/timestamp.rb index 04a1c03474..a5862ae06b 100644 --- a/activerecord/lib/active_record/timestamp.rb +++ b/activerecord/lib/active_record/timestamp.rb @@ -59,19 +59,26 @@ module ActiveRecord attribute_names.index_with(time || current_time_from_proper_timezone) end - private - def timestamp_attributes_for_create_in_model - timestamp_attributes_for_create.select { |c| column_names.include?(c) } - end + def timestamp_attributes_for_create_in_model + @timestamp_attributes_for_create_in_model ||= + (timestamp_attributes_for_create & column_names).freeze + end - def timestamp_attributes_for_update_in_model - timestamp_attributes_for_update.select { |c| column_names.include?(c) } - end + def timestamp_attributes_for_update_in_model + @timestamp_attributes_for_update_in_model ||= + (timestamp_attributes_for_update & column_names).freeze + end - def all_timestamp_attributes_in_model - timestamp_attributes_for_create_in_model + timestamp_attributes_for_update_in_model - end + def all_timestamp_attributes_in_model + @all_timestamp_attributes_in_model ||= + (timestamp_attributes_for_create_in_model + timestamp_attributes_for_update_in_model).freeze + end + def current_time_from_proper_timezone + default_timezone == :utc ? Time.now.utc : Time.now + end + + private def timestamp_attributes_for_create ["created_at", "created_on"] end @@ -80,8 +87,11 @@ module ActiveRecord ["updated_at", "updated_on"] end - def current_time_from_proper_timezone - default_timezone == :utc ? Time.now.utc : Time.now + def reload_schema_from_cache + @timestamp_attributes_for_create_in_model = nil + @timestamp_attributes_for_update_in_model = nil + @all_timestamp_attributes_in_model = nil + super end end @@ -124,19 +134,19 @@ module ActiveRecord end def timestamp_attributes_for_create_in_model - self.class.send(:timestamp_attributes_for_create_in_model) + self.class.timestamp_attributes_for_create_in_model end def timestamp_attributes_for_update_in_model - self.class.send(:timestamp_attributes_for_update_in_model) + self.class.timestamp_attributes_for_update_in_model end def all_timestamp_attributes_in_model - self.class.send(:all_timestamp_attributes_in_model) + self.class.all_timestamp_attributes_in_model end def current_time_from_proper_timezone - self.class.send(:current_time_from_proper_timezone) + self.class.current_time_from_proper_timezone end def max_updated_column_timestamp |