Skip to content

Commit 6a43bc1

Browse files
committed
Processing resolutions is array since its already computed value and wont change and ready to use.
1 parent 4649301 commit 6a43bc1

File tree

2 files changed

+10
-70
lines changed

2 files changed

+10
-70
lines changed

src/compiler/moduleNameResolver.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,12 @@ import {
9191
removeFileExtension,
9292
removePrefix,
9393
ResolutionMode,
94-
ResolutionNameAndModeGetter,
9594
ResolvedModuleWithFailedLookupLocations,
9695
ResolvedProjectReference,
9796
ResolvedTypeReferenceDirective,
9897
ResolvedTypeReferenceDirectiveWithFailedLookupLocations,
9998
some,
10099
sort,
101-
SourceFile,
102100
startsWith,
103101
supportedDeclarationExtensions,
104102
supportedJSExtensionsFlat,
@@ -1091,22 +1089,6 @@ export function createModeAwareCache<T>(): ModeAwareCache<T> {
10911089
}
10921090
}
10931091

1094-
/** @internal */
1095-
export function zipToModeAwareCache<K, V>(
1096-
file: SourceFile,
1097-
keys: readonly K[],
1098-
values: readonly V[],
1099-
nameAndModeGetter: ResolutionNameAndModeGetter<K, SourceFile>,
1100-
): ModeAwareCache<V> {
1101-
Debug.assert(keys.length === values.length);
1102-
const map = createModeAwareCache<V>();
1103-
for (let i = 0; i < keys.length; ++i) {
1104-
const entry = keys[i];
1105-
map.set(nameAndModeGetter.getName(entry), nameAndModeGetter.getMode(entry, file), values[i]);
1106-
}
1107-
return map;
1108-
}
1109-
11101092
function getOriginalOrResolvedModuleFileName(result: ResolvedModuleWithFailedLookupLocations) {
11111093
return result.resolvedModule && (result.resolvedModule.originalPath || result.resolvedModule.resolvedFileName);
11121094
}

src/compiler/program.ts

Lines changed: 10 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,6 @@ import {
328328
WriteFileCallback,
329329
WriteFileCallbackData,
330330
writeFileEnsuringDirectories,
331-
zipToModeAwareCache,
332331
} from "./_namespaces/ts";
333332
import * as performance from "./_namespaces/ts.performance";
334333

@@ -1514,9 +1513,9 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
15141513
let resolvedLibProcessing: Map<string, LibResolution> | undefined;
15151514

15161515
let resolvedModules: Map<Path, ModeAwareCache<ResolvedModuleWithFailedLookupLocations>> | undefined;
1517-
let resolvedModulesProcessing: Map<Path, ModeAwareCache<ResolvedModuleWithFailedLookupLocations>> | undefined;
1516+
let resolvedModulesProcessing: Map<Path, readonly ResolvedModuleWithFailedLookupLocations[]> | undefined;
15181517
let resolvedTypeReferenceDirectiveNames: Map<Path, ModeAwareCache<ResolvedTypeReferenceDirectiveWithFailedLookupLocations>> | undefined;
1519-
let resolvedTypeReferenceDirectiveNamesProcessing: Map<Path, ModeAwareCache<ResolvedTypeReferenceDirectiveWithFailedLookupLocations>> | undefined;
1518+
let resolvedTypeReferenceDirectiveNamesProcessing: Map<Path, readonly ResolvedTypeReferenceDirectiveWithFailedLookupLocations[]> | undefined;
15201519

15211520
let packageMap: Map<string, boolean> | undefined;
15221521

@@ -2095,23 +2094,6 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
20952094
return resolveModuleNamesWorker(moduleNames, file, /*reusedNames*/ undefined);
20962095
}
20972096

