Skip to content

Share redirects cache key calculation between multiple caches used for module resolution and type reference directive #55376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 58 additions & 8 deletions src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,7 @@ export interface PerNonRelativeNameCache<T> {
export interface ModuleResolutionCache extends PerDirectoryResolutionCache<ResolvedModuleWithFailedLookupLocations>, NonRelativeModuleNameResolutionCache, PackageJsonInfoCache {
getPackageJsonInfoCache(): PackageJsonInfoCache;
/** @internal */ clearAllExceptPackageJsonInfoCache(): void;
/** @internal */ optionsToRedirectsKey: Map<CompilerOptions, RedirectsCacheKey>;
}

/**
Expand Down Expand Up @@ -905,10 +906,11 @@ export interface CacheWithRedirects<K, V> {
}

/** @internal */
export function createCacheWithRedirects<K, V>(ownOptions: CompilerOptions | undefined): CacheWithRedirects<K, V> {
type RedirectsCacheKey = string & { __compilerOptionsKey: any; };
export type RedirectsCacheKey = string & { __compilerOptionsKey: any; };

/** @internal */
export function createCacheWithRedirects<K, V>(ownOptions: CompilerOptions | undefined, optionsToRedirectsKey: Map<CompilerOptions, RedirectsCacheKey>): CacheWithRedirects<K, V> {
const redirectsMap = new Map<CompilerOptions, Map<K, V>>();
const optionsToRedirectsKey = new Map<CompilerOptions, RedirectsCacheKey>();
const redirectsKeyToMap = new Map<RedirectsCacheKey, Map<K, V>>();
let ownMap = new Map<K, V>();
if (ownOptions) redirectsMap.set(ownOptions, ownMap);
Expand Down Expand Up @@ -1011,8 +1013,13 @@ function getOrCreateCache<K, V>(cacheWithRedirects: CacheWithRedirects<K, V>, re
return result;
}

function createPerDirectoryResolutionCache<T>(currentDirectory: string, getCanonicalFileName: GetCanonicalFileName, options: CompilerOptions | undefined): PerDirectoryResolutionCache<T> {
const directoryToModuleNameMap = createCacheWithRedirects<Path, ModeAwareCache<T>>(options);
function createPerDirectoryResolutionCache<T>(
currentDirectory: string,
getCanonicalFileName: GetCanonicalFileName,
options: CompilerOptions | undefined,
optionsToRedirectsKey: Map<CompilerOptions, RedirectsCacheKey>,
): PerDirectoryResolutionCache<T> {
const directoryToModuleNameMap = createCacheWithRedirects<Path, ModeAwareCache<T>>(options, optionsToRedirectsKey);
return {
getFromDirectoryCache,
getOrCreateCacheForDirectory,
Expand Down Expand Up @@ -1114,8 +1121,9 @@ function createNonRelativeNameResolutionCache<T>(
getCanonicalFileName: (s: string) => string,
options: CompilerOptions | undefined,
getResolvedFileName: (result: T) => string | undefined,
optionsToRedirectsKey: Map<CompilerOptions, RedirectsCacheKey>,
): NonRelativeNameResolutionCache<T> {
const moduleNameToDirectoryMap = createCacheWithRedirects<ModeAwareCacheKey, PerNonRelativeNameCache<T>>(options);
const moduleNameToDirectoryMap = createCacheWithRedirects<ModeAwareCacheKey, PerNonRelativeNameCache<T>>(options, optionsToRedirectsKey);
return {
getFromNonRelativeNameCache,
getOrCreateCacheForNonRelativeName,
Expand Down Expand Up @@ -1216,20 +1224,29 @@ function createNonRelativeNameResolutionCache<T>(
interface ModuleOrTypeReferenceResolutionCache<T> extends PerDirectoryResolutionCache<T>, NonRelativeNameResolutionCache<T>, PackageJsonInfoCache {
getPackageJsonInfoCache(): PackageJsonInfoCache;
clearAllExceptPackageJsonInfoCache(): void;
optionsToRedirectsKey: Map<CompilerOptions, RedirectsCacheKey>;
}
function createModuleOrTypeReferenceResolutionCache<T>(
currentDirectory: string,
getCanonicalFileName: (s: string) => string,
options: CompilerOptions | undefined,
packageJsonInfoCache: PackageJsonInfoCache | undefined,
getResolvedFileName: (result: T) => string | undefined,
optionsToRedirectsKey: Map<CompilerOptions, RedirectsCacheKey> | undefined,
): ModuleOrTypeReferenceResolutionCache<T> {
const perDirectoryResolutionCache = createPerDirectoryResolutionCache<T>(currentDirectory, getCanonicalFileName, options);
optionsToRedirectsKey ??= new Map();
const perDirectoryResolutionCache = createPerDirectoryResolutionCache<T>(
currentDirectory,
getCanonicalFileName,
options,
optionsToRedirectsKey,
);
const nonRelativeNameResolutionCache = createNonRelativeNameResolutionCache(
currentDirectory,
getCanonicalFileName,
options,
getResolvedFileName,
optionsToRedirectsKey,
);
packageJsonInfoCache ??= createPackageJsonInfoCache(currentDirectory, getCanonicalFileName);

Expand All @@ -1241,6 +1258,7 @@ function createModuleOrTypeReferenceResolutionCache<T>(
update,
getPackageJsonInfoCache: () => packageJsonInfoCache!,
clearAllExceptPackageJsonInfoCache,
optionsToRedirectsKey,
};

function clear() {
Expand All @@ -1264,13 +1282,29 @@ export function createModuleResolutionCache(
getCanonicalFileName: (s: string) => string,
options?: CompilerOptions,
packageJsonInfoCache?: PackageJsonInfoCache,
): ModuleResolutionCache;
/** @internal */
export function createModuleResolutionCache(
currentDirectory: string,
getCanonicalFileName: (s: string) => string,
options?: CompilerOptions,
packageJsonInfoCache?: PackageJsonInfoCache,
optionsToRedirectsKey?: Map<CompilerOptions, RedirectsCacheKey>,
): ModuleResolutionCache;
export function createModuleResolutionCache(
currentDirectory: string,
getCanonicalFileName: (s: string) => string,
options?: CompilerOptions,
packageJsonInfoCache?: PackageJsonInfoCache,
optionsToRedirectsKey?: Map<CompilerOptions, RedirectsCacheKey>,
): ModuleResolutionCache {
const result = createModuleOrTypeReferenceResolutionCache(
currentDirectory,
getCanonicalFileName,
options,
packageJsonInfoCache,
getOriginalOrResolvedModuleFileName,
optionsToRedirectsKey,
) as ModuleResolutionCache;
result.getOrCreateCacheForModuleName = (nonRelativeName, mode, redirectedReference) => result.getOrCreateCacheForNonRelativeName(nonRelativeName, mode, redirectedReference);
return result;
Expand All @@ -1281,13 +1315,29 @@ export function createTypeReferenceDirectiveResolutionCache(
getCanonicalFileName: (s: string) => string,
options?: CompilerOptions,
packageJsonInfoCache?: PackageJsonInfoCache,
): TypeReferenceDirectiveResolutionCache;
/** @internal */
export function createTypeReferenceDirectiveResolutionCache(
currentDirectory: string,
getCanonicalFileName: (s: string) => string,
options?: CompilerOptions,
packageJsonInfoCache?: PackageJsonInfoCache,
optionsToRedirectsKey?: Map<CompilerOptions, RedirectsCacheKey>,
): TypeReferenceDirectiveResolutionCache;
export function createTypeReferenceDirectiveResolutionCache(
currentDirectory: string,
getCanonicalFileName: (s: string) => string,
options?: CompilerOptions,
packageJsonInfoCache?: PackageJsonInfoCache,
optionsToRedirectsKey?: Map<CompilerOptions, RedirectsCacheKey>,
): TypeReferenceDirectiveResolutionCache {
return createModuleOrTypeReferenceResolutionCache(
currentDirectory,
getCanonicalFileName,
options,
packageJsonInfoCache,
getOriginalOrResolvedTypeReferenceFileName
getOriginalOrResolvedTypeReferenceFileName,
optionsToRedirectsKey,
);
}

Expand Down
8 changes: 7 additions & 1 deletion src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1621,7 +1621,13 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
).map(resolvedTypeReferenceDirective => ({ resolvedTypeReferenceDirective }));
}
else {
const typeReferenceDirectiveResolutionCache = createTypeReferenceDirectiveResolutionCache(currentDirectory, getCanonicalFileName, /*options*/ undefined, moduleResolutionCache?.getPackageJsonInfoCache());
const typeReferenceDirectiveResolutionCache = createTypeReferenceDirectiveResolutionCache(
currentDirectory,
getCanonicalFileName,
/*options*/ undefined,
moduleResolutionCache?.getPackageJsonInfoCache(),
moduleResolutionCache?.optionsToRedirectsKey,
);
actualResolveTypeReferenceDirectiveNamesWorker = (typeDirectiveNames, containingFile, redirectedReference, options, containingSourceFile) =>
loadWithModeAwareCache(
typeDirectiveNames,
Expand Down
1 change: 1 addition & 0 deletions src/compiler/resolutionCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
resolutionHost.getCanonicalFileName,
resolutionHost.getCompilationSettings(),
moduleResolutionCache.getPackageJsonInfoCache(),
moduleResolutionCache.optionsToRedirectsKey,
);

const resolvedLibraries = new Map<string, CachedResolvedModuleWithFailedLookupLocations>();
Expand Down
8 changes: 7 additions & 1 deletion src/compiler/tsbuildPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,13 @@ function createSolutionBuilderState<T extends BuilderProgram>(watch: boolean, ho
compilerHost.getModuleResolutionCache = () => moduleResolutionCache;
}
if (!compilerHost.resolveTypeReferenceDirectiveReferences && !compilerHost.resolveTypeReferenceDirectives) {
typeReferenceDirectiveResolutionCache = createTypeReferenceDirectiveResolutionCache(compilerHost.getCurrentDirectory(), compilerHost.getCanonicalFileName, /*options*/ undefined, moduleResolutionCache?.getPackageJsonInfoCache());
typeReferenceDirectiveResolutionCache = createTypeReferenceDirectiveResolutionCache(
compilerHost.getCurrentDirectory(),
compilerHost.getCanonicalFileName,
/*options*/ undefined,
moduleResolutionCache?.getPackageJsonInfoCache(),
moduleResolutionCache?.optionsToRedirectsKey,
);
compilerHost.resolveTypeReferenceDirectiveReferences = (typeDirectiveNames, containingFile, redirectedReference, options, containingSourceFile) =>
loadWithModeAwareCache(
typeDirectiveNames,
Expand Down