From 6f26e99cef4b6f20be8ef5cf7fee4b4755baaaec Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Fri, 5 Apr 2019 14:45:05 +0200 Subject: Improve == and hash methods on various schema cache structs to be allocation free. The previous implementation would allocate 2 arrays per comparisons. I tried relying on Struct, but they do allocate one Hash inside `Struct#hash`. --- .../lib/active_record/connection_adapters/column.rb | 21 +++++++++++++-------- .../connection_adapters/mysql/type_metadata.rb | 13 +++++-------- .../connection_adapters/postgresql/type_metadata.rb | 17 +++++++++-------- .../connection_adapters/sql_type_metadata.rb | 19 +++++++++++-------- 4 files changed, 38 insertions(+), 32 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/connection_adapters/column.rb b/activerecord/lib/active_record/connection_adapters/column.rb index 8c294ccf5a..21c5e43ea0 100644 --- a/activerecord/lib/active_record/connection_adapters/column.rb +++ b/activerecord/lib/active_record/connection_adapters/column.rb @@ -63,19 +63,24 @@ module ActiveRecord def ==(other) other.is_a?(Column) && - attributes_for_hash == other.attributes_for_hash + name == other.name && + default == other.default && + sql_type_metadata == other.sql_type_metadata && + null == other.null && + default_function == other.default_function && + collation == other.collation end alias :eql? :== def hash - attributes_for_hash.hash + Column.hash ^ + name.hash ^ + default.hash ^ + sql_type_metadata.hash ^ + null.hash ^ + default_function.hash ^ + collation.hash end - - protected - - def attributes_for_hash - [self.class, name, default, sql_type_metadata, null, default_function, collation, comment] - end end class NullColumn < Column diff --git a/activerecord/lib/active_record/connection_adapters/mysql/type_metadata.rb b/activerecord/lib/active_record/connection_adapters/mysql/type_metadata.rb index 7ad0944d51..56479f27bf 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql/type_metadata.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql/type_metadata.rb @@ -16,19 +16,16 @@ module ActiveRecord def ==(other) other.is_a?(MySQL::TypeMetadata) && - attributes_for_hash == other.attributes_for_hash + __getobj__ == other.__getobj__ && + extra == other.extra end alias eql? == def hash - attributes_for_hash.hash + TypeMetadata.hash ^ + __getobj__.hash ^ + extra.hash end - - protected - - def attributes_for_hash - [self.class, @type_metadata, extra] - end end end end 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 cd69d28139..403b3ead98 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/type_metadata.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/type_metadata.rb @@ -22,19 +22,20 @@ module ActiveRecord def ==(other) other.is_a?(PostgreSQLTypeMetadata) && - attributes_for_hash == other.attributes_for_hash + __getobj__ == other.__getobj__ && + oid == other.oid && + fmod == other.fmod && + array == other.array end alias eql? == def hash - attributes_for_hash.hash + PostgreSQLTypeMetadata.hash ^ + __getobj__.hash ^ + oid.hash ^ + fmod.hash ^ + array.hash end - - protected - - def attributes_for_hash - [self.class, @type_metadata, oid, fmod] - end end end end diff --git a/activerecord/lib/active_record/connection_adapters/sql_type_metadata.rb b/activerecord/lib/active_record/connection_adapters/sql_type_metadata.rb index 8489bcbf1d..df28df7a7c 100644 --- a/activerecord/lib/active_record/connection_adapters/sql_type_metadata.rb +++ b/activerecord/lib/active_record/connection_adapters/sql_type_metadata.rb @@ -16,19 +16,22 @@ module ActiveRecord def ==(other) other.is_a?(SqlTypeMetadata) && - attributes_for_hash == other.attributes_for_hash + sql_type == other.sql_type && + type == other.type && + limit == other.limit && + precision == other.precision && + scale == other.scale end alias eql? == def hash - attributes_for_hash.hash + SqlTypeMetadata.hash ^ + sql_type.hash ^ + type.hash ^ + limit.hash ^ + precision.hash >> 1 ^ + scale.hash >> 2 end - - protected - - def attributes_for_hash - [self.class, sql_type, type, limit, precision, scale] - end end end end -- cgit v1.2.3