Skip to content

Commit 78c42a7

Browse files
committed
Instead of checking each reason, check if references in file are already processed
1 parent 7f56cc5 commit 78c42a7

File tree

1 file changed

+12
-43
lines changed

1 file changed

+12
-43
lines changed

src/compiler/program.ts

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
append,
77
arrayIsEqualTo,
88
AsExpression,
9-
AutomaticTypeDirectiveFile,
109
BuilderProgram,
1110
CancellationToken,
1211
canHaveDecorators,
@@ -215,7 +214,6 @@ import {
215214
JsonSourceFile,
216215
JsxEmit,
217216
length,
218-
LibFile,
219217
libMap,
220218
LibResolution,
221219
libs,
@@ -249,7 +247,6 @@ import {
249247
OperationCanceledException,
250248
optionsHaveChanges,
251249
PackageId,
252-
packageIdIsEqual,
253250
packageIdToPackageName,
254251
packageIdToString,
255252
PackageJsonInfoCache,
@@ -288,7 +285,6 @@ import {
288285
resolveTypeReferenceDirective,
289286
returnFalse,
290287
returnUndefined,
291-
RootFile,
292288
SatisfiesExpression,
293289
ScriptKind,
294290
ScriptTarget,
@@ -1210,35 +1206,6 @@ interface DiagnosticCache<T extends Diagnostic> {
12101206
allDiagnostics?: readonly T[];
12111207
}
12121208

1213-
function fileIncludeReasonIsEqual(a: FileIncludeReason, b: FileIncludeReason): boolean {
1214-
if (a === b) return true;
1215-
if (a.kind !== b.kind) return false;
1216-
1217-
switch (a.kind) {
1218-
case FileIncludeKind.RootFile:
1219-
Debug.type<RootFile>(b);
1220-
return a.index === b.index;
1221-
case FileIncludeKind.LibFile:
1222-
Debug.type<LibFile>(b);
1223-
return a.index === b.index;
1224-
case FileIncludeKind.SourceFromProjectReference:
1225-
case FileIncludeKind.OutputFromProjectReference:
1226-
Debug.type<ProjectReferenceFile>(b);
1227-
return a.index === b.index;
1228-
case FileIncludeKind.Import:
1229-
case FileIncludeKind.ReferenceFile:
1230-
case FileIncludeKind.TypeReferenceDirective:
1231-
case FileIncludeKind.LibReferenceDirective:
1232-
Debug.type<ReferencedFile>(b);
1233-
return a.file === b.file && a.index === b.index;
1234-
case FileIncludeKind.AutomaticTypeDirectiveFile:
1235-
Debug.type<AutomaticTypeDirectiveFile>(b);
1236-
return a.typeReference === b.typeReference && packageIdIsEqual(a.packageId, b.packageId);
1237-
default:
1238-
return Debug.assertNever(a);
1239-
}
1240-
}
1241-
12421209
/** @internal */
12431210
export function isReferencedFile(reason: FileIncludeReason | undefined): reason is ReferencedFile {
12441211
switch (reason?.kind) {
@@ -1617,6 +1584,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
16171584
let classifiableNames: Set<__String>;
16181585
const ambientModuleNameToUnmodifiedFileName = new Map<string, string>();
16191586
let fileReasons = createMultiMap<Path, FileIncludeReason>();
1587+
let filesWithReferencesProcessed: Set<Path> | undefined;
16201588
let fileReasonsToChain: Map<Path, FileReasonToChainCache> | undefined;
16211589
let reasonToRelatedInfo: Map<FileIncludeReason, DiagnosticWithLocation | false> | undefined;
16221590
const cachedBindAndCheckDiagnosticsForFile: DiagnosticCache<Diagnostic> = {};
@@ -1916,6 +1884,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
19161884
files = stableSort(processingDefaultLibFiles, compareDefaultLibFiles).concat(processingOtherFiles);
19171885
processingDefaultLibFiles = undefined;
19181886
processingOtherFiles = undefined;
1887+
filesWithReferencesProcessed = undefined;
19191888
}
19201889

19211890
// Release any files we have acquired in the old program but are
@@ -3751,10 +3720,10 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
37513720
const originalFileName = fileName;
37523721
if (filesByName.has(path)) {
37533722
const file = filesByName.get(path);
3754-
addFileIncludeReason(file || undefined, reason);
3723+
const addedReason = addFileIncludeReason(file || undefined, reason, /*checkExisting*/ true);
37553724
// try to check if we've already seen this file but with a different casing in path
37563725
// NOTE: this only makes sense for case-insensitive file systems, and only on files which are not redirected
3757-
if (file && !(options.forceConsistentCasingInFileNames === false)) {
3726+
if (file && addedReason && !(options.forceConsistentCasingInFileNames === false)) {
37583727
const checkedName = file.fileName;
37593728
const isRedirect = toPath(checkedName) !== toPath(fileName);
37603729
if (isRedirect) {
@@ -3831,7 +3800,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
38313800
const dupFile = createRedirectedSourceFile(fileFromPackageId, file!, fileName, path, toPath(fileName), originalFileName, sourceFileOptions);
38323801
redirectTargetsMap.add(fileFromPackageId.path, fileName);
38333802
addFileToFilesByName(dupFile, path, fileName, redirectedPath);
3834-
addFileIncludeReason(dupFile, reason);
3803+
addFileIncludeReason(dupFile, reason, /*checkExisting*/ false);
38353804
sourceFileToPackageName.set(path, packageIdToPackageName(packageId));
38363805
processingOtherFiles!.push(dupFile);
38373806
return dupFile;
@@ -3852,7 +3821,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
38523821
file.originalFileName = originalFileName;
38533822
file.packageJsonLocations = sourceFileOptions.packageJsonLocations?.length ? sourceFileOptions.packageJsonLocations : undefined;
38543823
file.packageJsonScope = sourceFileOptions.packageJsonScope;
3855-
addFileIncludeReason(file, reason);
3824+
addFileIncludeReason(file, reason, /*checkExisting*/ false);
38563825

38573826
if (host.useCaseSensitiveFileNames()) {
38583827
const pathLowerCase = toFileNameLowerCase(path);
@@ -3885,17 +3854,17 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
38853854
else {
38863855
processingOtherFiles!.push(file);
38873856
}
3857+
(filesWithReferencesProcessed ??= new Set()).add(file.path);
38883858
}
38893859
return file;
38903860
}
38913861

3892-
function addFileIncludeReason(file: SourceFile | undefined, reason: FileIncludeReason) {
3893-
if (file) {
3894-
const existing = fileReasons.get(file.path);
3895-
if (!some(existing, r => fileIncludeReasonIsEqual(r, reason))) {
3896-
fileReasons.add(file.path, reason);
3897-
}
3862+
function addFileIncludeReason(file: SourceFile | undefined, reason: FileIncludeReason, checkExisting: boolean) {
3863+
if (file && (!checkExisting || !isReferencedFile(reason) || !filesWithReferencesProcessed?.has(reason.file))) {
3864+
fileReasons.add(file.path, reason);
3865+
return true;
38983866
}
3867+
return false;
38993868
}
39003869

39013870
function addFileToFilesByName(file: SourceFile | undefined, path: Path, fileName: string, redirectedPath: Path | undefined) {

0 commit comments

Comments
 (0)