Skip to content

Commit e6f3baf

Browse files
Optimize path mapping lookups (#59048)
1 parent 497316f commit e6f3baf

File tree

5 files changed

+63
-22
lines changed

5 files changed

+63
-22
lines changed

src/compiler/commandLineParser.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3071,7 +3071,6 @@ function parseJsonConfigFileContentWorker(
30713071
validatedFilesSpecBeforeSubstitution,
30723072
validatedIncludeSpecsBeforeSubstitution,
30733073
validatedExcludeSpecsBeforeSubstitution,
3074-
pathPatterns: undefined, // Initialized on first use
30753074
isDefaultIncludeSpec,
30763075
};
30773076
}

src/compiler/core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2414,7 +2414,7 @@ export function findBestPatternMatch<T>(values: readonly T[], getPattern: (value
24142414
for (let i = 0; i < values.length; i++) {
24152415
const v = values[i];
24162416
const pattern = getPattern(v);
2417-
if (isPatternMatch(pattern, candidate) && pattern.prefix.length > longestMatchPrefixLength) {
2417+
if (pattern.prefix.length > longestMatchPrefixLength && isPatternMatch(pattern, candidate)) {
24182418
longestMatchPrefixLength = pattern.prefix.length;
24192419
matchedValue = v;
24202420
}

src/compiler/moduleNameResolver.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ import {
7979
normalizeSlashes,
8080
PackageId,
8181
packageIdToString,
82+
ParsedPatterns,
8283
Path,
8384
pathIsRelative,
84-
Pattern,
8585
patternText,
8686
readJson,
8787
removeExtension,
@@ -1558,7 +1558,7 @@ function tryLoadModuleUsingOptionalResolutionSettings(extensions: Extensions, mo
15581558
}
15591559

15601560
function tryLoadModuleUsingPathsIfEligible(extensions: Extensions, moduleName: string, loader: ResolutionKindSpecificLoader, state: ModuleResolutionState) {
1561-
const { baseUrl, paths, configFile } = state.compilerOptions;
1561+
const { baseUrl, paths } = state.compilerOptions;
15621562
if (paths && !pathIsRelative(moduleName)) {
15631563
if (state.traceEnabled) {
15641564
if (baseUrl) {
@@ -1567,7 +1567,7 @@ function tryLoadModuleUsingPathsIfEligible(extensions: Extensions, moduleName: s
15671567
trace(state.host, Diagnostics.paths_option_is_specified_looking_for_a_pattern_to_match_module_name_0, moduleName);
15681568
}
15691569
const baseDirectory = getPathsBasePath(state.compilerOptions, state.host)!; // Always defined when 'paths' is defined
1570-
const pathPatterns = configFile?.configFileSpecs ? configFile.configFileSpecs.pathPatterns ||= tryParsePatterns(paths) : undefined;
1570+
const pathPatterns = tryParsePatterns(paths);
15711571
return tryLoadModuleUsingPaths(extensions, moduleName, baseDirectory, paths, pathPatterns, loader, /*onlyRecordFailures*/ false, state);
15721572
}
15731573
}
@@ -2518,7 +2518,8 @@ function loadNodeModuleFromDirectoryWorker(extensions: Extensions, candidate: st
25182518
if (state.traceEnabled) {
25192519
trace(state.host, Diagnostics.package_json_has_a_typesVersions_entry_0_that_matches_compiler_version_1_looking_for_a_pattern_to_match_module_name_2, versionPaths.version, version, moduleName);
25202520
}
2521-
const result = tryLoadModuleUsingPaths(extensions, moduleName, candidate, versionPaths.paths, /*pathPatterns*/ undefined, loader, onlyRecordFailuresForPackageFile || onlyRecordFailuresForIndex, state);
2521+
const pathPatterns = tryParsePatterns(versionPaths.paths);
2522+
const result = tryLoadModuleUsingPaths(extensions, moduleName, candidate, versionPaths.paths, pathPatterns, loader, onlyRecordFailuresForPackageFile || onlyRecordFailuresForIndex, state);
25222523
if (result) {
25232524
return removeIgnoredPackageId(result.value);
25242525
}
@@ -3112,16 +3113,16 @@ function loadModuleFromSpecificNodeModulesDirectory(extensions: Extensions, modu
31123113
trace(state.host, Diagnostics.package_json_has_a_typesVersions_entry_0_that_matches_compiler_version_1_looking_for_a_pattern_to_match_module_name_2, versionPaths.version, version, rest);
31133114
}
31143115
const packageDirectoryExists = nodeModulesDirectoryExists && directoryProbablyExists(packageDirectory, state.host);
3115-
const fromPaths = tryLoadModuleUsingPaths(extensions, rest, packageDirectory, versionPaths.paths, /*pathPatterns*/ undefined, loader, !packageDirectoryExists, state);
3116+
const pathPatterns = tryParsePatterns(versionPaths.paths);
3117+
const fromPaths = tryLoadModuleUsingPaths(extensions, rest, packageDirectory, versionPaths.paths, pathPatterns, loader, !packageDirectoryExists, state);
31163118
if (fromPaths) {
31173119
return fromPaths.value;
31183120
}
31193121
}
31203122
return loader(extensions, candidate, !nodeModulesDirectoryExists, state);
31213123
}
31223124

3123-
function tryLoadModuleUsingPaths(extensions: Extensions, moduleName: string, baseDirectory: string, paths: MapLike<string[]>, pathPatterns: readonly (string | Pattern)[] | undefined, loader: ResolutionKindSpecificLoader, onlyRecordFailures: boolean, state: ModuleResolutionState): SearchResult<Resolved> {
3124-
pathPatterns ||= tryParsePatterns(paths);
3125+
function tryLoadModuleUsingPaths(extensions: Extensions, moduleName: string, baseDirectory: string, paths: MapLike<string[]>, pathPatterns: ParsedPatterns, loader: ResolutionKindSpecificLoader, onlyRecordFailures: boolean, state: ModuleResolutionState): SearchResult<Resolved> {
31253126
const matchedPattern = matchPatternOrExact(pathPatterns, moduleName);
31263127
if (matchedPattern) {
31273128
const matchedStar = isString(matchedPattern) ? undefined : matchedText(matchedPattern, moduleName);

src/compiler/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7628,7 +7628,6 @@ export interface ConfigFileSpecs {
76287628
validatedFilesSpecBeforeSubstitution: readonly string[] | undefined;
76297629
validatedIncludeSpecsBeforeSubstitution: readonly string[] | undefined;
76307630
validatedExcludeSpecsBeforeSubstitution: readonly string[] | undefined;
7631-
pathPatterns: readonly (string | Pattern)[] | undefined;
76327631
isDefaultIncludeSpec: boolean;
76337632
}
76347633

src/compiler/utilities.ts

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9972,8 +9972,51 @@ export function tryParsePattern(pattern: string): string | Pattern | undefined {
99729972
}
99739973

99749974
/** @internal */
9975-
export function tryParsePatterns(paths: MapLike<string[]>): (string | Pattern)[] {
9976-
return mapDefined(getOwnKeys(paths), path => tryParsePattern(path));
9975+
export interface ParsedPatterns {
9976+
matchableStringSet: ReadonlySet<string> | undefined;
9977+
patterns: (readonly Pattern[]) | undefined;
9978+
}
9979+
9980+
const parsedPatternsCache = new WeakMap<MapLike<string[]>, ParsedPatterns>();
9981+
9982+
/**
9983+
* Divides patterns into a set of exact specifiers and patterns.
9984+
* NOTE that this function caches the result based on object identity.
9985+
*
9986+
* @internal
9987+
*/
9988+
export function tryParsePatterns(paths: MapLike<string[]>): ParsedPatterns {
9989+
let result = parsedPatternsCache.get(paths);
9990+
if (result !== undefined) {
9991+
return result;
9992+
}
9993+
9994+
let matchableStringSet: Set<string> | undefined;
9995+
let patterns: Pattern[] | undefined;
9996+
9997+
const pathList = getOwnKeys(paths);
9998+
for (const path of pathList) {
9999+
const patternOrStr = tryParsePattern(path);
10000+
if (patternOrStr === undefined) {
10001+
continue;
10002+
}
10003+
else if (typeof patternOrStr === "string") {
10004+
(matchableStringSet ??= new Set()).add(patternOrStr);
10005+
}
10006+
else {
10007+
(patterns ??= []).push(patternOrStr);
10008+
}
10009+
}
10010+
10011+
parsedPatternsCache.set(
10012+
paths,
10013+
result = {
10014+
matchableStringSet,
10015+
patterns,
10016+
},
10017+
);
10018+
10019+
return result;
997710020
}
997810021

997910022
/** @internal */
@@ -10030,22 +10073,21 @@ export const emptyFileSystemEntries: FileSystemEntries = {
1003010073
};
1003110074

1003210075
/**
10033-
* patternOrStrings contains both patterns (containing "*") and regular strings.
10076+
* `parsedPatterns` contains both patterns (containing "*") and regular strings.
1003410077
* Return an exact match if possible, or a pattern match, or undefined.
1003510078
* (These are verified by verifyCompilerOptions to have 0 or 1 "*" characters.)
1003610079
*
1003710080
* @internal
1003810081
*/
10039-
export function matchPatternOrExact(patternOrStrings: readonly (string | Pattern)[], candidate: string): string | Pattern | undefined {
10040-
const patterns: Pattern[] = [];
10041-
for (const patternOrString of patternOrStrings) {
10042-
if (patternOrString === candidate) {
10043-
return candidate;
10044-
}
10082+
export function matchPatternOrExact(parsedPatterns: ParsedPatterns, candidate: string): string | Pattern | undefined {
10083+
const { matchableStringSet, patterns } = parsedPatterns;
1004510084

10046-
if (!isString(patternOrString)) {
10047-
patterns.push(patternOrString);
10048-
}
10085+
if (matchableStringSet?.has(candidate)) {
10086+
return candidate;
10087+
}
10088+
10089+
if (patterns === undefined || patterns.length === 0) {
10090+
return undefined;
1004910091
}
1005010092

1005110093
return findBestPatternMatch(patterns, _ => _, candidate);

0 commit comments

Comments
 (0)