From fab4ef0bdeae9d192c3efadeb7ef62fe9028d4cd Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Tue, 7 Mar 2017 13:26:41 -0800 Subject: [PATCH] cache semantic and declaration diagnostics in program (#14516) --- src/compiler/program.ts | 44 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/src/compiler/program.ts b/src/compiler/program.ts index d9ddf486e0d..abb8b0b2f09 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -290,6 +290,11 @@ namespace ts { return resolutions; } + interface DiagnosticCache { + perFile?: FileMap; + allDiagnostics?: Diagnostic[]; + } + export function createProgram(rootNames: string[], options: CompilerOptions, host?: CompilerHost, oldProgram?: Program): Program { let program: Program; let files: SourceFile[] = []; @@ -298,6 +303,9 @@ namespace ts { let noDiagnosticsTypeChecker: TypeChecker; let classifiableNames: Map; + let cachedSemanticDiagnosticsForFile: DiagnosticCache = {}; + let cachedDeclarationDiagnosticsForFile: DiagnosticCache = {}; + let resolvedTypeReferenceDirectives = createMap(); let fileProcessingDiagnostics = createDiagnosticCollection(); @@ -899,6 +907,10 @@ namespace ts { } function getSemanticDiagnosticsForFile(sourceFile: SourceFile, cancellationToken: CancellationToken): Diagnostic[] { + return getAndCacheDiagnostics(sourceFile, cancellationToken, cachedSemanticDiagnosticsForFile, getSemanticDiagnosticsForFileNoCache); + } + + function getSemanticDiagnosticsForFileNoCache(sourceFile: SourceFile, cancellationToken: CancellationToken): Diagnostic[] { return runWithCancellationToken(() => { const typeChecker = getDiagnosticsProducingTypeChecker(); @@ -1094,7 +1106,11 @@ namespace ts { }); } - function getDeclarationDiagnosticsWorker(sourceFile: SourceFile, cancellationToken: CancellationToken): Diagnostic[] { + function getDeclarationDiagnosticsWorker(sourceFile: SourceFile | undefined, cancellationToken: CancellationToken): Diagnostic[] { + return getAndCacheDiagnostics(sourceFile, cancellationToken, cachedDeclarationDiagnosticsForFile, getDeclarationDiagnosticsForFileNoCache); + } + + function getDeclarationDiagnosticsForFileNoCache(sourceFile: SourceFile| undefined, cancellationToken: CancellationToken) { return runWithCancellationToken(() => { const resolver = getDiagnosticsProducingTypeChecker().getEmitResolver(sourceFile, cancellationToken); // Don't actually write any files since we're just getting diagnostics. @@ -1102,6 +1118,32 @@ namespace ts { }); } + function getAndCacheDiagnostics( + sourceFile: SourceFile | undefined, + cancellationToken: CancellationToken, + cache: DiagnosticCache, + getDiagnostics: (sourceFile: SourceFile, cancellationToken: CancellationToken) => Diagnostic[]) { + + const cachedResult = sourceFile + ? cache.perFile && cache.perFile.get(sourceFile.path) + : cache.allDiagnostics; + + if (cachedResult) { + return cachedResult; + } + const result = getDiagnostics(sourceFile, cancellationToken) || emptyArray; + if (sourceFile) { + if (!cache.perFile) { + cache.perFile = createFileMap(); + } + cache.perFile.set(sourceFile.path, result); + } + else { + cache.allDiagnostics = result; + } + return result; + } + function getDeclarationDiagnosticsForFile(sourceFile: SourceFile, cancellationToken: CancellationToken): Diagnostic[] { return isDeclarationFile(sourceFile) ? [] : getDeclarationDiagnosticsWorker(sourceFile, cancellationToken); }