diff options
author | Jean Boussier <jean.boussier@gmail.com> | 2019-04-08 13:10:15 +0200 |
---|---|---|
committer | Jean Boussier <jean.boussier@gmail.com> | 2019-06-03 13:31:42 +0200 |
commit | 17acb771d815f030ac1cf36b1af4050f02816c39 (patch) | |
tree | 7ebab580a70c36274c18011546e357f532f7b0a5 /activerecord/lib/active_record/connection_adapters/column.rb | |
parent | eece0bf1087dc2cb59605a5a9beb88c6b935c194 (diff) | |
download | rails-17acb771d815f030ac1cf36b1af4050f02816c39.tar.gz rails-17acb771d815f030ac1cf36b1af4050f02816c39.tar.bz2 rails-17acb771d815f030ac1cf36b1af4050f02816c39.zip |
Deduplicate various Active Record schema cache structures
Real world database schemas contain a lot of duplicated data.
Some column names like `id`, `created_at` etc can easily be repeated
hundreds of times. Same for SqlTypeMetada, most database will contain
only a limited number of possible combinations.
This result in a lot of wasted memory.
The idea here is to make these data sctructures immutable, use a registry
to substitute similar instances with pre-existing ones.
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/column.rb')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/column.rb | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/column.rb b/activerecord/lib/active_record/connection_adapters/column.rb index 279d0b9e84..2708d2756b 100644 --- a/activerecord/lib/active_record/connection_adapters/column.rb +++ b/activerecord/lib/active_record/connection_adapters/column.rb @@ -5,6 +5,8 @@ module ActiveRecord module ConnectionAdapters # An abstract definition of a column in a table. class Column + include Deduplicable + attr_reader :name, :default, :sql_type_metadata, :null, :default_function, :collation, :comment delegate :precision, :scale, :limit, :type, :sql_type, to: :sql_type_metadata, allow_nil: true @@ -76,6 +78,7 @@ module ActiveRecord def hash Column.hash ^ name.hash ^ + name.encoding.hash ^ default.hash ^ sql_type_metadata.hash ^ null.hash ^ @@ -83,6 +86,17 @@ module ActiveRecord collation.hash ^ comment.hash end + + private + def deduplicated + @name = -name + @sql_type_metadata = sql_type_metadata.deduplicate if sql_type_metadata + @default = -default if default + @default_function = -default_function if default_function + @collation = -collation if collation + @comment = -comment if comment + super + end end class NullColumn < Column |