aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/deduplicable.rb
diff options
context:
space:
mode:
authorJean Boussier <jean.boussier@gmail.com>2019-04-08 13:10:15 +0200
committerJean Boussier <jean.boussier@gmail.com>2019-06-03 13:31:42 +0200
commit17acb771d815f030ac1cf36b1af4050f02816c39 (patch)
tree7ebab580a70c36274c18011546e357f532f7b0a5 /activerecord/lib/active_record/connection_adapters/deduplicable.rb
parenteece0bf1087dc2cb59605a5a9beb88c6b935c194 (diff)
downloadrails-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/deduplicable.rb')
-rw-r--r--activerecord/lib/active_record/connection_adapters/deduplicable.rb29
1 files changed, 29 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/deduplicable.rb b/activerecord/lib/active_record/connection_adapters/deduplicable.rb
new file mode 100644
index 0000000000..fb2fd60bbc
--- /dev/null
+++ b/activerecord/lib/active_record/connection_adapters/deduplicable.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+module ActiveRecord
+ module ConnectionAdapters # :nodoc:
+ module Deduplicable
+ extend ActiveSupport::Concern
+
+ module ClassMethods
+ def registry
+ @registry ||= {}
+ end
+
+ def new(*)
+ super.deduplicate
+ end
+ end
+
+ def deduplicate
+ self.class.registry[self] ||= deduplicated
+ end
+ alias :-@ :deduplicate
+
+ private
+ def deduplicated
+ freeze
+ end
+ end
+ end
+end