Skip to content

Commit 116b9b3

Browse files
committed
Use per directory cache for type reference directive resolution as well
1 parent 55f0eec commit 116b9b3

File tree

7 files changed

+222
-106
lines changed

7 files changed

+222
-106
lines changed

src/compiler/diagnosticMessages.json

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4849,6 +4849,22 @@
48494849
"category": "Error",
48504850
"code": 6238
48514851
},
4852+
"Using cached result of 'package.json' at '{0}' that indicates it was found.": {
4853+
"category": "Message",
4854+
"code": 6239
4855+
},
4856+
"Using cached result of 'package.json' at '{0}' that indicates it was not found.": {
4857+
"category": "Message",
4858+
"code": 6240
4859+
},
4860+
"Resolution for type reference directive '{0}' was found in cache from location '{1}'.": {
4861+
"category": "Message",
4862+
"code": 6241
4863+
},
4864+
"======== Resolving type reference directive '{0}', containing file '{1}'. ========": {
4865+
"category": "Message",
4866+
"code": 6242
4867+
},
48524868

48534869
"Projects to reference": {
48544870
"category": "Message",
@@ -5041,14 +5057,6 @@
50415057
"code": 6387,
50425058
"reportsDeprecated": true
50435059
},
5044-
"Using cached result of 'package.json' at '{0}' that indicates it was found.": {
5045-
"category": "Message",
5046-
"code": 6388
5047-
},
5048-
"Using cached result of 'package.json' at '{0}' that indicates it was not found.": {
5049-
"category": "Message",
5050-
"code": 6389
5051-
},
50525060

