Skip to content

Commit 8919029

Browse files
Avoid leaning on the WeakMap when we can cache on ConfigFileSpecs.
1 parent 3d4bc20 commit 8919029

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed

src/compiler/commandLineParser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3042,6 +3042,7 @@ function parseJsonConfigFileContentWorker(
30423042
validatedFilesSpecBeforeSubstitution,
30433043
validatedIncludeSpecsBeforeSubstitution,
30443044
validatedExcludeSpecsBeforeSubstitution,
3045+
pathPatterns: undefined, // Initialized on first use
30453046
isDefaultIncludeSpec,
30463047
};
30473048
}

src/compiler/moduleNameResolver.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,7 +1559,7 @@ function tryLoadModuleUsingOptionalResolutionSettings(extensions: Extensions, mo
15591559
}
15601560

15611561
function tryLoadModuleUsingPathsIfEligible(extensions: Extensions, moduleName: string, loader: ResolutionKindSpecificLoader, state: ModuleResolutionState) {
1562-
const { baseUrl, paths } = state.compilerOptions;
1562+
const { baseUrl, paths, configFile } = state.compilerOptions;
15631563
if (paths && !pathIsRelative(moduleName)) {
15641564
if (state.traceEnabled) {
15651565
if (baseUrl) {
@@ -1569,7 +1569,15 @@ function tryLoadModuleUsingPathsIfEligible(extensions: Extensions, moduleName: s
15691569
}
15701570
const baseDirectory = getPathsBasePath(state.compilerOptions, state.host)!; // Always defined when 'paths' is defined
15711571
// TODO: do we need to sort by aggregate length?
1572-
const pathPatterns = tryParsePatterns(paths);
1572+
let pathPatterns;
1573+
if (configFile?.configFileSpecs) {
1574+
// Cache on this object instead of internally through a WeakMap.
1575+
// This seems to do a bit better.
1576+
pathPatterns = configFile.configFileSpecs.pathPatterns ??= tryParsePatterns(paths, /*shouldCache*/ false);
1577+
}
1578+
else {
1579+
pathPatterns = tryParsePatterns(paths);
1580+
}
15731581
return tryLoadModuleUsingPaths(extensions, moduleName, baseDirectory, paths, pathPatterns, loader, /*onlyRecordFailures*/ false, state);
15741582
}
15751583
}

src/compiler/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
OptionsNameMap,
1313
PackageJsonInfo,
1414
PackageJsonInfoCache,
15+
ParsedPatterns,
1516
Pattern,
1617
SymlinkCache,
1718
ThisContainer,
@@ -7512,6 +7513,7 @@ export interface ConfigFileSpecs {
75127513
validatedFilesSpecBeforeSubstitution: readonly string[] | undefined;
75137514
validatedIncludeSpecsBeforeSubstitution: readonly string[] | undefined;
75147515
validatedExcludeSpecsBeforeSubstitution: readonly string[] | undefined;
7516+
pathPatterns: ParsedPatterns | undefined;
75157517
isDefaultIncludeSpec: boolean;
75167518
}
75177519

src/compiler/utilities.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10020,10 +10020,13 @@ const parsedPatternsCache = new WeakMap<MapLike<string[]>, ParsedPatterns>();
1002010020
*
1002110021
* @internal
1002210022
*/
10023-
export function tryParsePatterns(paths: MapLike<string[]>, sortByAggregateLength: boolean = false): ParsedPatterns {
10024-
let result = parsedPatternsCache.get(paths);
10025-
if (result !== undefined) {
10026-
return result;
10023+
export function tryParsePatterns(paths: MapLike<string[]>, shouldCache: boolean = true, sortByAggregateLength: boolean = false): ParsedPatterns {
10024+
let result: ParsedPatterns | undefined;
10025+
if (shouldCache) {
10026+
result = parsedPatternsCache.get(paths);
10027+
if (result !== undefined) {
10028+
return result;
10029+
}
1002710030
}
1002810031

1002910032
let matchableStringSet: Set<string> | undefined;
@@ -10051,13 +10054,14 @@ export function tryParsePatterns(paths: MapLike<string[]>, sortByAggregateLength
1005110054
return prefixComparison;
1005210055
});
1005310056

10054-
parsedPatternsCache.set(
10055-
paths,
10056-
result = {
10057-
matchableStringSet,
10058-
sortedPatterns,
10059-
},
10060-
);
10057+
result = {
10058+
matchableStringSet,
10059+
sortedPatterns,
10060+
};
10061+
10062+
if (shouldCache) {
10063+
parsedPatternsCache.set(paths, result);
10064+
}
1006110065

1006210066
return result;
1006310067
}

0 commit comments

Comments
 (0)