Skip to content

Commit 5b1a9dd

Browse files
authored
Replace record keys in maps (#3659)
1 parent 9e55e7b commit 5b1a9dd

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

lib/src/model/model_element.dart

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ abstract class ModelElement extends Canonicalization
173173
}
174174

175175
// Return the cached ModelElement if it exists.
176-
var cachedModelElement = packageGraph
177-
.allConstructedModelElements[(e, library, enclosingContainer)];
176+
var cachedModelElement = packageGraph.allConstructedModelElements[
177+
ConstructedModelElementsKey(e, library, enclosingContainer)];
178178
if (cachedModelElement != null) {
179179
return cachedModelElement;
180180
}
@@ -263,8 +263,8 @@ abstract class ModelElement extends Canonicalization
263263
}
264264

265265
// Return the cached ModelElement if it exists.
266-
var cachedModelElement = packageGraph
267-
.allConstructedModelElements[(e, library, enclosingContainer)];
266+
var cachedModelElement = packageGraph.allConstructedModelElements[
267+
ConstructedModelElementsKey(e, library, enclosingContainer)];
268268
if (cachedModelElement != null) {
269269
return cachedModelElement;
270270
}
@@ -294,11 +294,12 @@ abstract class ModelElement extends Canonicalization
294294
// is fixed?
295295
if (library != Library.sentinel && newModelElement is! Parameter) {
296296
runtimeStats.incrementAccumulator('modelElementCacheInsertion');
297-
var key = (e, library, enclosingContainer);
297+
var key = ConstructedModelElementsKey(e, library, enclosingContainer);
298298
library.packageGraph.allConstructedModelElements[key] = newModelElement;
299299
if (newModelElement is Inheritable) {
300300
library.packageGraph.allInheritableElements
301-
.putIfAbsent((e, library), () => {}).add(newModelElement);
301+
.putIfAbsent(InheritableElementsKey(e, library), () => {})
302+
.add(newModelElement);
302303
}
303304
}
304305
}

lib/src/model/package_graph.dart

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,12 @@ class PackageGraph with CommentReferable, Nameable, ModelBuilder {
318318

319319
/// All [ModelElement]s constructed for this package; a superset of
320320
/// the elements gathered in [_gatherModelElements].
321-
final Map<(Element element, Library library, Container? enclosingElement),
322-
ModelElement> allConstructedModelElements = {};
321+
final Map<ConstructedModelElementsKey, ModelElement>
322+
allConstructedModelElements = {};
323323

324324
/// Anything that might be inheritable, place here for later lookup.
325-
final Map<(Element, Library), Set<ModelElement>> allInheritableElements = {};
325+
final Map<InheritableElementsKey, Set<ModelElement>> allInheritableElements =
326+
{};
326327

327328
/// A mapping of the list of classes which implement each class.
328329
final Map<InheritingContainer, List<InheritingContainer>> _implementors =
@@ -800,18 +801,20 @@ class PackageGraph with CommentReferable, Nameable, ModelBuilder {
800801
{InheritingContainer? preferredClass}) {
801802
var candidates = <ModelElement>{};
802803
if (lib != null) {
803-
var constructedWithKey = allConstructedModelElements[(e, lib, null)];
804+
var constructedWithKey = allConstructedModelElements[
805+
ConstructedModelElementsKey(e, lib, null)];
804806
if (constructedWithKey != null) {
805807
candidates.add(constructedWithKey);
806808
}
807-
var constructedWithKeyWithClass =
808-
allConstructedModelElements[(e, lib, preferredClass)];
809+
var constructedWithKeyWithClass = allConstructedModelElements[
810+
ConstructedModelElementsKey(e, lib, preferredClass)];
809811
if (constructedWithKeyWithClass != null) {
810812
candidates.add(constructedWithKeyWithClass);
811813
}
812814
if (candidates.isEmpty) {
813815
candidates = {
814-
...?allInheritableElements[(e, lib)]?.where((me) => me.isCanonical),
816+
...?allInheritableElements[InheritableElementsKey(e, lib)]
817+
?.where((me) => me.isCanonical),
815818
};
816819
}
817820
}
@@ -1004,3 +1007,30 @@ class PackageGraph with CommentReferable, Nameable, ModelBuilder {
10041007
@override
10051008
Iterable<CommentReferable> get referenceParents => const [];
10061009
}
1010+
1011+
class ConstructedModelElementsKey {
1012+
final Element element;
1013+
final Library library;
1014+
final Container? enclosingElement;
1015+
1016+
ConstructedModelElementsKey(
1017+
this.element, this.library, this.enclosingElement);
1018+
1019+
@override
1020+
late final int hashCode = Object.hash(element, library, enclosingElement);
1021+
1022+
@override
1023+
bool operator ==(Object other) {
1024+
if (other is! ConstructedModelElementsKey) return false;
1025+
return other.element == element &&
1026+
other.library == library &&
1027+
other.enclosingElement == enclosingElement;
1028+
}
1029+
}
1030+
1031+
class InheritableElementsKey {
1032+
final Element element;
1033+
final Library library;
1034+
1035+
InheritableElementsKey(this.element, this.library);
1036+
}

0 commit comments

Comments
 (0)