Skip to content

Commit ddce8d5

Browse files
scheglovCommit Queue
authored and
Commit Queue
committed
Macro. Add more details for performance.
Bug: #55784 Change-Id: I2e633eae9f28a6ed053eca762068e8fac7e56c6d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/372121 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 88e62a2 commit ddce8d5

File tree

5 files changed

+274
-107
lines changed

5 files changed

+274
-107
lines changed

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

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,10 @@ class AnalysisDriver {
11511151

11521152
FileState file = _fsState.getFileForPath(path);
11531153
RecordingErrorListener listener = RecordingErrorListener();
1154-
CompilationUnit unit = file.parse(listener);
1154+
CompilationUnit unit = file.parse(
1155+
errorListener: listener,
1156+
performance: OperationPerformanceImpl('<root>'),
1157+
);
11551158
return ParsedUnitResultImpl(
11561159
session: currentSession,
11571160
fileState: file,
@@ -1848,25 +1851,35 @@ class AnalysisDriver {
18481851
}
18491852

18501853
Future<void> _getUnitElement(String path) async {
1851-
FileState file = _fsState.getFileForPath(path);
1854+
await scheduler.accumulatedPerformance.runAsync(
1855+
'getUnitElement',
1856+
(performance) async {
1857+
FileState file = _fsState.getFileForPath(path);
18521858

1853-
// Prepare the library - the file itself, or the known library.
1854-
var kind = file.kind;
1855-
var library = kind.library ?? kind.asLibrary;
1859+
// Prepare the library - the file itself, or the known library.
1860+
var kind = file.kind;
1861+
var library = kind.library ?? kind.asLibrary;
18561862

1857-
await libraryContext.load(
1858-
targetLibrary: library,
1859-
performance: OperationPerformanceImpl('<root>'),
1860-
);
1863+
await performance.runAsync(
1864+
'libraryContext',
1865+
(performance) async {
1866+
await libraryContext.load(
1867+
targetLibrary: library,
1868+
performance: performance,
1869+
);
1870+
},
1871+
);
18611872

1862-
var element = libraryContext.computeUnitElement(library, file);
1863-
var result = UnitElementResultImpl(
1864-
session: currentSession,
1865-
fileState: file,
1866-
element: element,
1867-
);
1873+
var element = libraryContext.computeUnitElement(library, file);
1874+
var result = UnitElementResultImpl(
1875+
session: currentSession,
1876+
fileState: file,
1877+
element: element,
1878+
);
18681879

1869-
_unitElementRequestedFiles.completeAll(path, result);
1880+
_unitElementRequestedFiles.completeAll(path, result);
1881+
},
1882+
);
18701883
}
18711884

18721885
bool _hasLibraryByUri(String uriStr) {

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

Lines changed: 95 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import 'package:analyzer/src/summary/api_signature.dart';
3737
import 'package:analyzer/src/summary/package_bundle_reader.dart';
3838
import 'package:analyzer/src/summary2/informative_data.dart';
3939
import 'package:analyzer/src/util/file_paths.dart' as file_paths;
40+
import 'package:analyzer/src/util/performance/operation_performance.dart';
4041
import 'package:analyzer/src/util/uri.dart';
4142
import 'package:analyzer/src/utilities/extensions/collection.dart';
4243
import 'package:analyzer/src/utilities/uri_cache.dart';
@@ -495,10 +496,16 @@ class FileState {
495496
}
496497

497498
/// Return a new parsed unresolved [CompilationUnit].
498-
CompilationUnitImpl parse([AnalysisErrorListener? errorListener]) {
499+
CompilationUnitImpl parse({
500+
AnalysisErrorListener? errorListener,
501+
required OperationPerformanceImpl performance,
502+
}) {
499503
errorListener ??= AnalysisErrorListener.NULL_LISTENER;
500504
try {
501-
return _parse(errorListener);
505+
return _parse(
506+
errorListener,
507+
performance: performance,
508+
);
502509
} catch (exception, stackTrace) {
503510
throw CaughtExceptionWithFiles(
504511
exception,
@@ -512,42 +519,50 @@ class FileState {
512519
CompilationUnitImpl parseCode({
513520
required String code,
514521
required AnalysisErrorListener errorListener,
522+
required OperationPerformanceImpl performance,
515523
}) {
516-
CharSequenceReader reader = CharSequenceReader(code);
517-
Scanner scanner = Scanner(source, reader, errorListener)
518-
..configureFeatures(
519-
featureSetForOverriding: featureSet,
520-
featureSet: featureSet.restrictToVersion(
521-
packageLanguageVersion,
522-
),
524+
return performance.run('parseCode', (performance) {
525+
performance.getDataInt('length').add(code.length);
526+
527+
CharSequenceReader reader = CharSequenceReader(code);
528+
Scanner scanner = Scanner(source, reader, errorListener)
529+
..configureFeatures(
530+
featureSetForOverriding: featureSet,
531+
featureSet: featureSet.restrictToVersion(
532+
packageLanguageVersion,
533+
),
534+
);
535+
Token token = scanner.tokenize(reportScannerErrors: false);
536+
LineInfo lineInfo = LineInfo(scanner.lineStarts);
537+
538+
Parser parser = Parser(
539+
source,
540+
errorListener,
541+
featureSet: scanner.featureSet,
542+
lineInfo: lineInfo,
523543
);
524-
Token token = scanner.tokenize(reportScannerErrors: false);
525-
LineInfo lineInfo = LineInfo(scanner.lineStarts);
526-
527-
Parser parser = Parser(
528-
source,
529-
errorListener,
530-
featureSet: scanner.featureSet,
531-
lineInfo: lineInfo,
532-
);
533544

534-
var unit = parser.parseCompilationUnit(token);
535-
unit.languageVersion = LibraryLanguageVersion(
536-
package: packageLanguageVersion,
537-
override: scanner.overrideVersion,
538-
);
545+
var unit = parser.parseCompilationUnit(token);
546+
unit.languageVersion = LibraryLanguageVersion(
547+
package: packageLanguageVersion,
548+
override: scanner.overrideVersion,
549+
);
539550

540-
// Ensure the string canonicalization cache size is reasonable.
541-
pruneStringCanonicalizationCache();
551+
// Ensure the string canonicalization cache size is reasonable.
552+
pruneStringCanonicalizationCache();
542553

543-
return unit;
554+
return unit;
555+
});
544556
}
545557

546558
/// Read the file content and ensure that all of the file properties are
547559
/// consistent with the read content, including API signature.
548560
///
549561
/// Return how the file changed since the last refresh.
550-
FileStateRefreshResult refresh() {
562+
FileStateRefreshResult refresh({
563+
OperationPerformanceImpl? performance,
564+
}) {
565+
performance ??= OperationPerformanceImpl('<root>');
551566
_invalidateCurrentUnresolvedData();
552567

553568
FileContent rawFileState;
@@ -578,7 +593,12 @@ class FileState {
578593
}
579594

580595
// Prepare the unlinked unit.
581-
_driverUnlinkedUnit = _getUnlinkedUnit(previousUnlinkedKey);
596+
performance.run('getUnlinkedUnit', (performance) {
597+
_driverUnlinkedUnit = _getUnlinkedUnit(
598+
previousUnlinkedKey,
599+
performance: performance,
600+
);
601+
});
582602
_unlinked2 = _driverUnlinkedUnit!.unit;
583603
_lineInfo = LineInfo(_unlinked2!.lineStarts);
584604

@@ -699,7 +719,10 @@ class FileState {
699719

700720
/// Return the unlinked unit, freshly deserialized from bytes,
701721
/// previously deserialized from bytes, or new.
702-
AnalysisDriverUnlinkedUnit _getUnlinkedUnit(String? previousUnlinkedKey) {
722+
AnalysisDriverUnlinkedUnit _getUnlinkedUnit(
723+
String? previousUnlinkedKey, {
724+
required OperationPerformanceImpl performance,
725+
}) {
703726
if (previousUnlinkedKey != null) {
704727
if (previousUnlinkedKey != _unlinkedKey) {
705728
_fsState.unlinkedUnitStore.release(previousUnlinkedKey);
@@ -723,7 +746,10 @@ class FileState {
723746
return result;
724747
}
725748

726-
var unit = parse();
749+
var unit = parse(
750+
performance: performance,
751+
);
752+
727753
return _fsState._logger.run('Create unlinked for $path', () {
728754
var unlinkedUnit = serializeAstUnlinked2(
729755
unit,
@@ -759,10 +785,14 @@ class FileState {
759785
}
760786
}
761787

762-
CompilationUnitImpl _parse(AnalysisErrorListener errorListener) {
788+
CompilationUnitImpl _parse(
789+
AnalysisErrorListener errorListener, {
790+
required OperationPerformanceImpl performance,
791+
}) {
763792
return parseCode(
764793
code: content,
765794
errorListener: errorListener,
795+
performance: performance,
766796
);
767797
}
768798

@@ -1316,7 +1346,10 @@ class FileSystemState {
13161346
/// to a file, for example because it is invalid (e.g. a `package:` URI
13171347
/// without a package name), or we don't know this package. The returned
13181348
/// file has the last known state since if was last refreshed.
1319-
UriResolution? getFileForUri(Uri uri) {
1349+
UriResolution? getFileForUri(
1350+
Uri uri, {
1351+
OperationPerformanceImpl? performance,
1352+
}) {
13201353
var uriSource = _sourceFactory.forUri2(uri);
13211354

13221355
// If the external store has this URI, create a stub file for it.
@@ -1349,7 +1382,12 @@ class FileSystemState {
13491382
return null;
13501383
}
13511384

1352-
file = _newFile(resource, path, rewrittenUri);
1385+
file = _newFile(
1386+
resource,
1387+
path,
1388+
rewrittenUri,
1389+
performance: performance,
1390+
);
13531391
}
13541392
return UriResolutionFile(file);
13551393
}
@@ -1494,7 +1532,12 @@ class FileSystemState {
14941532
nonPackageLanguageVersion: analysisOptions.nonPackageLanguageVersion);
14951533
}
14961534

1497-
FileState _newFile(File resource, String path, Uri uri) {
1535+
FileState _newFile(
1536+
File resource,
1537+
String path,
1538+
Uri uri, {
1539+
OperationPerformanceImpl? performance,
1540+
}) {
14981541
FileSource uriSource = FileSource(resource, uri);
14991542
WorkspacePackage? workspacePackage = _workspace?.findPackageFor(path);
15001543
AnalysisOptionsImpl analysisOptions = _getAnalysisOptions(resource);
@@ -1509,7 +1552,14 @@ class FileSystemState {
15091552
knownFilePaths.add(path);
15101553
knownFiles.add(file);
15111554
fileStamp++;
1512-
file.refresh();
1555+
1556+
performance ??= OperationPerformanceImpl('<root>');
1557+
performance.run('fileState.refresh', (performance) {
1558+
file.refresh(
1559+
performance: performance,
1560+
);
1561+
});
1562+
15131563
onNewFile(file);
15141564
return file;
15151565
}
@@ -1843,6 +1893,7 @@ class LibraryFileKind extends LibraryOrAugmentationFileKind {
18431893
AugmentationImportWithFile addMacroAugmentation(
18441894
String code, {
18451895
required int? partialIndex,
1896+
required OperationPerformanceImpl performance,
18461897
}) {
18471898
var pathContext = file._fsState.pathContext;
18481899
var libraryFileName = pathContext.basename(file.path);
@@ -1869,7 +1920,15 @@ class LibraryFileKind extends LibraryOrAugmentationFileKind {
18691920
// Or this happens during the explicit `refresh()`, more below.
18701921
file._fsState._macroFileContent = fileContent;
18711922

1872-
var macroFileResolution = file._fsState.getFileForUri(macroUri);
1923+
var macroFileResolution = performance.run(
1924+
'getFileForUri',
1925+
(performance) {
1926+
return file._fsState.getFileForUri(
1927+
macroUri,
1928+
performance: performance,
1929+
);
1930+
},
1931+
);
18731932
macroFileResolution as UriResolutionFile;
18741933
var macroFile = macroFileResolution.file;
18751934

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,10 @@ class LibraryAnalyzer {
575575
/// Return a new parsed unresolved [CompilationUnit].
576576
UnitAnalysis _parse(FileState file) {
577577
var errorListener = RecordingErrorListener();
578-
var unit = file.parse(errorListener);
578+
var unit = file.parse(
579+
errorListener: errorListener,
580+
performance: OperationPerformanceImpl('<root>'),
581+
);
579582

580583
// TODO(scheglov): Store [IgnoreInfo] as unlinked data.
581584

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ class LibraryContext {
347347
libraryKind.addMacroAugmentation(
348348
macroGeneratedCode,
349349
partialIndex: null,
350+
performance: OperationPerformanceImpl('<root>'),
350351
);
351352
}
352353
}

0 commit comments

Comments
 (0)