Skip to content

Commit

Permalink
Elements. Stop using ElementLocation for Element.hashCode, use identi…
Browse files Browse the repository at this point in the history
…tyHashCode().

And because we don't use `location` often anymore, stop caching it.

Change-Id: I3470803e09499fc49e57351bfe27a0993b223dae
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/401941
Reviewed-by: Brian Wilkerson <[email protected]>
Reviewed-by: Phil Quitslund <[email protected]>
  • Loading branch information
scheglov committed Dec 21, 2024
1 parent b79f187 commit facaffc
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 45 deletions.
38 changes: 2 additions & 36 deletions pkg/analyzer/lib/src/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2528,12 +2528,6 @@ abstract class ElementImpl implements Element, Element2 {
/// Cached flags denoting presence of specific annotations in [_metadata].
int _metadataFlags = 0;

/// A cached copy of the calculated hashCode for this element.
int? _cachedHashCode;

/// A cached copy of the calculated location for this element.
ElementLocation? _cachedLocation;

/// The documentation comment for this element.
String? _docComment;

Expand Down Expand Up @@ -2666,13 +2660,6 @@ abstract class ElementImpl implements Element, Element2 {
return false;
}

@override
int get hashCode {
// TODO(scheglov): We might want to re-visit this optimization in the future.
// We cache the hash code value as this is a very frequently called method.
return _cachedHashCode ??= location.hashCode;
}

@override
bool get hasImmutable {
var metadata = this.metadata;
Expand Down Expand Up @@ -2992,7 +2979,7 @@ abstract class ElementImpl implements Element, Element2 {

@override
ElementLocation get location {
return _cachedLocation ??= ElementLocationImpl.con1(this);
return ElementLocationImpl.con1(this);
}

@override
Expand Down Expand Up @@ -3311,11 +3298,6 @@ abstract class ElementImpl implements Element, Element2 {
}

abstract class ElementImpl2 implements Element2 {
/// A cached copy of the calculated `hashCode` for this element.
int? _cachedHashCode;

ElementLocation? _cachedLocation;

@override
final int id = ElementImpl._NEXT_ID++;

Expand All @@ -3332,13 +3314,6 @@ abstract class ElementImpl2 implements Element2 {
// TODO(augmentations): implement enclosingElement2
Element2? get enclosingElement2 => throw UnimplementedError();

@override
int get hashCode {
// TODO(scheglov): We might want to re-visit this optimization in the future.
// We cache the hash code as this is a very frequently called method.
return _cachedHashCode ??= location.hashCode;
}

/// Return an identifier that uniquely identifies this element among the
/// children of this element's parent.
String get identifier {
Expand All @@ -3364,7 +3339,7 @@ abstract class ElementImpl2 implements Element2 {

@override
ElementLocation? get location {
return _cachedLocation ??= ElementLocationImpl.fromElement(this);
return ElementLocationImpl.fromElement(this);
}

@override
Expand Down Expand Up @@ -6546,9 +6521,6 @@ class JoinPatternVariableElementImpl extends PatternVariableElementImpl
JoinPatternVariableElementImpl2 get element =>
super.element as JoinPatternVariableElementImpl2;

@override
int get hashCode => identityHashCode(this);

@override
bool get isConsistent {
return inconsistency == shared.JoinedPatternVariableInconsistency.none;
Expand Down Expand Up @@ -7273,9 +7245,6 @@ class LibraryExportElementImpl extends _ExistingElementImpl
@override
LibraryElement2? get exportedLibrary2 => exportedLibrary;

@override
int get hashCode => identityHashCode(this);

@override
String get identifier => 'export@$nameOffset';

Expand Down Expand Up @@ -7330,9 +7299,6 @@ class LibraryImportElementImpl extends _ExistingElementImpl
return super.enclosingElement3 as CompilationUnitElementImpl;
}

@override
int get hashCode => identityHashCode(this);

@override
String get identifier => 'import@$nameOffset';

Expand Down
11 changes: 5 additions & 6 deletions pkg/analyzer/lib/src/dart/element/type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -370,16 +370,15 @@ class FunctionTypeImpl extends TypeImpl implements FunctionType {
for (var namedParameter in sortedNamedParameters) {
namedParameterInfo.add(namedParameter.isRequired);
namedParameterInfo.add(namedParameter.name);
namedParameterInfo.add(namedParameter.type);
}
}

return Object.hash(
nullabilitySuffix,
returnType,
requiredPositionalParameterCount,
Object.hashAll(positionalParameterTypes),
namedParameterInfo);
nullabilitySuffix,
returnType,
requiredPositionalParameterCount,
namedParameterInfo,
);
}

/// Given two functions [f1] and [f2] where f1 and f2 are known to be
Expand Down
1 change: 0 additions & 1 deletion pkg/analyzer/test/src/dart/element/element_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ class ClassElementImplTest extends AbstractTypeSystemTest {
expect(classA.hasNonFinalField, isTrue);
}

@FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/44522')
void test_hasNonFinalField_true_inherited() {
var classA = class_(name: 'A');
ClassElementImpl classB =
Expand Down
4 changes: 2 additions & 2 deletions pkg/analyzer/test/src/dart/element/function_type_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ class FunctionTypeTest extends AbstractTypeSystemTest {
}

test_hash_optionalPositionalParameterType() {
_testHashesSometimesDiffer((i) => FunctionTypeImpl(
_testHashesAlwaysEqual((i) => FunctionTypeImpl(
typeFormals: const [],
parameters: [
positionalParameter(name: 'x', type: class_(name: 'C$i').thisType)
Expand Down Expand Up @@ -364,7 +364,7 @@ class FunctionTypeTest extends AbstractTypeSystemTest {
}

test_hash_requiredPositionalParameterType() {
_testHashesSometimesDiffer((i) => FunctionTypeImpl(
_testHashesAlwaysEqual((i) => FunctionTypeImpl(
typeFormals: const [],
parameters: [
requiredParameter(name: 'x', type: class_(name: 'C$i').thisType)
Expand Down

0 comments on commit facaffc

Please sign in to comment.