Skip to content

Commit aeb226b

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Index metadata and flush on a library invalidation.
#33798 Change-Id: I8454de31c6d7770aadd7cb59965d7a6f6916b236 Reviewed-on: https://dart-review.googlesource.com/64400 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Paul Berry <[email protected]>
1 parent 647c72e commit aeb226b

File tree

2 files changed

+57
-14
lines changed

2 files changed

+57
-14
lines changed

pkg/analyzer/lib/src/dart/analysis/frontend_resolution.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ class FrontEndCompiler {
9292
/// Each value is the compilation result of the key library.
9393
final Map<Uri, LibraryCompilationResult> _results = {};
9494

95+
/// Index of metadata in [_component].
96+
final AnalyzerMetadataIndex _metadataIndex = new AnalyzerMetadataIndex();
97+
9598
/// The [Component] with currently valid libraries. When a file is invalidated,
9699
/// we remove the file, its library, and everything affected from [_component].
97100
Component _component = new Component();
@@ -225,7 +228,7 @@ class FrontEndCompiler {
225228
await kernelTarget.buildOutlines(nameRoot: _component.root);
226229
Component newComponent = await kernelTarget.buildComponent();
227230
if (newComponent != null) {
228-
AnalyzerMetadataRepository.merge(newComponent, _component);
231+
_metadataIndex.replaceComponent(newComponent);
229232
return newComponent;
230233
} else {
231234
return _component;
@@ -287,6 +290,7 @@ class FrontEndCompiler {
287290
if (library == null) return;
288291

289292
// Invalidate the library.
293+
_metadataIndex.invalidate(library);
290294
_component.libraries.remove(library);
291295
_component.root.removeChild('${library.importUri}');
292296
_component.uriToSource.remove(libraryUri);

pkg/analyzer/lib/src/dart/analysis/kernel_metadata.dart

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,58 @@ class AnalyzerMetadataFactory implements MetadataFactory {
6666
}
6767
}
6868

69+
/// Index of metadata.
70+
class AnalyzerMetadataIndex {
71+
final Map<kernel.Library, List<kernel.TreeNode>> libraryNodes = {};
72+
AnalyzerMetadataRepository repository;
73+
74+
/// The [library] was invalidated, flush its metadata.
75+
void invalidate(kernel.Library library) {
76+
var nodes = libraryNodes.remove(library);
77+
nodes?.forEach(repository.mapping.remove);
78+
}
79+
80+
/// A [newComponent] has been compiled, with new, and only new, metadata.
81+
/// Merge the existing [repository] into the new one, and replace it.
82+
void replaceComponent(kernel.Component newComponent) {
83+
AnalyzerMetadataRepository newRepository =
84+
newComponent.metadata[AnalyzerMetadataRepository.TAG];
85+
if (newRepository != null) {
86+
_indexNewMetadata(newRepository);
87+
if (repository != null) {
88+
newRepository.mapping.addAll(repository.mapping);
89+
}
90+
repository = newRepository;
91+
} else {
92+
newComponent.metadata[AnalyzerMetadataRepository.TAG] = repository;
93+
}
94+
}
95+
96+
void _indexNewMetadata(AnalyzerMetadataRepository newRepository) {
97+
for (var node in newRepository.mapping.keys) {
98+
var library = _enclosingLibrary(node);
99+
assert(library != null);
100+
101+
var nodes = libraryNodes[library];
102+
if (nodes == null) {
103+
nodes = <kernel.TreeNode>[];
104+
libraryNodes[library] = nodes;
105+
}
106+
107+
nodes.add(node);
108+
}
109+
}
110+
111+
static kernel.Library _enclosingLibrary(kernel.TreeNode node) {
112+
for (; node != null; node = node.parent) {
113+
if (node is kernel.Library) {
114+
return node;
115+
}
116+
}
117+
return null;
118+
}
119+
}
120+
69121
/// Analyzer specific implementation of [kernel.MetadataRepository].
70122
class AnalyzerMetadataRepository
71123
implements kernel.MetadataRepository<AnalyzerMetadata> {
@@ -127,17 +179,4 @@ class AnalyzerMetadataRepository
127179
sink.writeByte(0);
128180
}
129181
}
130-
131-
/// Merge metadata from the [source] into the [destination].
132-
static void merge(kernel.Component destination, kernel.Component source) {
133-
kernel.MetadataRepository destinationRepo = destination.metadata[TAG];
134-
kernel.MetadataRepository sourceRepo = source.metadata[TAG];
135-
if (sourceRepo != null) {
136-
if (destinationRepo != null) {
137-
destinationRepo.mapping.addAll(sourceRepo.mapping);
138-
} else {
139-
destination.metadata[TAG] = sourceRepo;
140-
}
141-
}
142-
}
143182
}

0 commit comments

Comments
 (0)