From 6ab132fe065c86b34dcf86abc990b604f89a7d14 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Mon, 14 Aug 2023 12:54:04 -0700 Subject: [PATCH] Share redirects cache key calculation between multiple caches used for module resolution and type reference directive (Will share between per directory and non relative names cache of each of these) so calculation reduced to 1 instead of 4 --- src/compiler/moduleNameResolver.ts | 66 ++++++++++++++++++++++++++---- src/compiler/program.ts | 8 +++- src/compiler/resolutionCache.ts | 1 + src/compiler/tsbuildPublic.ts | 8 +++- 4 files changed, 73 insertions(+), 10 deletions(-) diff --git a/src/compiler/moduleNameResolver.ts b/src/compiler/moduleNameResolver.ts index 3049f8b742d9d..c229d42be1b36 100644 --- a/src/compiler/moduleNameResolver.ts +++ b/src/compiler/moduleNameResolver.ts @@ -854,6 +854,7 @@ export interface PerNonRelativeNameCache { export interface ModuleResolutionCache extends PerDirectoryResolutionCache, NonRelativeModuleNameResolutionCache, PackageJsonInfoCache { getPackageJsonInfoCache(): PackageJsonInfoCache; /** @internal */ clearAllExceptPackageJsonInfoCache(): void; + /** @internal */ optionsToRedirectsKey: Map; } /** @@ -905,10 +906,11 @@ export interface CacheWithRedirects { } /** @internal */ -export function createCacheWithRedirects(ownOptions: CompilerOptions | undefined): CacheWithRedirects { - type RedirectsCacheKey = string & { __compilerOptionsKey: any; }; +export type RedirectsCacheKey = string & { __compilerOptionsKey: any; }; + +/** @internal */ +export function createCacheWithRedirects(ownOptions: CompilerOptions | undefined, optionsToRedirectsKey: Map): CacheWithRedirects { const redirectsMap = new Map>(); - const optionsToRedirectsKey = new Map(); const redirectsKeyToMap = new Map>(); let ownMap = new Map(); if (ownOptions) redirectsMap.set(ownOptions, ownMap); @@ -1011,8 +1013,13 @@ function getOrCreateCache(cacheWithRedirects: CacheWithRedirects, re return result; } -function createPerDirectoryResolutionCache(currentDirectory: string, getCanonicalFileName: GetCanonicalFileName, options: CompilerOptions | undefined): PerDirectoryResolutionCache { - const directoryToModuleNameMap = createCacheWithRedirects>(options); +function createPerDirectoryResolutionCache( + currentDirectory: string, + getCanonicalFileName: GetCanonicalFileName, + options: CompilerOptions | undefined, + optionsToRedirectsKey: Map, +): PerDirectoryResolutionCache { + const directoryToModuleNameMap = createCacheWithRedirects>(options, optionsToRedirectsKey); return { getFromDirectoryCache, getOrCreateCacheForDirectory, @@ -1114,8 +1121,9 @@ function createNonRelativeNameResolutionCache( getCanonicalFileName: (s: string) => string, options: CompilerOptions | undefined, getResolvedFileName: (result: T) => string | undefined, + optionsToRedirectsKey: Map, ): NonRelativeNameResolutionCache { - const moduleNameToDirectoryMap = createCacheWithRedirects>(options); + const moduleNameToDirectoryMap = createCacheWithRedirects>(options, optionsToRedirectsKey); return { getFromNonRelativeNameCache, getOrCreateCacheForNonRelativeName, @@ -1216,6 +1224,7 @@ function createNonRelativeNameResolutionCache( interface ModuleOrTypeReferenceResolutionCache extends PerDirectoryResolutionCache, NonRelativeNameResolutionCache, PackageJsonInfoCache { getPackageJsonInfoCache(): PackageJsonInfoCache; clearAllExceptPackageJsonInfoCache(): void; + optionsToRedirectsKey: Map; } function createModuleOrTypeReferenceResolutionCache( currentDirectory: string, @@ -1223,13 +1232,21 @@ function createModuleOrTypeReferenceResolutionCache( options: CompilerOptions | undefined, packageJsonInfoCache: PackageJsonInfoCache | undefined, getResolvedFileName: (result: T) => string | undefined, + optionsToRedirectsKey: Map | undefined, ): ModuleOrTypeReferenceResolutionCache { - const perDirectoryResolutionCache = createPerDirectoryResolutionCache(currentDirectory, getCanonicalFileName, options); + optionsToRedirectsKey ??= new Map(); + const perDirectoryResolutionCache = createPerDirectoryResolutionCache( + currentDirectory, + getCanonicalFileName, + options, + optionsToRedirectsKey, + ); const nonRelativeNameResolutionCache = createNonRelativeNameResolutionCache( currentDirectory, getCanonicalFileName, options, getResolvedFileName, + optionsToRedirectsKey, ); packageJsonInfoCache ??= createPackageJsonInfoCache(currentDirectory, getCanonicalFileName); @@ -1241,6 +1258,7 @@ function createModuleOrTypeReferenceResolutionCache( update, getPackageJsonInfoCache: () => packageJsonInfoCache!, clearAllExceptPackageJsonInfoCache, + optionsToRedirectsKey, }; function clear() { @@ -1264,6 +1282,21 @@ 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, +): ModuleResolutionCache; +export function createModuleResolutionCache( + currentDirectory: string, + getCanonicalFileName: (s: string) => string, + options?: CompilerOptions, + packageJsonInfoCache?: PackageJsonInfoCache, + optionsToRedirectsKey?: Map, ): ModuleResolutionCache { const result = createModuleOrTypeReferenceResolutionCache( currentDirectory, @@ -1271,6 +1304,7 @@ export function createModuleResolutionCache( options, packageJsonInfoCache, getOriginalOrResolvedModuleFileName, + optionsToRedirectsKey, ) as ModuleResolutionCache; result.getOrCreateCacheForModuleName = (nonRelativeName, mode, redirectedReference) => result.getOrCreateCacheForNonRelativeName(nonRelativeName, mode, redirectedReference); return result; @@ -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, +): TypeReferenceDirectiveResolutionCache; +export function createTypeReferenceDirectiveResolutionCache( + currentDirectory: string, + getCanonicalFileName: (s: string) => string, + options?: CompilerOptions, + packageJsonInfoCache?: PackageJsonInfoCache, + optionsToRedirectsKey?: Map, ): TypeReferenceDirectiveResolutionCache { return createModuleOrTypeReferenceResolutionCache( currentDirectory, getCanonicalFileName, options, packageJsonInfoCache, - getOriginalOrResolvedTypeReferenceFileName + getOriginalOrResolvedTypeReferenceFileName, + optionsToRedirectsKey, ); } diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 227d3b5a3983f..9d1c258cec3e5 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -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, diff --git a/src/compiler/resolutionCache.ts b/src/compiler/resolutionCache.ts index bf7972763a568..7d6ac150c05e8 100644 --- a/src/compiler/resolutionCache.ts +++ b/src/compiler/resolutionCache.ts @@ -452,6 +452,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD resolutionHost.getCanonicalFileName, resolutionHost.getCompilationSettings(), moduleResolutionCache.getPackageJsonInfoCache(), + moduleResolutionCache.optionsToRedirectsKey, ); const resolvedLibraries = new Map(); diff --git a/src/compiler/tsbuildPublic.ts b/src/compiler/tsbuildPublic.ts index 3bc998eb7dc26..b734ca7eed949 100644 --- a/src/compiler/tsbuildPublic.ts +++ b/src/compiler/tsbuildPublic.ts @@ -455,7 +455,13 @@ function createSolutionBuilderState(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,