From 8919029ce8d49c244dd9698d5cc2539bac63680a Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 28 Jun 2024 01:29:10 +0000 Subject: [PATCH] Avoid leaning on the WeakMap when we can cache on `ConfigFileSpecs`. --- src/compiler/commandLineParser.ts | 1 + src/compiler/moduleNameResolver.ts | 12 ++++++++++-- src/compiler/types.ts | 2 ++ src/compiler/utilities.ts | 26 +++++++++++++++----------- 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 4368d1039ce20..5c0a33c6e46bb 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -3042,6 +3042,7 @@ function parseJsonConfigFileContentWorker( validatedFilesSpecBeforeSubstitution, validatedIncludeSpecsBeforeSubstitution, validatedExcludeSpecsBeforeSubstitution, + pathPatterns: undefined, // Initialized on first use isDefaultIncludeSpec, }; } diff --git a/src/compiler/moduleNameResolver.ts b/src/compiler/moduleNameResolver.ts index 5291540dee4e4..64fc43b538f85 100644 --- a/src/compiler/moduleNameResolver.ts +++ b/src/compiler/moduleNameResolver.ts @@ -1559,7 +1559,7 @@ function tryLoadModuleUsingOptionalResolutionSettings(extensions: Extensions, mo } function tryLoadModuleUsingPathsIfEligible(extensions: Extensions, moduleName: string, loader: ResolutionKindSpecificLoader, state: ModuleResolutionState) { - const { baseUrl, paths } = state.compilerOptions; + const { baseUrl, paths, configFile } = state.compilerOptions; if (paths && !pathIsRelative(moduleName)) { if (state.traceEnabled) { if (baseUrl) { @@ -1569,7 +1569,15 @@ function tryLoadModuleUsingPathsIfEligible(extensions: Extensions, moduleName: s } const baseDirectory = getPathsBasePath(state.compilerOptions, state.host)!; // Always defined when 'paths' is defined // TODO: do we need to sort by aggregate length? - const pathPatterns = tryParsePatterns(paths); + let pathPatterns; + if (configFile?.configFileSpecs) { + // Cache on this object instead of internally through a WeakMap. + // This seems to do a bit better. + pathPatterns = configFile.configFileSpecs.pathPatterns ??= tryParsePatterns(paths, /*shouldCache*/ false); + } + else { + pathPatterns = tryParsePatterns(paths); + } return tryLoadModuleUsingPaths(extensions, moduleName, baseDirectory, paths, pathPatterns, loader, /*onlyRecordFailures*/ false, state); } } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 53e0de202c9a4..3ee2a197e1bbd 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -12,6 +12,7 @@ import { OptionsNameMap, PackageJsonInfo, PackageJsonInfoCache, + ParsedPatterns, Pattern, SymlinkCache, ThisContainer, @@ -7512,6 +7513,7 @@ export interface ConfigFileSpecs { validatedFilesSpecBeforeSubstitution: readonly string[] | undefined; validatedIncludeSpecsBeforeSubstitution: readonly string[] | undefined; validatedExcludeSpecsBeforeSubstitution: readonly string[] | undefined; + pathPatterns: ParsedPatterns | undefined; isDefaultIncludeSpec: boolean; } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 560fc7b90ac28..48785426c60bd 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -10020,10 +10020,13 @@ const parsedPatternsCache = new WeakMap, ParsedPatterns>(); * * @internal */ -export function tryParsePatterns(paths: MapLike, sortByAggregateLength: boolean = false): ParsedPatterns { - let result = parsedPatternsCache.get(paths); - if (result !== undefined) { - return result; +export function tryParsePatterns(paths: MapLike, shouldCache: boolean = true, sortByAggregateLength: boolean = false): ParsedPatterns { + let result: ParsedPatterns | undefined; + if (shouldCache) { + result = parsedPatternsCache.get(paths); + if (result !== undefined) { + return result; + } } let matchableStringSet: Set | undefined; @@ -10051,13 +10054,14 @@ export function tryParsePatterns(paths: MapLike, sortByAggregateLength return prefixComparison; }); - parsedPatternsCache.set( - paths, - result = { - matchableStringSet, - sortedPatterns, - }, - ); + result = { + matchableStringSet, + sortedPatterns, + }; + + if (shouldCache) { + parsedPatternsCache.set(paths, result); + } return result; }