Avoid unnecessary buildInfo read if host supports caching it (avoids in --build scenario) and some reporting cleanup (#51403)

* Emit diagnostics when just manipuating bundle at that time itself
[4:04:42 PM] Updating output of project '/TypeScript/src/tsserver/tsconfig.json'...

Memory used:        581215K
transformTime time:   0.01s
Source Map time:      0.35s
commentTime time:     0.00s
printTime time:       0.58s
Emit time:            0.90s
I/O Write time:       0.01s
Total time:           0.90s

* Pull out getSourceFile and writeFile in a function

* Fix incorrect solutionPerformance reporting in watch mode

* Remove unnecessary build info read when host can give cached buildInfo

* Simplify overloads

* Accept API change
This commit is contained in:
Sheetal Nandi
2022-11-04 19:19:57 -07:00
committed by GitHub
parent f0216e3421
commit 354891cf43
17 changed files with 275 additions and 250 deletions

View File

@@ -752,15 +752,20 @@ namespace ts {
const solutionPerformance = enableSolutionPerformance(sys, buildOptions);
updateSolutionBuilderHost(sys, cb, buildHost, solutionPerformance);
const onWatchStatusChange = buildHost.onWatchStatusChange;
let reportBuildStatistics = false;
buildHost.onWatchStatusChange = (d, newLine, options, errorCount) => {
onWatchStatusChange?.(d, newLine, options, errorCount);
if (d.code === Diagnostics.Found_0_errors_Watching_for_file_changes.code ||
d.code === Diagnostics.Found_1_error_Watching_for_file_changes.code) {
if (reportBuildStatistics && (
d.code === Diagnostics.Found_0_errors_Watching_for_file_changes.code ||
d.code === Diagnostics.Found_1_error_Watching_for_file_changes.code
)) {
reportSolutionBuilderTimes(builder, solutionPerformance);
}
};
const builder = createSolutionBuilderWithWatch(buildHost, projects, buildOptions, watchOptions);
builder.build();
reportSolutionBuilderTimes(builder, solutionPerformance);
reportBuildStatistics = true;
return builder;
}
@@ -855,7 +860,11 @@ namespace ts {
reportStatistics(sys, program.getProgram(), solutionPerformance);
cb(program);
};
buildHost.afterEmitBundle = cb;
buildHost.beforeEmitBundle = config => enableStatisticsAndTracing(sys, config.options, /*isBuildMode*/ true);
buildHost.afterEmitBundle = config => {
reportStatistics(sys, config, solutionPerformance);
cb(config);
};
}
function updateCreateProgram<T extends BuilderProgram>(sys: System, host: { createProgram: CreateProgram<T>; }, isBuildMode: boolean) {
@@ -997,6 +1006,7 @@ namespace ts {
});
performance.disable();
performance.enable();
solutionPerformance.clear();
reportAllStatistics(sys, statistics);
@@ -1035,8 +1045,14 @@ namespace ts {
return startsWith(name, "SolutionBuilder::");
}
function reportStatistics(sys: System, program: Program, solutionPerformance: SolutionPerformance | undefined) {
const compilerOptions = program.getCompilerOptions();
function isProgram(programOrConfig: Program | ParsedCommandLine): programOrConfig is Program {
return !(programOrConfig as ParsedCommandLine).options;
}
function reportStatistics(sys: System, programOrConfig: Program | ParsedCommandLine, solutionPerformance: SolutionPerformance | undefined) {
const program = isProgram(programOrConfig) ? programOrConfig : undefined;
const config = isProgram(programOrConfig) ? undefined : programOrConfig;
const compilerOptions = program ? program.getCompilerOptions() : config!.options;
if (canTrace(sys, compilerOptions)) {
tracing?.stopTracing();
@@ -1046,23 +1062,24 @@ namespace ts {
if (canReportDiagnostics(sys, compilerOptions)) {
statistics = [];
const memoryUsed = sys.getMemoryUsage ? sys.getMemoryUsage() : -1;
reportCountStatistic("Files", program.getSourceFiles().length);
if (program) {
reportCountStatistic("Files", program.getSourceFiles().length);
const lineCounts = countLines(program);
if (compilerOptions.extendedDiagnostics) {
for (const key of arrayFrom(lineCounts.keys())) {
reportCountStatistic("Lines of " + key, lineCounts.get(key)!);
const lineCounts = countLines(program);
if (compilerOptions.extendedDiagnostics) {
for (const key of arrayFrom(lineCounts.keys())) {
reportCountStatistic("Lines of " + key, lineCounts.get(key)!);
}
}
else {
reportCountStatistic("Lines", reduceLeftIterator(lineCounts.values(), (sum, count) => sum + count, 0));
}
}
else {
reportCountStatistic("Lines", reduceLeftIterator(lineCounts.values(), (sum, count) => sum + count, 0));
}
reportCountStatistic("Identifiers", program.getIdentifierCount());
reportCountStatistic("Symbols", program.getSymbolCount());
reportCountStatistic("Types", program.getTypeCount());
reportCountStatistic("Instantiations", program.getInstantiationCount());
reportCountStatistic("Identifiers", program.getIdentifierCount());
reportCountStatistic("Symbols", program.getSymbolCount());
reportCountStatistic("Types", program.getTypeCount());
reportCountStatistic("Instantiations", program.getInstantiationCount());
}
if (memoryUsed >= 0) {
reportStatisticalValue({ name: "Memory used", value: memoryUsed, type: StatisticType.memory }, /*aggregate*/ true);
}
@@ -1073,11 +1090,13 @@ namespace ts {
const checkTime = isPerformanceEnabled ? performance.getDuration("Check") : 0;
const emitTime = isPerformanceEnabled ? performance.getDuration("Emit") : 0;
if (compilerOptions.extendedDiagnostics) {
const caches = program.getRelationCacheSizes();
reportCountStatistic("Assignability cache size", caches.assignable);
reportCountStatistic("Identity cache size", caches.identity);
reportCountStatistic("Subtype cache size", caches.subtype);
reportCountStatistic("Strict subtype cache size", caches.strictSubtype);
if (program) {
const caches = program.getRelationCacheSizes();
reportCountStatistic("Assignability cache size", caches.assignable);
reportCountStatistic("Identity cache size", caches.identity);
reportCountStatistic("Subtype cache size", caches.subtype);
reportCountStatistic("Strict subtype cache size", caches.strictSubtype);
}
if (isPerformanceEnabled) {
performance.forEachMeasure((name, duration) => {
if (!isSolutionMarkOrMeasure(name)) reportTimeStatistic(`${name} time`, duration, /*aggregate*/ true);