From dafda50c6aeda10fa3f6399e34c8673ebf7cbf2c Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Fri, 14 Jan 2022 15:45:18 -0800 Subject: [PATCH] [framework] remove hashcode override for Element (#96644) --- .../flutter/lib/src/widgets/framework.dart | 23 +------------------ .../flutter/test/widgets/framework_test.dart | 15 ++++++++++++ 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/packages/flutter/lib/src/widgets/framework.dart b/packages/flutter/lib/src/widgets/framework.dart index 08a6b45a0f4..189f3e21b5e 100644 --- a/packages/flutter/lib/src/widgets/framework.dart +++ b/packages/flutter/lib/src/widgets/framework.dart @@ -3137,30 +3137,9 @@ abstract class Element extends DiagnosticableTree implements BuildContext { /// in a build method if it is known that they will not change. @nonVirtual @override - // ignore: avoid_equals_and_hash_code_on_mutable_classes + // ignore: avoid_equals_and_hash_code_on_mutable_classes, hash_and_equals bool operator ==(Object other) => identical(this, other); - // Custom implementation of hash code optimized for the ".of" pattern used - // with `InheritedWidgets`. - // - // `Element.dependOnInheritedWidgetOfExactType` relies heavily on hash-based - // `Set` look-ups, putting this getter on the performance critical path. - // - // The value is designed to fit within the SMI representation. This makes - // the cached value use less memory (one field and no extra heap objects) and - // cheap to compare (no indirection). - // - // See also: - // - // * https://dart.dev/articles/dart-vm/numeric-computation, which - // explains how numbers are represented in Dart. - @nonVirtual - @override - // ignore: avoid_equals_and_hash_code_on_mutable_classes - int get hashCode => _cachedHash; - final int _cachedHash = _nextHashCode = (_nextHashCode + 1) % 0xffffff; - static int _nextHashCode = 1; - /// Information set by parent to define where this child fits in its parent's /// child list. /// diff --git a/packages/flutter/test/widgets/framework_test.dart b/packages/flutter/test/widgets/framework_test.dart index ba4c9e8a23e..23f9c370a12 100644 --- a/packages/flutter/test/widgets/framework_test.dart +++ b/packages/flutter/test/widgets/framework_test.dart @@ -1656,6 +1656,21 @@ The findRenderObject() method was called for the following element: )), ); }); + + testWidgets('Elements use the identity hashCode', (WidgetTester tester) async { + final StatefulElement statefulElement = StatefulElement(const _StatefulLeaf()); + expect(statefulElement.hashCode, identityHashCode(statefulElement)); + + final StatelessElement statelessElement = StatelessElement(const Placeholder()); + + expect(statelessElement.hashCode, identityHashCode(statelessElement)); + + final InheritedElement inheritedElement = InheritedElement( + const Directionality(textDirection: TextDirection.ltr, child: Placeholder()), + ); + + expect(inheritedElement.hashCode, identityHashCode(inheritedElement)); + }); } class _WidgetWithNoVisitChildren extends StatelessWidget {