Skip to content

Commit c8c99b5

Browse files
scheglovCommit Queue
authored and
Commit Queue
committed
Cache getResolvedLibrary() results for priority libraries.
Bug: #47968 Change-Id: I123e8cea6ed629dd3fc1379e7e3a86ae5685d65e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/306302 Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 378c0e5 commit c8c99b5

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ class AnalysisDriver implements AnalysisDriverGeneric {
223223
/// Cached results for [_priorityFiles].
224224
final Map<String, ResolvedUnitResult> _priorityResults = {};
225225

226+
/// Cached results of [getResolvedLibrary].
227+
final Map<LibraryFileKind, ResolvedLibraryResultImpl> _resolvedLibraryCache =
228+
{};
229+
226230
/// The controller for the [exceptions] stream.
227231
final StreamController<ExceptionResult> _exceptionController =
228232
StreamController<ExceptionResult>();
@@ -484,6 +488,7 @@ class AnalysisDriver implements AnalysisDriverGeneric {
484488
}
485489
if (file_paths.isDart(resourceProvider.pathContext, path)) {
486490
_priorityResults.clear();
491+
_resolvedLibraryCache.clear();
487492
_pendingFileChanges.add(
488493
_FileChange(path, _FileChangeKind.add),
489494
);
@@ -575,6 +580,7 @@ class AnalysisDriver implements AnalysisDriverGeneric {
575580
}
576581
if (file_paths.isDart(resourceProvider.pathContext, path)) {
577582
_priorityResults.clear();
583+
_resolvedLibraryCache.clear();
578584
_pendingFileChanges.add(
579585
_FileChange(path, _FileChangeKind.change),
580586
);
@@ -1219,6 +1225,7 @@ class AnalysisDriver implements AnalysisDriverGeneric {
12191225
if (file_paths.isDart(resourceProvider.pathContext, path)) {
12201226
_lastProducedSignatures.remove(path);
12211227
_priorityResults.clear();
1228+
_resolvedLibraryCache.clear();
12221229
_pendingFileChanges.add(
12231230
_FileChange(path, _FileChangeKind.remove),
12241231
);
@@ -1438,6 +1445,11 @@ class AnalysisDriver implements AnalysisDriverGeneric {
14381445
Future<ResolvedLibraryResultImpl> _computeResolvedLibrary(
14391446
LibraryFileKind library,
14401447
) async {
1448+
final cached = _resolvedLibraryCache[library];
1449+
if (cached != null) {
1450+
return cached;
1451+
}
1452+
14411453
final path = library.file.path;
14421454
return _logger.runAsync('Compute resolved library $path', () async {
14431455
testView?.numOfAnalyzedLibraries++;
@@ -1466,11 +1478,17 @@ class AnalysisDriver implements AnalysisDriverGeneric {
14661478
);
14671479
}
14681480

1469-
return ResolvedLibraryResultImpl(
1481+
final result = ResolvedLibraryResultImpl(
14701482
session: currentSession,
14711483
element: resolvedUnits.first.libraryElement,
14721484
units: resolvedUnits,
14731485
);
1486+
1487+
if (_isLibraryWithPriorityFile(library)) {
1488+
_resolvedLibraryCache[library] = result;
1489+
}
1490+
1491+
return result;
14741492
});
14751493
}
14761494

pkg/analyzer/test/src/dart/analysis/driver_test.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,6 +1957,28 @@ class B {}
19571957
expect(result.units[0].errors, isEmpty);
19581958
}
19591959

1960+
test_getResolvedLibrary_cachePriority() async {
1961+
final a = newFile('/test/lib/a.dart', '');
1962+
1963+
driver.priorityFiles = [a.path];
1964+
1965+
final result1 = await driver.getResolvedLibrary(a.path);
1966+
result1 as ResolvedLibraryResult;
1967+
1968+
final testView = driver.testView!;
1969+
await waitForIdleWithoutExceptions();
1970+
testView.numOfAnalyzedLibraries = 0;
1971+
allResults.clear();
1972+
1973+
// Ask again, the same cache instance should be returned.
1974+
final result2 = await driver.getResolvedLibrary(a.path);
1975+
expect(result2, same(result1));
1976+
1977+
// No new analysis, no results into the stream.
1978+
expect(testView.numOfAnalyzedLibraries, isZero);
1979+
expect(allResults, isEmpty);
1980+
}
1981+
19601982
test_getResolvedLibrary_invalidPath_notAbsolute() async {
19611983
var result = await driver.getResolvedLibrary('not_absolute.dart');
19621984
expect(result, isA<InvalidPathResult>());

0 commit comments

Comments
 (0)