aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorMichael Lovitt <michael@lovitt.net>2017-05-18 15:52:45 -0500
committerMichael Lovitt <michael@lovitt.net>2017-05-24 17:26:20 -0500
commit63dd12b7b83541d8a469a8e6aed1607d77f0d994 (patch)
treea1c4ce44c6b89af0a97e16007c359524a4a3fdb7 /activerecord/lib/active_record
parentbfc62febac905412cdbcb7698d5a3b3ea5167af3 (diff)
downloadrails-63dd12b7b83541d8a469a8e6aed1607d77f0d994.tar.gz
rails-63dd12b7b83541d8a469a8e6aed1607d77f0d994.tar.bz2
rails-63dd12b7b83541d8a469a8e6aed1607d77f0d994.zip
Performance optimization for ActiveRecord#column_defaults
Memoize the #column_defaults class property, as ActiveRecord does for other properties in this module. This change addresses slowness in ActiveRecord initialization introduced starting in Rails 5.0. This method's performance has not changed with Rails 5, but it is now called much more frequently than before: every time an STI model is instantiated.
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/model_schema.rb3
1 files changed, 2 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/model_schema.rb b/activerecord/lib/active_record/model_schema.rb
index 54216caaaf..aa9087fae3 100644
--- a/activerecord/lib/active_record/model_schema.rb
+++ b/activerecord/lib/active_record/model_schema.rb
@@ -377,7 +377,7 @@ module ActiveRecord
# default values when instantiating the Active Record object for this table.
def column_defaults
load_schema
- _default_attributes.to_hash
+ @column_defaults ||= _default_attributes.to_hash
end
def _default_attributes # :nodoc:
@@ -466,6 +466,7 @@ module ActiveRecord
@attribute_types = nil
@content_columns = nil
@default_attributes = nil
+ @column_defaults = nil
@inheritance_column = nil unless defined?(@explicit_inheritance_column) && @explicit_inheritance_column
@attributes_builder = nil
@columns = nil