Skip to content

Commit b24cb84

Browse files
committed
Cache DirectoryBasedDartSdk sources.
This reduces analysis time from of a trivial Dart app from 822 ms to 762 ms. With SDK summaries. [email protected] BUG= Review URL: https://codereview.chromium.org/1577363002 .
1 parent 85a7aff commit b24cb84

File tree

1 file changed

+47
-22
lines changed

1 file changed

+47
-22
lines changed

pkg/analyzer/lib/src/generated/sdk_io.dart

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
library analyzer.src.generated.sdk_io;
66

7+
import 'dart:collection';
78
import 'dart:io';
89

910
import 'package:analyzer/src/context/context.dart';
@@ -200,6 +201,11 @@ class DirectoryBasedDartSdk implements DartSdk {
200201
*/
201202
JavaFile _sdkDirectory;
202203

204+
/**
205+
* The directory within the SDK directory that contains the libraries.
206+
*/
207+
JavaFile _libraryDirectory;
208+
203209
/**
204210
* The revision number of this SDK, or `"0"` if the revision number cannot be
205211
* discovered.
@@ -231,6 +237,11 @@ class DirectoryBasedDartSdk implements DartSdk {
231237
*/
232238
LibraryMap _libraryMap;
233239

240+
/**
241+
* The mapping from Dart URI's to the corresponding sources.
242+
*/
243+
Map<String, Source> _uriToSourceMap = new HashMap<String, Source>();
244+
234245
/**
235246
* Initialize a newly created SDK to represent the Dart SDK installed in the
236247
* [sdkDirectory]. The flag [useDart2jsPaths] is `true` if the dart2js path
@@ -333,8 +344,13 @@ class DirectoryBasedDartSdk implements DartSdk {
333344
/**
334345
* Return the directory within the SDK directory that contains the libraries.
335346
*/
336-
JavaFile get libraryDirectory =>
337-
new JavaFile.relative(_sdkDirectory, _LIB_DIRECTORY_NAME);
347+
JavaFile get libraryDirectory {
348+
if (_libraryDirectory == null) {
349+
_libraryDirectory =
350+
new JavaFile.relative(_sdkDirectory, _LIB_DIRECTORY_NAME);
351+
}
352+
return _libraryDirectory;
353+
}
338354

339355
/**
340356
* Return the file containing the Pub executable, or `null` if it does not exist.
@@ -514,6 +530,35 @@ class DirectoryBasedDartSdk implements DartSdk {
514530

515531
@override
516532
Source mapDartUri(String dartUri) {
533+
Source source = _uriToSourceMap[dartUri];
534+
if (source == null) {
535+
source = _mapDartUri(dartUri);
536+
_uriToSourceMap[dartUri] = source;
537+
}
538+
return source;
539+
}
540+
541+
/**
542+
* Return the [SdkBundle] for this SDK, if it exists, or `null` otherwise.
543+
*/
544+
SdkBundle _getSummarySdkBundle() {
545+
String rootPath = directory.getAbsolutePath();
546+
String path = pathos.join(rootPath, 'lib', '_internal', 'analysis_summary');
547+
try {
548+
File file = new File(path);
549+
if (file.existsSync()) {
550+
List<int> bytes = file.readAsBytesSync();
551+
return new SdkBundle.fromBuffer(bytes);
552+
}
553+
} catch (exception, stackTrace) {
554+
AnalysisEngine.instance.logger.logError(
555+
'Failed to load SDK analysis summary from $path',
556+
new CaughtException(exception, stackTrace));
557+
}
558+
return null;
559+
}
560+
561+
FileBasedSource _mapDartUri(String dartUri) {
517562
String libraryName;
518563
String relativePath;
519564
int index = dartUri.indexOf('/');
@@ -540,26 +585,6 @@ class DirectoryBasedDartSdk implements DartSdk {
540585
}
541586
}
542587

543-
/**
544-
* Return the [SdkBundle] for this SDK, if it exists, or `null` otherwise.
545-
*/
546-
SdkBundle _getSummarySdkBundle() {
547-
String rootPath = directory.getAbsolutePath();
548-
String path = pathos.join(rootPath, 'lib', '_internal', 'analysis_summary');
549-
try {
550-
File file = new File(path);
551-
if (file.existsSync()) {
552-
List<int> bytes = file.readAsBytesSync();
553-
return new SdkBundle.fromBuffer(bytes);
554-
}
555-
} catch (exception, stackTrace) {
556-
AnalysisEngine.instance.logger.logError(
557-
'Failed to load SDK analysis summary from $path',
558-
new CaughtException(exception, stackTrace));
559-
}
560-
return null;
561-
}
562-
563588
/**
564589
* Return the given [file] if it exists and is executable, or `null` if it
565590
* does not exist or is not executable.

0 commit comments

Comments
 (0)