50535061
"The expected type comes from property '{0}' which is declared here on type '{1}'": {
50545062
"category": "Message",

src/compiler/moduleNameResolver.ts

Lines changed: 163 additions & 73 deletions
Large diffs are not rendered by default.

src/compiler/program.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ namespace ts {
843843
let _compilerOptionsObjectLiteralSyntax: ObjectLiteralExpression | false | undefined;
844844

845845
let moduleResolutionCache: ModuleResolutionCache | undefined;
846-
let packageJsonInfoCache: PackageJsonInfoCache | undefined;
846+
let typeReferenceDirectiveResolutionCache: TypeReferenceDirectiveResolutionCache | undefined;
847847
let actualResolveModuleNamesWorker: (moduleNames: string[], containingFile: string, reusedNames?: string[], redirectedReference?: ResolvedProjectReference) => ResolvedModuleFull[];
848848
const hasInvalidatedResolution = host.hasInvalidatedResolution || returnFalse;
849849
if (host.resolveModuleNames) {
@@ -868,14 +868,14 @@ namespace ts {
868868
actualResolveTypeReferenceDirectiveNamesWorker = (typeDirectiveNames, containingFile, redirectedReference) => host.resolveTypeReferenceDirectives!(Debug.checkEachDefined(typeDirectiveNames), containingFile, redirectedReference, options);
869869
}
870870
else {
871-
packageJsonInfoCache = moduleResolutionCache || createPackageJsonInfoCache(currentDirectory, getCanonicalFileName);
871+
typeReferenceDirectiveResolutionCache = createTypeReferenceDirectiveResolutionCache(currentDirectory, getCanonicalFileName, /*options*/ undefined, moduleResolutionCache?.getPackageJsonInfoCache());
872872
const loader = (typesRef: string, containingFile: string, redirectedReference: ResolvedProjectReference | undefined) => resolveTypeReferenceDirective(
873873
typesRef,
874874
containingFile,
875875
options,
876876
host,
877877
redirectedReference,
878-
packageJsonInfoCache,
878+
typeReferenceDirectiveResolutionCache,
879879
).resolvedTypeReferenceDirective!; // TODO: GH#18217
880880
actualResolveTypeReferenceDirectiveNamesWorker = (typeReferenceDirectiveNames, containingFile, redirectedReference) => loadWithLocalCache<ResolvedTypeReferenceDirective>(Debug.checkEachDefined(typeReferenceDirectiveNames), containingFile, redirectedReference, loader);
881881
}
@@ -1045,7 +1045,7 @@ namespace ts {
10451045
);
10461046
}
10471047

1048-
packageJsonInfoCache = undefined;
1048+
typeReferenceDirectiveResolutionCache = undefined;
10491049

10501050
// unconditionally set oldProgram to undefined to prevent it from being captured in closure
10511051
oldProgram = undefined;

src/compiler/resolutionCache.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,23 @@ namespace ts {
166166
const resolvedModuleNames = new Map<Path, ESMap<string, CachedResolvedModuleWithFailedLookupLocations>>();
167167
const perDirectoryResolvedModuleNames: CacheWithRedirects<ESMap<string, CachedResolvedModuleWithFailedLookupLocations>> = createCacheWithRedirects();
168168
const nonRelativeModuleNameCache: CacheWithRedirects<PerModuleNameCache> = createCacheWithRedirects();
169-
const moduleResolutionCache = createModuleResolutionCacheWithMaps(
169+
const moduleResolutionCache = createModuleResolutionCache(
170+
getCurrentDirectory(),
171+
resolutionHost.getCanonicalFileName,
172+
/*options*/ undefined,
170173
perDirectoryResolvedModuleNames,
171174
nonRelativeModuleNameCache,
172-
getCurrentDirectory(),
173-
resolutionHost.getCanonicalFileName
174175
);
175176

176177
const resolvedTypeReferenceDirectives = new Map<Path, ESMap<string, CachedResolvedTypeReferenceDirectiveWithFailedLookupLocations>>();
177178
const perDirectoryResolvedTypeReferenceDirectives: CacheWithRedirects<ESMap<string, CachedResolvedTypeReferenceDirectiveWithFailedLookupLocations>> = createCacheWithRedirects();
179+
const typeReferenceDirectiveResolutionCache = createTypeReferenceDirectiveResolutionCache(
180+
getCurrentDirectory(),
181+
resolutionHost.getCanonicalFileName,
182+
/*options*/ undefined,
183+
moduleResolutionCache.getPackageJsonInfoCache(),
184+
perDirectoryResolvedTypeReferenceDirectives
185+
);
178186

179187
/**
180188
* These are the extensions that failed lookup files will have by default,
@@ -285,7 +293,7 @@ namespace ts {
285293

286294
function clearPerDirectoryResolutions() {
287295
moduleResolutionCache.clear();
288-
perDirectoryResolvedTypeReferenceDirectives.clear();
296+
typeReferenceDirectiveResolutionCache.clear();
289297
nonRelativeExternalModuleResolutions.forEach(watchFailedLookupLocationOfNonRelativeModuleResolutions);
290298
nonRelativeExternalModuleResolutions.clear();
291299
}
@@ -335,7 +343,7 @@ namespace ts {
335343
}
336344

337345
function resolveTypeReferenceDirective(typeReferenceDirectiveName: string, containingFile: string | undefined, options: CompilerOptions, host: ModuleResolutionHost, redirectedReference?: ResolvedProjectReference): CachedResolvedTypeReferenceDirectiveWithFailedLookupLocations {
338-
return ts.resolveTypeReferenceDirective(typeReferenceDirectiveName, containingFile, options, host, redirectedReference, moduleResolutionCache);
346+
return ts.resolveTypeReferenceDirective(typeReferenceDirectiveName, containingFile, options, host, redirectedReference, typeReferenceDirectiveResolutionCache);
339347
}
340348

341349
interface ResolveNamesWithLocalCacheInput<T extends ResolutionWithFailedLookupLocations, R extends ResolutionWithResolvedFileName> {

src/compiler/tsbuildPublic.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ namespace ts {
238238

239239
readonly compilerHost: CompilerHost;
240240
readonly moduleResolutionCache: ModuleResolutionCache | undefined;
241-
readonly packageJsonInfoCache: PackageJsonInfoCache | undefined;
241+
readonly typeReferenceDirectiveResolutionCache: TypeReferenceDirectiveResolutionCache | undefined;
242242

243243
// Mutable state
244244
buildOrder: AnyBuildOrder | undefined;
@@ -276,14 +276,14 @@ namespace ts {
276276
compilerHost.resolveModuleNames = maybeBind(host, host.resolveModuleNames);
277277
compilerHost.resolveTypeReferenceDirectives = maybeBind(host, host.resolveTypeReferenceDirectives);
278278
const moduleResolutionCache = !compilerHost.resolveModuleNames ? createModuleResolutionCache(currentDirectory, getCanonicalFileName) : undefined;
279-
const packageJsonInfoCache = !compilerHost.resolveTypeReferenceDirectives ? moduleResolutionCache || createPackageJsonInfoCache(currentDirectory, getCanonicalFileName) : undefined;
279+
const typeReferenceDirectiveResolutionCache = !compilerHost.resolveTypeReferenceDirectives ? createTypeReferenceDirectiveResolutionCache(currentDirectory, getCanonicalFileName, /*options*/ undefined, moduleResolutionCache?.getPackageJsonInfoCache()) : undefined;
280280
if (!compilerHost.resolveModuleNames) {
281281
const loader = (moduleName: string, containingFile: string, redirectedReference: ResolvedProjectReference | undefined) => resolveModuleName(moduleName, containingFile, state.projectCompilerOptions, compilerHost, moduleResolutionCache, redirectedReference).resolvedModule!;
282282
compilerHost.resolveModuleNames = (moduleNames, containingFile, _reusedNames, redirectedReference) =>
283283
loadWithLocalCache<ResolvedModuleFull>(Debug.checkEachDefined(moduleNames), containingFile, redirectedReference, loader);
284284
}
285285
if (!compilerHost.resolveTypeReferenceDirectives) {
286-
const loader = (moduleName: string, containingFile: string, redirectedReference: ResolvedProjectReference | undefined) => resolveTypeReferenceDirective(moduleName, containingFile, state.projectCompilerOptions, compilerHost, redirectedReference, state.packageJsonInfoCache).resolvedTypeReferenceDirective!;
286+
const loader = (moduleName: string, containingFile: string, redirectedReference: ResolvedProjectReference | undefined) => resolveTypeReferenceDirective(moduleName, containingFile, state.projectCompilerOptions, compilerHost, redirectedReference, state.typeReferenceDirectiveResolutionCache).resolvedTypeReferenceDirective!;
287287
compilerHost.resolveTypeReferenceDirectives = (typeReferenceDirectiveNames, containingFile, redirectedReference) =>
288288
loadWithLocalCache<ResolvedTypeReferenceDirective>(Debug.checkEachDefined(typeReferenceDirectiveNames), containingFile, redirectedReference, loader);
289289
}
@@ -317,7 +317,7 @@ namespace ts {
317317

318318
compilerHost,
319319
moduleResolutionCache,
320-
packageJsonInfoCache,
320+
typeReferenceDirectiveResolutionCache,
321321

322322
// Mutable state
323323
buildOrder: undefined,
@@ -556,7 +556,7 @@ namespace ts {
556556
function disableCache(state: SolutionBuilderState) {
557557
if (!state.cache) return;
558558

559-
const { cache, host, compilerHost, extendedConfigCache, moduleResolutionCache, packageJsonInfoCache } = state;
559+
const { cache, host, compilerHost, extendedConfigCache, moduleResolutionCache, typeReferenceDirectiveResolutionCache } = state;
560560

561561
host.readFile = cache.originalReadFile;
562562
host.fileExists = cache.originalFileExists;
@@ -567,7 +567,7 @@ namespace ts {
567567
state.readFileWithCache = cache.originalReadFileWithCache;
568568
extendedConfigCache.clear();
569569
moduleResolutionCache?.clear();
570-
packageJsonInfoCache?.clear();
570+
typeReferenceDirectiveResolutionCache?.clear();
571571
state.cache = undefined;
572572
}
573573

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4713,7 +4713,7 @@ declare namespace ts {
47134713
* This is possible in case if resolution is performed for directives specified via 'types' parameter. In this case initial path for secondary lookups
47144714
* is assumed to be the same as root directory of the project.
47154715
*/
4716-
export function resolveTypeReferenceDirective(typeReferenceDirectiveName: string, containingFile: string | undefined, options: CompilerOptions, host: ModuleResolutionHost, redirectedReference?: ResolvedProjectReference, packageJsonInfoCache?: PackageJsonInfoCache): ResolvedTypeReferenceDirectiveWithFailedLookupLocations;
4716+
export function resolveTypeReferenceDirective(typeReferenceDirectiveName: string, containingFile: string | undefined, options: CompilerOptions, host: ModuleResolutionHost, redirectedReference?: ResolvedProjectReference, cache?: TypeReferenceDirectiveResolutionCache): ResolvedTypeReferenceDirectiveWithFailedLookupLocations;
47174717
/**
47184718
* Given a set of options, returns the set of type directive names
47194719
* that should be included for this program automatically.
@@ -4723,19 +4723,24 @@ declare namespace ts {
47234723
* this list is only the set of defaults that are implicitly included.
47244724
*/
47254725
export function getAutomaticTypeDirectiveNames(options: CompilerOptions, host: ModuleResolutionHost): string[];
4726+
export interface TypeReferenceDirectiveResolutionCache extends PerDirectoryResolutionCache<ResolvedTypeReferenceDirectiveWithFailedLookupLocations>, PackageJsonInfoCache {
4727+
}
47264728
/**
4727-
* Cached module resolutions per containing directory.
4729+
* Cached resolutions per containing directory.
47284730
* This assumes that any module id will have the same resolution for sibling files located in the same folder.
47294731
*/
4730-
export interface ModuleResolutionCache extends NonRelativeModuleNameResolutionCache, PackageJsonInfoCache {
4731-
getOrCreateCacheForDirectory(directoryName: string, redirectedReference?: ResolvedProjectReference): Map<ResolvedModuleWithFailedLookupLocations>;
4732+
export interface PerDirectoryResolutionCache<T> {
4733+
getOrCreateCacheForDirectory(directoryName: string, redirectedReference?: ResolvedProjectReference): Map<T>;
47324734
clear(): void;
47334735
/**
47344736
* Updates with the current compilerOptions the cache will operate with.
47354737
* This updates the redirects map as well if needed so module resolutions are cached if they can across the projects
47364738
*/
47374739
update(options: CompilerOptions): void;
47384740
}
4741+
export interface ModuleResolutionCache extends PerDirectoryResolutionCache<ResolvedModuleWithFailedLookupLocations>, NonRelativeModuleNameResolutionCache, PackageJsonInfoCache {
4742+
getPackageJsonInfoCache(): PackageJsonInfoCache;
4743+
}
47394744
/**
47404745
* Stored map from non-relative module name to a table: directory -> result of module lookup in this directory
47414746
* We support only non-relative module names because resolution of relative module names is usually more deterministic and thus less expensive.
@@ -4751,7 +4756,7 @@ declare namespace ts {
47514756
set(directory: string, result: ResolvedModuleWithFailedLookupLocations): void;
47524757
}
47534758
export function createModuleResolutionCache(currentDirectory: string, getCanonicalFileName: (s: string) => string, options?: CompilerOptions): ModuleResolutionCache;
4754-
export function createPackageJsonInfoCache(currentDirectory: string, getCanonicalFileName: (s: string) => string): PackageJsonInfoCache;
4759+
export function createTypeReferenceDirectiveResolutionCache(currentDirectory: string, getCanonicalFileName: (s: string) => string, options?: CompilerOptions, packageJsonInfoCache?: PackageJsonInfoCache): TypeReferenceDirectiveResolutionCache;
47554760
export function resolveModuleNameFromCache(moduleName: string, containingFile: string, cache: ModuleResolutionCache): ResolvedModuleWithFailedLookupLocations | undefined;
47564761
export function resolveModuleName(moduleName: string, containingFile: string, compilerOptions: CompilerOptions, host: ModuleResolutionHost, cache?: ModuleResolutionCache, redirectedReference?: ResolvedProjectReference): ResolvedModuleWithFailedLookupLocations;
47574762
export function nodeModuleNameResolver(moduleName: string, containingFile: string, compilerOptions: CompilerOptions, host: ModuleResolutionHost, cache?: ModuleResolutionCache, redirectedReference?: ResolvedProjectReference): ResolvedModuleWithFailedLookupLocations;

tests/baselines/reference/api/typescript.d.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4713,7 +4713,7 @@ declare namespace ts {
47134713
* This is possible in case if resolution is performed for directives specified via 'types' parameter. In this case initial path for secondary lookups
47144714
* is assumed to be the same as root directory of the project.
47154715
*/
4716-
export function resolveTypeReferenceDirective(typeReferenceDirectiveName: string, containingFile: string | undefined, options: CompilerOptions, host: ModuleResolutionHost, redirectedReference?: ResolvedProjectReference, packageJsonInfoCache?: PackageJsonInfoCache): ResolvedTypeReferenceDirectiveWithFailedLookupLocations;
4716+
export function resolveTypeReferenceDirective(typeReferenceDirectiveName: string, containingFile: string | undefined, options: CompilerOptions, host: ModuleResolutionHost, redirectedReference?: ResolvedProjectReference, cache?: TypeReferenceDirectiveResolutionCache): ResolvedTypeReferenceDirectiveWithFailedLookupLocations;
47174717
/**
47184718
* Given a set of options, returns the set of type directive names
47194719
* that should be included for this program automatically.
@@ -4723,19 +4723,24 @@ declare namespace ts {
47234723
* this list is only the set of defaults that are implicitly included.
47244724
*/
47254725
export function getAutomaticTypeDirectiveNames(options: CompilerOptions, host: ModuleResolutionHost): string[];
4726+
export interface TypeReferenceDirectiveResolutionCache extends PerDirectoryResolutionCache<ResolvedTypeReferenceDirectiveWithFailedLookupLocations>, PackageJsonInfoCache {
4727+
}
47264728
/**
4727-
* Cached module resolutions per containing directory.
4729+
* Cached resolutions per containing directory.
47284730
* This assumes that any module id will have the same resolution for sibling files located in the same folder.
47294731
*/
4730-
export interface ModuleResolutionCache extends NonRelativeModuleNameResolutionCache, PackageJsonInfoCache {
4731-
getOrCreateCacheForDirectory(directoryName: string, redirectedReference?: ResolvedProjectReference): Map<ResolvedModuleWithFailedLookupLocations>;
4732+
export interface PerDirectoryResolutionCache<T> {
4733+
getOrCreateCacheForDirectory(directoryName: string, redirectedReference?: ResolvedProjectReference): Map<T>;
47324734
clear(): void;
47334735
/**
47344736
* Updates with the current compilerOptions the cache will operate with.
47354737
* This updates the redirects map as well if needed so module resolutions are cached if they can across the projects
47364738
*/
47374739
update(options: CompilerOptions): void;
47384740
}
4741+
export interface ModuleResolutionCache extends PerDirectoryResolutionCache<ResolvedModuleWithFailedLookupLocations>, NonRelativeModuleNameResolutionCache, PackageJsonInfoCache {
4742+
getPackageJsonInfoCache(): PackageJsonInfoCache;
4743+
}
47394744
/**
47404745
* Stored map from non-relative module name to a table: directory -> result of module lookup in this directory
47414746
* We support only non-relative module names because resolution of relative module names is usually more deterministic and thus less expensive.
@@ -4751,7 +4756,7 @@ declare namespace ts {
47514756
set(directory: string, result: ResolvedModuleWithFailedLookupLocations): void;
47524757
}
47534758
export function createModuleResolutionCache(currentDirectory: string, getCanonicalFileName: (s: string) => string, options?: CompilerOptions): ModuleResolutionCache;
4754-
export function createPackageJsonInfoCache(currentDirectory: string, getCanonicalFileName: (s: string) => string): PackageJsonInfoCache;
4759+
export function createTypeReferenceDirectiveResolutionCache(currentDirectory: string, getCanonicalFileName: (s: string) => string, options?: CompilerOptions, packageJsonInfoCache?: PackageJsonInfoCache): TypeReferenceDirectiveResolutionCache;
47554760
export function resolveModuleNameFromCache(moduleName: string, containingFile: string, cache: ModuleResolutionCache): ResolvedModuleWithFailedLookupLocations | undefined;
47564761
export function resolveModuleName(moduleName: string, containingFile: string, compilerOptions: CompilerOptions, host: ModuleResolutionHost, cache?: ModuleResolutionCache, redirectedReference?: ResolvedProjectReference): ResolvedModuleWithFailedLookupLocations;
47574762
export function nodeModuleNameResolver(moduleName: string, containingFile: string, compilerOptions: CompilerOptions, host: ModuleResolutionHost, cache?: ModuleResolutionCache, redirectedReference?: ResolvedProjectReference): ResolvedModuleWithFailedLookupLocations;

0 commit comments

Comments
 (0)