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/postgresql | |
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/postgresql')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/postgresql/column.rb | 13 | ||||
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/postgresql/type_metadata.rb | 8 |
2 files changed, 21 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/column.rb b/activerecord/lib/active_record/connection_adapters/postgresql/column.rb index ec25bb1e19..b4ea6c42d3 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/column.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/column.rb @@ -23,6 +23,19 @@ module ActiveRecord def sql_type super.sub(/\[\]\z/, "") end + + def ==(other) + other.is_a?(Column) && + super && + serial? == other.serial? + end + alias :eql? :== + + def hash + Column.hash ^ + super.hash ^ + serial?.hash + end end end PostgreSQLColumn = PostgreSQL::Column # :nodoc: diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/type_metadata.rb b/activerecord/lib/active_record/connection_adapters/postgresql/type_metadata.rb index 8bdec623af..b7f6479357 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/type_metadata.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/type_metadata.rb @@ -7,6 +7,8 @@ module ActiveRecord class TypeMetadata < DelegateClass(SqlTypeMetadata) undef to_yaml if method_defined?(:to_yaml) + include Deduplicable + attr_reader :oid, :fmod def initialize(type_metadata, oid: nil, fmod: nil) @@ -29,6 +31,12 @@ module ActiveRecord oid.hash ^ fmod.hash end + + private + def deduplicated + __setobj__(__getobj__.deduplicate) + super + end end end PostgreSQLTypeMetadata = PostgreSQL::TypeMetadata |