Skip to content

Commit adfa25e

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

File tree

1 file changed

+13
-43
lines changed

1 file changed

+13
-43
lines changed

src/compiler/program.ts

+13-43
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> = {};
@@ -1918,6 +1886,8 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
19181886
processingOtherFiles = undefined;
19191887
}
19201888

1889+
filesWithReferencesProcessed = undefined;
1890+
19211891
// Release any files we have acquired in the old program but are
19221892
// not part of the new program.
19231893
if (oldProgram && host.onReleaseOldSourceFile) {
@@ -3751,10 +3721,10 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
37513721
const originalFileName = fileName;
37523722
if (filesByName.has(path)) {
37533723
const file = filesByName.get(path);
3754-
addFileIncludeReason(file || undefined, reason);
3724+
const addedReason = addFileIncludeReason(file || undefined, reason, /*checkExisting*/ true);
37553725
// try to check if we've already seen this file but with a different casing in path
37563726
// NOTE: this only makes sense for case-insensitive file systems, and only on files which are not redirected
3757-
if (file && !(options.forceConsistentCasingInFileNames === false)) {
3727+
if (file && addedReason && !(options.forceConsistentCasingInFileNames === false)) {
37583728
const checkedName = file.fileName;
37593729
const isRedirect = toPath(checkedName) !== toPath(fileName);
37603730
if (isRedirect) {
@@ -3831,7 +3801,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
38313801
const dupFile = createRedirectedSourceFile(fileFromPackageId, file!, fileName, path, toPath(fileName), originalFileName, sourceFileOptions);
38323802
redirectTargetsMap.add(fileFromPackageId.path, fileName);
38333803
addFileToFilesByName(dupFile, path, fileName, redirectedPath);
3834-
addFileIncludeReason(dupFile, reason);
3804+
addFileIncludeReason(dupFile, reason, /*checkExisting*/ false);
38353805
sourceFileToPackageName.set(path, packageIdToPackageName(packageId));
38363806
processingOtherFiles!.push(dupFile);
38373807
return dupFile;
@@ -3852,7 +3822,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
38523822
file.originalFileName = originalFileName;
38533823
file.packageJsonLocations = sourceFileOptions.packageJsonLocations?.length ? sourceFileOptions.packageJsonLocations : undefined;
38543824
file.packageJsonScope = sourceFileOptions.packageJsonScope;
3855-
addFileIncludeReason(file, reason);
3825+
addFileIncludeReason(file, reason, /*checkExisting*/ false);
38563826

38573827
if (host.useCaseSensitiveFileNames()) {
38583828
const pathLowerCase = toFileNameLowerCase(path);
@@ -3885,17 +3855,17 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
38853855
else {
38863856
processingOtherFiles!.push(file);
38873857
}
3858+
(filesWithReferencesProcessed ??= new Set()).add(file.path);
38883859
}
38893860
return file;
38903861
}
38913862

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-
}
3863+
function addFileIncludeReason(file: SourceFile | undefined, reason: FileIncludeReason, checkExisting: boolean) {
3864+
if (file && (!checkExisting || !isReferencedFile(reason) || !filesWithReferencesProcessed?.has(reason.file))) {
3865+
fileReasons.add(file.path, reason);
3866+
return true;
38983867
}
3868+
return false;
38993869
}
39003870

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

0 commit comments

Comments
 (0)