Skip to content

Replace record keys in maps #3659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions lib/src/model/model_element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ abstract class ModelElement extends Canonicalization
}

// Return the cached ModelElement if it exists.
var cachedModelElement = packageGraph
.allConstructedModelElements[(e, library, enclosingContainer)];
var cachedModelElement = packageGraph.allConstructedModelElements[
ConstructedModelElementsKey(e, library, enclosingContainer)];
if (cachedModelElement != null) {
return cachedModelElement;
}
Expand Down Expand Up @@ -263,8 +263,8 @@ abstract class ModelElement extends Canonicalization
}

// Return the cached ModelElement if it exists.
var cachedModelElement = packageGraph
.allConstructedModelElements[(e, library, enclosingContainer)];
var cachedModelElement = packageGraph.allConstructedModelElements[
ConstructedModelElementsKey(e, library, enclosingContainer)];
if (cachedModelElement != null) {
return cachedModelElement;
}
Expand Down Expand Up @@ -294,11 +294,12 @@ abstract class ModelElement extends Canonicalization
// is fixed?
if (library != Library.sentinel && newModelElement is! Parameter) {
runtimeStats.incrementAccumulator('modelElementCacheInsertion');
var key = (e, library, enclosingContainer);
var key = ConstructedModelElementsKey(e, library, enclosingContainer);
library.packageGraph.allConstructedModelElements[key] = newModelElement;
if (newModelElement is Inheritable) {
library.packageGraph.allInheritableElements
.putIfAbsent((e, library), () => {}).add(newModelElement);
.putIfAbsent(InheritableElementsKey(e, library), () => {})
.add(newModelElement);
}
}
}
Expand Down
44 changes: 37 additions & 7 deletions lib/src/model/package_graph.dart
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,12 @@ class PackageGraph with CommentReferable, Nameable, ModelBuilder {

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

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

/// A mapping of the list of classes which implement each class.
final Map<InheritingContainer, List<InheritingContainer>> _implementors =
Expand Down Expand Up @@ -800,18 +801,20 @@ class PackageGraph with CommentReferable, Nameable, ModelBuilder {
{InheritingContainer? preferredClass}) {
var candidates = <ModelElement>{};
if (lib != null) {
var constructedWithKey = allConstructedModelElements[(e, lib, null)];
var constructedWithKey = allConstructedModelElements[
ConstructedModelElementsKey(e, lib, null)];
if (constructedWithKey != null) {
candidates.add(constructedWithKey);
}
var constructedWithKeyWithClass =
allConstructedModelElements[(e, lib, preferredClass)];
var constructedWithKeyWithClass = allConstructedModelElements[
ConstructedModelElementsKey(e, lib, preferredClass)];
if (constructedWithKeyWithClass != null) {
candidates.add(constructedWithKeyWithClass);
}
if (candidates.isEmpty) {
candidates = {
...?allInheritableElements[(e, lib)]?.where((me) => me.isCanonical),
...?allInheritableElements[InheritableElementsKey(e, lib)]
?.where((me) => me.isCanonical),
};
}
}
Expand Down Expand Up @@ -1004,3 +1007,30 @@ class PackageGraph with CommentReferable, Nameable, ModelBuilder {
@override
Iterable<CommentReferable> get referenceParents => const [];
}

class ConstructedModelElementsKey {
final Element element;
final Library library;
final Container? enclosingElement;

ConstructedModelElementsKey(
this.element, this.library, this.enclosingElement);

@override
late final int hashCode = Object.hash(element, library, enclosingElement);

@override
bool operator ==(Object other) {
if (other is! ConstructedModelElementsKey) return false;
return other.element == element &&
other.library == library &&
other.enclosingElement == enclosingElement;
}
}

class InheritableElementsKey {
final Element element;
final Library library;

InheritableElementsKey(this.element, this.library);
}