Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added caches to tsc's CompilerHost #27068

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/tsc/tsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,42 @@ namespace ts {
return builder.buildAllProjects();
}

function cachePerfMarkNames(cacheName: string) {
return {
misses: `${cacheName} cache misses`,
hits: `${cacheName} cache hits`,
};
}

function addCache<T>(func: (arg: string) => T, perfName: string) {
const cache = createMap<T>();
const perfNames = cachePerfMarkNames(perfName);
return (arg: string) => {
let result = cache.get(arg);
if (result !== undefined) {
performance.mark(perfNames.hits);
return result;
}

performance.mark(perfNames.misses);

result = func(arg);
cache.set(arg, result);
return result;
};
}

function addCachesToHost(host: CompilerHost) {
host.fileExists = addCache(host.fileExists, "fileExists");

if (host.directoryExists !== undefined) {
host.directoryExists = addCache(host.directoryExists, "directoryExists");
}
}

function performCompilation(rootNames: string[], projectReferences: ReadonlyArray<ProjectReference> | undefined, options: CompilerOptions, configFileParsingDiagnostics?: ReadonlyArray<Diagnostic>) {
const host = createCompilerHost(options);
addCachesToHost(host);
enableStatistics(options);

const programOptions: CreateProgramOptions = {
Expand Down Expand Up @@ -297,6 +331,15 @@ namespace ts {
const emitTime = performance.getDuration("Emit");
if (compilerOptions.extendedDiagnostics) {
performance.forEachMeasure((name, duration) => reportTimeStatistic(`${name} time`, duration));

forEach([
"fileExists",
"directoryExists"
], (cacheName: string) => {
const fileExistCacheNames = cachePerfMarkNames(cacheName);
reportCountStatistic(`${fileExistCacheNames.hits} count`, performance.getCount(fileExistCacheNames.hits));
reportCountStatistic(`${fileExistCacheNames.misses} count`, performance.getCount(fileExistCacheNames.misses));
});
}
else {
// Individual component times.
Expand Down