Skip to content

Commit 9261ca7

Browse files
authored
Share redirects cache key calculation between multiple caches used for module resolution and type reference directive (#55376)
1 parent 5725506 commit 9261ca7

File tree

4 files changed

+73
-10
lines changed

4 files changed

+73
-10
lines changed

src/compiler/moduleNameResolver.ts

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,7 @@ export interface PerNonRelativeNameCache<T> {
854854
export interface ModuleResolutionCache extends PerDirectoryResolutionCache<ResolvedModuleWithFailedLookupLocations>, NonRelativeModuleNameResolutionCache, PackageJsonInfoCache {
855855
getPackageJsonInfoCache(): PackageJsonInfoCache;
856856
/** @internal */ clearAllExceptPackageJsonInfoCache(): void;
857+
/** @internal */ optionsToRedirectsKey: Map<CompilerOptions, RedirectsCacheKey>;
857858
}
858859

859860
/**
@@ -905,10 +906,11 @@ export interface CacheWithRedirects<K, V> {
905906
}
906907

907908
/** @internal */
908-
export function createCacheWithRedirects<K, V>(ownOptions: CompilerOptions | undefined): CacheWithRedirects<K, V> {
909-
type RedirectsCacheKey = string & { __compilerOptionsKey: any; };
909+
export type RedirectsCacheKey = string & { __compilerOptionsKey: any; };
910+
911+
/** @internal */
912+
export function createCacheWithRedirects<K, V>(ownOptions: CompilerOptions | undefined, optionsToRedirectsKey: Map<CompilerOptions, RedirectsCacheKey>): CacheWithRedirects<K, V> {
910913
const redirectsMap = new Map<CompilerOptions, Map<K, V>>();
911-
const optionsToRedirectsKey = new Map<CompilerOptions, RedirectsCacheKey>();
912914
const redirectsKeyToMap = new Map<RedirectsCacheKey, Map<K, V>>();
913915
let ownMap = new Map<K, V>();
914916
if (ownOptions) redirectsMap.set(ownOptions, ownMap);
@@ -1011,8 +1013,13 @@ function getOrCreateCache<K, V>(cacheWithRedirects: CacheWithRedirects<K, V>, re
10111013
return result;
10121014
}
10131015

1014-
function createPerDirectoryResolutionCache<T>(currentDirectory: string, getCanonicalFileName: GetCanonicalFileName, options: CompilerOptions | undefined): PerDirectoryResolutionCache<T> {
1015-
const directoryToModuleNameMap = createCacheWithRedirects<Path, ModeAwareCache<T>>(options);
1016+
function createPerDirectoryResolutionCache<T>(
1017+
currentDirectory: string,
1018+
getCanonicalFileName: GetCanonicalFileName,
1019+
options: CompilerOptions | undefined,
1020+
optionsToRedirectsKey: Map<CompilerOptions, RedirectsCacheKey>,
1021+
): PerDirectoryResolutionCache<T> {
1022+
const directoryToModuleNameMap = createCacheWithRedirects<Path, ModeAwareCache<T>>(options, optionsToRedirectsKey);
10161023
return {
10171024
getFromDirectoryCache,
10181025
getOrCreateCacheForDirectory,
@@ -1114,8 +1121,9 @@ function createNonRelativeNameResolutionCache<T>(
11141121
getCanonicalFileName: (s: string) => string,
11151122
options: CompilerOptions | undefined,
11161123
getResolvedFileName: (result: T) => string | undefined,
1124+
optionsToRedirectsKey: Map<CompilerOptions, RedirectsCacheKey>,
11171125
): NonRelativeNameResolutionCache<T> {
1118-
const moduleNameToDirectoryMap = createCacheWithRedirects<ModeAwareCacheKey, PerNonRelativeNameCache<T>>(options);
1126+
const moduleNameToDirectoryMap = createCacheWithRedirects<ModeAwareCacheKey, PerNonRelativeNameCache<T>>(options, optionsToRedirectsKey);
11191127
return {
11201128
getFromNonRelativeNameCache,
11211129
getOrCreateCacheForNonRelativeName,
@@ -1216,20 +1224,29 @@ function createNonRelativeNameResolutionCache<T>(
12161224
interface ModuleOrTypeReferenceResolutionCache<T> extends PerDirectoryResolutionCache<T>, NonRelativeNameResolutionCache<T>, PackageJsonInfoCache {
12171225
getPackageJsonInfoCache(): PackageJsonInfoCache;
12181226
clearAllExceptPackageJsonInfoCache(): void;
1227+
optionsToRedirectsKey: Map<CompilerOptions, RedirectsCacheKey>;
12191228
}
12201229
function createModuleOrTypeReferenceResolutionCache<T>(
12211230
currentDirectory: string,
12221231
getCanonicalFileName: (s: string) => string,
12231232
options: CompilerOptions | undefined,
12241233
packageJsonInfoCache: PackageJsonInfoCache | undefined,
12251234
getResolvedFileName: (result: T) => string | undefined,
1235+
optionsToRedirectsKey: Map<CompilerOptions, RedirectsCacheKey> | undefined,
12261236
): ModuleOrTypeReferenceResolutionCache<T> {
1227-
const perDirectoryResolutionCache = createPerDirectoryResolutionCache<T>(currentDirectory, getCanonicalFileName, options);
1237+
optionsToRedirectsKey ??= new Map();
1238+
const perDirectoryResolutionCache = createPerDirectoryResolutionCache<T>(
1239+
currentDirectory,
1240+
getCanonicalFileName,
1241+
options,
1242+
optionsToRedirectsKey,
1243+
);
12281244
const nonRelativeNameResolutionCache = createNonRelativeNameResolutionCache(
12291245
currentDirectory,
12301246
getCanonicalFileName,
12311247
options,
12321248
getResolvedFileName,
1249+
optionsToRedirectsKey,
12331250
);
12341251
packageJsonInfoCache ??= createPackageJsonInfoCache(currentDirectory, getCanonicalFileName);
12351252

@@ -1241,6 +1258,7 @@ function createModuleOrTypeReferenceResolutionCache<T>(
12411258
update,
12421259
getPackageJsonInfoCache: () => packageJsonInfoCache!,
12431260
clearAllExceptPackageJsonInfoCache,
1261+
optionsToRedirectsKey,
12441262
};
12451263

12461264
function clear() {
@@ -1264,13 +1282,29 @@ export function createModuleResolutionCache(
12641282
getCanonicalFileName: (s: string) => string,
12651283
options?: CompilerOptions,
12661284
packageJsonInfoCache?: PackageJsonInfoCache,
1285+
): ModuleResolutionCache;
1286+
/** @internal */
1287+
export function createModuleResolutionCache(
1288+
currentDirectory: string,
1289+
getCanonicalFileName: (s: string) => string,
1290+
options?: CompilerOptions,
1291+
packageJsonInfoCache?: PackageJsonInfoCache,
1292+
optionsToRedirectsKey?: Map<CompilerOptions, RedirectsCacheKey>,
1293+
): ModuleResolutionCache;
1294+
export function createModuleResolutionCache(
1295+
currentDirectory: string,
1296+
getCanonicalFileName: (s: string) => string,
1297+
options?: CompilerOptions,
1298+
packageJsonInfoCache?: PackageJsonInfoCache,
1299+
optionsToRedirectsKey?: Map<CompilerOptions, RedirectsCacheKey>,
12671300
): ModuleResolutionCache {
12681301
const result = createModuleOrTypeReferenceResolutionCache(
12691302
currentDirectory,
12701303
getCanonicalFileName,
12711304
options,
12721305
packageJsonInfoCache,
12731306
getOriginalOrResolvedModuleFileName,
1307+
optionsToRedirectsKey,
12741308
) as ModuleResolutionCache;
12751309
result.getOrCreateCacheForModuleName = (nonRelativeName, mode, redirectedReference) => result.getOrCreateCacheForNonRelativeName(nonRelativeName, mode, redirectedReference);
12761310
return result;
@@ -1281,13 +1315,29 @@ export function createTypeReferenceDirectiveResolutionCache(
12811315
getCanonicalFileName: (s: string) => string,
12821316
options?: CompilerOptions,
12831317
packageJsonInfoCache?: PackageJsonInfoCache,
1318+
): TypeReferenceDirectiveResolutionCache;
1319+
/** @internal */
1320+
export function createTypeReferenceDirectiveResolutionCache(
1321+
currentDirectory: string,
1322+
getCanonicalFileName: (s: string) => string,
1323+
options?: CompilerOptions,
1324+
packageJsonInfoCache?: PackageJsonInfoCache,
1325+
optionsToRedirectsKey?: Map<CompilerOptions, RedirectsCacheKey>,
1326+
): TypeReferenceDirectiveResolutionCache;
1327+
export function createTypeReferenceDirectiveResolutionCache(
1328+
currentDirectory: string,
1329+
getCanonicalFileName: (s: string) => string,
1330+
options?: CompilerOptions,
1331+
packageJsonInfoCache?: PackageJsonInfoCache,
1332+
optionsToRedirectsKey?: Map<CompilerOptions, RedirectsCacheKey>,
12841333
): TypeReferenceDirectiveResolutionCache {
12851334
return createModuleOrTypeReferenceResolutionCache(
12861335
currentDirectory,
12871336
getCanonicalFileName,
12881337
options,
12891338
packageJsonInfoCache,
1290-
getOriginalOrResolvedTypeReferenceFileName
1339+
getOriginalOrResolvedTypeReferenceFileName,
1340+
optionsToRedirectsKey,
12911341
);
12921342
}
12931343

src/compiler/program.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1621,7 +1621,13 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
16211621
).map(resolvedTypeReferenceDirective => ({ resolvedTypeReferenceDirective }));
16221622
}
16231623
else {
1624-
const typeReferenceDirectiveResolutionCache = createTypeReferenceDirectiveResolutionCache(currentDirectory, getCanonicalFileName, /*options*/ undefined, moduleResolutionCache?.getPackageJsonInfoCache());
1624+
const typeReferenceDirectiveResolutionCache = createTypeReferenceDirectiveResolutionCache(
1625+
currentDirectory,
1626+
getCanonicalFileName,
1627+
/*options*/ undefined,
1628+
moduleResolutionCache?.getPackageJsonInfoCache(),
1629+
moduleResolutionCache?.optionsToRedirectsKey,
1630+
);
16251631
actualResolveTypeReferenceDirectiveNamesWorker = (typeDirectiveNames, containingFile, redirectedReference, options, containingSourceFile) =>
16261632
loadWithModeAwareCache(
16271633
typeDirectiveNames,

src/compiler/resolutionCache.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
452452
resolutionHost.getCanonicalFileName,
453453
resolutionHost.getCompilationSettings(),
454454
moduleResolutionCache.getPackageJsonInfoCache(),
455+
moduleResolutionCache.optionsToRedirectsKey,
455456
);
456457

457458
const resolvedLibraries = new Map<string, CachedResolvedModuleWithFailedLookupLocations>();

src/compiler/tsbuildPublic.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,13 @@ function createSolutionBuilderState<T extends BuilderProgram>(watch: boolean, ho
455455
compilerHost.getModuleResolutionCache = () => moduleResolutionCache;
456456
}
457457
if (!compilerHost.resolveTypeReferenceDirectiveReferences && !compilerHost.resolveTypeReferenceDirectives) {
458-
typeReferenceDirectiveResolutionCache = createTypeReferenceDirectiveResolutionCache(compilerHost.getCurrentDirectory(), compilerHost.getCanonicalFileName, /*options*/ undefined, moduleResolutionCache?.getPackageJsonInfoCache());
458+
typeReferenceDirectiveResolutionCache = createTypeReferenceDirectiveResolutionCache(
459+
compilerHost.getCurrentDirectory(),
460+
compilerHost.getCanonicalFileName,
461+
/*options*/ undefined,
462+
moduleResolutionCache?.getPackageJsonInfoCache(),
463+
moduleResolutionCache?.optionsToRedirectsKey,
464+
);
459465
compilerHost.resolveTypeReferenceDirectiveReferences = (typeDirectiveNames, containingFile, redirectedReference, options, containingSourceFile) =>
460466
loadWithModeAwareCache(
461467
typeDirectiveNames,

0 commit comments

Comments
 (0)