2098-
const alreadyComputedResolutions = resolvedModulesProcessing?.get(file.path);
2099-
if (alreadyComputedResolutions) {
2100-
// `file` was created for the new program.
2101-
//
2102-
// We only set `file.resolvedModules` via work from the current function,
2103-
// so it is defined iff we already called the current function on `file`.
2104-
// That call happened no later than the creation of the `file` object,
2105-
// which per above occurred during the current program creation.
2106-
// Since we assume the filesystem does not change during program creation,
2107-
// it is safe to reuse resolutions from the earlier call.
2108-
const result: ResolvedModuleWithFailedLookupLocations[] = [];
2109-
for (const moduleName of moduleNames) {
2110-
const resolvedModule = alreadyComputedResolutions.get(moduleName.text, getModeForUsageLocation(file, moduleName))!;
2111-
result.push(resolvedModule);
2112-
}
2113-
return result;
2114-
}
21152097
// At this point, we know at least one of the following hold:
21162098
// - file has local declarations for ambient modules
21172099
// - old program state is available
@@ -2240,33 +2222,12 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
22402222
return resolveTypeReferenceDirectiveNamesWorker(typeDirectiveNames, containingFile, /*reusedNames*/ undefined);
22412223
}
22422224

2243-
const oldSourceFile = !isString(containingFile) ? oldProgram && oldProgram.getSourceFile(containingFile.fileName) : undefined;
2244-
if (!isString(containingFile)) {
2245-
const alreadyComputedResolutions = resolvedTypeReferenceDirectiveNamesProcessing?.get(containingFile.path);
2246-
if (alreadyComputedResolutions) {
2247-
// `file` was created for the new program.
2248-
//
2249-
// We only set `file.resolvedTypeReferenceDirectiveNames` via work from the current function,
2250-
// so it is defined iff we already called the current function on `file`.
2251-
// That call happened no later than the creation of the `file` object,
2252-
// which per above occurred during the current program creation.
2253-
// Since we assume the filesystem does not change during program creation,
2254-
// it is safe to reuse resolutions from the earlier call.
2255-
const result: ResolvedTypeReferenceDirectiveWithFailedLookupLocations[] = [];
2256-
for (const typeDirectiveName of typeDirectiveNames as readonly FileReference[]) {
2257-
// We lower-case all type references because npm automatically lowercases all packages. See GH#9824.
2258-
const resolvedTypeReferenceDirective = alreadyComputedResolutions.get(getTypeReferenceResolutionName(typeDirectiveName), getModeForFileReference(typeDirectiveName, containingFile.impliedNodeFormat))!;
2259-
result.push(resolvedTypeReferenceDirective);
2260-
}
2261-
return result;
2262-
}
2263-
}
2264-
22652225
/** An ordered list of module names for which we cannot recover the resolution. */
22662226
let unknownTypeReferenceDirectiveNames: T[] | undefined;
22672227
let result: ResolvedTypeReferenceDirectiveWithFailedLookupLocations[] | undefined;
22682228
let reusedNames: T[] | undefined;
22692229
const containingSourceFile = !isString(containingFile) ? containingFile : undefined;
2230+
const oldSourceFile = !isString(containingFile) ? oldProgram && oldProgram.getSourceFile(containingFile.fileName) : undefined;
22702231
const canReuseResolutions = !isString(containingFile) ?
22712232
containingFile === oldSourceFile && !hasInvalidatedResolutions(containingFile.path) :
22722233
!hasInvalidatedResolutions(toPath(containingFile));
@@ -2516,24 +2477,19 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
25162477
for (const newSourceFile of modifiedSourceFiles) {
25172478
const moduleNames = getModuleNames(newSourceFile);
25182479
const resolutions = resolveModuleNamesReusingOldState(moduleNames, newSourceFile);
2480+
(resolvedModulesProcessing ??= new Map()).set(newSourceFile.path, resolutions);
25192481
const oldResolutions = oldProgram.resolvedModules?.get(newSourceFile.path);
25202482
// ensure that module resolution results are still correct
25212483
const resolutionsChanged = hasChangesInResolutions(moduleNames, newSourceFile, resolutions, oldResolutions, moduleResolutionIsEqualTo, moduleResolutionNameAndModeGetter);
2522-
if (resolutionsChanged) {
2523-
structureIsReused = StructureIsReused.SafeModules;
2524-
(resolvedModulesProcessing ??= new Map()).set(newSourceFile.path, zipToModeAwareCache(newSourceFile, moduleNames, resolutions, moduleResolutionNameAndModeGetter));
2525-
}
2526-
else if (oldResolutions) {
2527-
(resolvedModulesProcessing ??= new Map()).set(newSourceFile.path, oldResolutions);
2528-
}
2484+
if (resolutionsChanged) structureIsReused = StructureIsReused.SafeModules;
25292485
const typesReferenceDirectives = newSourceFile.typeReferenceDirectives;
25302486
const typeReferenceResolutions = resolveTypeReferenceDirectiveNamesReusingOldState(typesReferenceDirectives, newSourceFile);
2487+
(resolvedTypeReferenceDirectiveNamesProcessing ??= new Map()).set(newSourceFile.path, typeReferenceResolutions);
25312488
// ensure that types resolutions are still correct
25322489
const oldTypeResolutions = oldProgram.resolvedTypeReferenceDirectiveNames?.get(newSourceFile.path);
25332490
const typeReferenceResolutionsChanged = hasChangesInResolutions(typesReferenceDirectives, newSourceFile, typeReferenceResolutions, oldTypeResolutions, typeDirectiveIsEqualTo, typeReferenceResolutionNameAndModeGetter);
25342491
if (typeReferenceResolutionsChanged) {
25352492
structureIsReused = StructureIsReused.SafeModules;
2536-
(resolvedTypeReferenceDirectiveNamesProcessing ??= new Map()).set(newSourceFile.path, zipToModeAwareCache(newSourceFile, typesReferenceDirectives, typeReferenceResolutions, typeReferenceResolutionNameAndModeGetter));
25372493
}
25382494
else if (oldTypeResolutions) {
25392495
(resolvedTypeReferenceDirectiveNamesProcessing ??= new Map()).set(newSourceFile.path, oldTypeResolutions);
@@ -3844,7 +3800,8 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
38443800
const typeDirectives = file.typeReferenceDirectives;
38453801
if (!typeDirectives.length) return;
38463802

3847-
const resolutions = resolveTypeReferenceDirectiveNamesReusingOldState(typeDirectives, file);
3803+
const resolutions = resolvedTypeReferenceDirectiveNamesProcessing?.get(file.path) ||
3804+
resolveTypeReferenceDirectiveNamesReusingOldState(typeDirectives, file);
38483805
const resolutionsInFile = createModeAwareCache<ResolvedTypeReferenceDirectiveWithFailedLookupLocations>();
38493806
(resolvedTypeReferenceDirectiveNames ??= new Map()).set(file.path, resolutionsInFile);
38503807
for (let index = 0; index < typeDirectives.length; index++) {
@@ -4020,7 +3977,8 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
40203977
if (file.imports.length || file.moduleAugmentations.length) {
40213978
// Because global augmentation doesn't have string literal name, we can check for global augmentation as such.
40223979
const moduleNames = getModuleNames(file);
4023-
const resolutions = resolveModuleNamesReusingOldState(moduleNames, file);
3980+
const resolutions = resolvedModulesProcessing?.get(file.path) ||
3981+
resolveModuleNamesReusingOldState(moduleNames, file);
40243982
Debug.assert(resolutions.length === moduleNames.length);
40253983
const optionsForFile = (useSourceOfProjectReferenceRedirect ? getRedirectReferenceForResolution(file)?.commandLine.options : undefined) || options;
40263984
const resolutionsInFile = createModeAwareCache<ResolutionWithFailedLookupLocations>();

0 commit comments

Comments
 (0)