Skip to content

Commit

Permalink
Merge pull request #36375 from kamipo/fast_save
Browse files Browse the repository at this point in the history
Avoid making extra 5 arrays in each `save`
  • Loading branch information
kamipo committed Jun 2, 2019
2 parents 9374c5b + 613060d commit f7396f9
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions activerecord/lib/active_record/timestamp.rb
Expand Up @@ -59,19 +59,26 @@ def touch_attributes_with_time(*names, time: nil)
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
Expand All @@ -80,8 +87,11 @@ def timestamp_attributes_for_update
["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

Expand Down Expand Up @@ -124,19 +134,19 @@ def should_record_timestamps?
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
Expand Down

0 comments on commit f7396f9

Please sign in to comment.