Skip to content

Commit b945b9b

Browse files
committed
Cache parsed path mapping patterns
If a project has many of them (e.g. 1800), parsing the patterns repeatedly can take up a lot of time.
1 parent 3938958 commit b945b9b

File tree

4 files changed

+44
-31
lines changed

4 files changed

+44
-31
lines changed

src/compiler/binder.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1977,19 +1977,17 @@ namespace ts {
19771977
declareModuleSymbol(node);
19781978
}
19791979
else {
1980-
let pattern: Pattern | undefined;
1980+
let pattern: string | Pattern | undefined;
19811981
if (node.name.kind === SyntaxKind.StringLiteral) {
19821982
const { text } = node.name;
1983-
if (hasZeroOrOneAsteriskCharacter(text)) {
1984-
pattern = tryParsePattern(text);
1985-
}
1986-
else {
1983+
pattern = tryParsePattern(text);
1984+
if (pattern === undefined) {
19871985
errorOnFirstToken(node.name, Diagnostics.Pattern_0_can_have_at_most_one_Asterisk_character, text);
19881986
}
19891987
}
19901988

19911989
const symbol = declareSymbolAndAddToSymbolTable(node, SymbolFlags.ValueModule, SymbolFlags.ValueModuleExcludes)!;
1992-
file.patternAmbientModules = append<PatternAmbientModule>(file.patternAmbientModules, pattern && { pattern, symbol });
1990+
file.patternAmbientModules = append<PatternAmbientModule>(file.patternAmbientModules, pattern && !isString(pattern) ? { pattern, symbol } : undefined);
19931991
}
19941992
}
19951993
else {

src/compiler/moduleNameResolver.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,14 @@ namespace ts {
9696
};
9797
}
9898

99+
function tryParsePatterns(paths: MapLike<string[]>): (string | Pattern)[] {
100+
return mapDefined(getOwnKeys(paths), path => tryParsePattern(path));
101+
}
102+
99103
interface ModuleResolutionState {
100104
host: ModuleResolutionHost;
101105
compilerOptions: CompilerOptions;
106+
compilerPathPatterns?: (string | Pattern)[];
102107
traceEnabled: boolean;
103108
failedLookupLocations: Push<string>;
104109
resultFromCache?: ResolvedModuleWithFailedLookupLocations;
@@ -961,7 +966,10 @@ namespace ts {
961966
trace(state.host, Diagnostics.paths_option_is_specified_looking_for_a_pattern_to_match_module_name_0, moduleName);
962967
}
963968
const baseDirectory = getPathsBasePath(state.compilerOptions, state.host)!; // Always defined when 'paths' is defined
964-
return tryLoadModuleUsingPaths(extensions, moduleName, baseDirectory, paths, loader, /*onlyRecordFailures*/ false, state);
969+
if (!state.compilerPathPatterns) {
970+
state.compilerPathPatterns = tryParsePatterns(paths);
971+
}
972+
return tryLoadModuleUsingPaths(extensions, moduleName, baseDirectory, paths, state.compilerPathPatterns, loader, /*onlyRecordFailures*/ false, state);
965973
}
966974
}
967975

@@ -1400,7 +1408,8 @@ namespace ts {
14001408
if (state.traceEnabled) {
14011409
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);
14021410
}
1403-
const result = tryLoadModuleUsingPaths(extensions, moduleName, candidate, versionPaths.paths, loader, onlyRecordFailuresForPackageFile || onlyRecordFailuresForIndex, state);
1411+
const paths = versionPaths.paths;
1412+
const result = tryLoadModuleUsingPaths(extensions, moduleName, candidate, paths, tryParsePatterns(paths), loader, onlyRecordFailuresForPackageFile || onlyRecordFailuresForIndex, state);
14041413
if (result) {
14051414
return removeIgnoredPackageId(result.value);
14061415
}
@@ -1536,7 +1545,8 @@ namespace ts {
15361545
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, packageInfo.versionPaths.version, version, rest);
15371546
}
15381547
const packageDirectoryExists = nodeModulesDirectoryExists && directoryProbablyExists(packageDirectory, state.host);
1539-
const fromPaths = tryLoadModuleUsingPaths(extensions, rest, packageDirectory, packageInfo.versionPaths.paths, loader, !packageDirectoryExists, state);
1548+
const paths = packageInfo.versionPaths.paths;
1549+
const fromPaths = tryLoadModuleUsingPaths(extensions, rest, packageDirectory, paths, tryParsePatterns(paths), loader, !packageDirectoryExists, state);
15401550
if (fromPaths) {
15411551
return fromPaths.value;
15421552
}
@@ -1546,8 +1556,8 @@ namespace ts {
15461556
return loader(extensions, candidate, !nodeModulesDirectoryExists, state);
15471557
}
15481558

1549-
function tryLoadModuleUsingPaths(extensions: Extensions, moduleName: string, baseDirectory: string, paths: MapLike<string[]>, loader: ResolutionKindSpecificLoader, onlyRecordFailures: boolean, state: ModuleResolutionState): SearchResult<Resolved> {
1550-
const matchedPattern = matchPatternOrExact(getOwnKeys(paths), moduleName);
1559+
function tryLoadModuleUsingPaths(extensions: Extensions, moduleName: string, baseDirectory: string, paths: MapLike<string[]>, pathPatterns: (string | Pattern)[], loader: ResolutionKindSpecificLoader, onlyRecordFailures: boolean, state: ModuleResolutionState): SearchResult<Resolved> {
1560+
const matchedPattern = matchPatternOrExact(pathPatterns, moduleName);
15511561
if (matchedPattern) {
15521562
const matchedStar = isString(matchedPattern) ? undefined : matchedText(matchedPattern, moduleName);
15531563
const matchedPatternText = isString(matchedPattern) ? matchedPattern : patternText(matchedPattern);

src/compiler/utilities.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6769,14 +6769,21 @@ namespace ts {
67696769
return changeAnyExtension(path, newExtension, extensionsToRemove, /*ignoreCase*/ false) as T;
67706770
}
67716771

6772-
export function tryParsePattern(pattern: string): Pattern | undefined {
6773-
// This should be verified outside of here and a proper error thrown.
6774-
Debug.assert(hasZeroOrOneAsteriskCharacter(pattern));
6772+
/**
6773+
* Returns the input if there are no stars, a pattern if there is exactly one,
6774+
* and undefined if there are more.
6775+
*/
6776+
export function tryParsePattern(pattern: string): string | Pattern | undefined {
67756777
const indexOfStar = pattern.indexOf("*");
6776-
return indexOfStar === -1 ? undefined : {
6777-
prefix: pattern.substr(0, indexOfStar),
6778-
suffix: pattern.substr(indexOfStar + 1)
6779-
};
6778+
if (indexOfStar === -1) {
6779+
return pattern;
6780+
}
6781+
return pattern.indexOf("*", indexOfStar + 1) !== -1
6782+
? undefined
6783+
: {
6784+
prefix: pattern.substr(0, indexOfStar),
6785+
suffix: pattern.substr(indexOfStar + 1)
6786+
};
67806787
}
67816788

67826789
export function positionIsSynthesized(pos: number): boolean {
@@ -6822,21 +6829,19 @@ namespace ts {
68226829

68236830

68246831
/**
6825-
* patternStrings contains both pattern strings (containing "*") and regular strings.
6832+
* patternOrStrings contains both patterns (containing "*") and regular strings.
68266833
* Return an exact match if possible, or a pattern match, or undefined.
68276834
* (These are verified by verifyCompilerOptions to have 0 or 1 "*" characters.)
68286835
*/
6829-
export function matchPatternOrExact(patternStrings: readonly string[], candidate: string): string | Pattern | undefined {
6836+
export function matchPatternOrExact(patternOrStrings: readonly (string | Pattern)[], candidate: string): string | Pattern | undefined {
68306837
const patterns: Pattern[] = [];
6831-
for (const patternString of patternStrings) {
6832-
if (!hasZeroOrOneAsteriskCharacter(patternString)) continue;
6833-
const pattern = tryParsePattern(patternString);
6834-
if (pattern) {
6835-
patterns.push(pattern);
6836-
}
6837-
else if (patternString === candidate) {
6838-
// pattern was matched as is - no need to search further
6839-
return patternString;
6838+
for (const patternOrString of patternOrStrings) {
6839+
if (patternOrString === candidate) {
6840+
return candidate;
6841+
}
6842+
6843+
if (!isString(patternOrString)) {
6844+
patterns.push(patternOrString);
68406845
}
68416846
}
68426847

src/services/stringCompletions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,8 +553,8 @@ namespace ts.Completions.StringCompletions {
553553
return undefined;
554554
}
555555

556-
const parsed = hasZeroOrOneAsteriskCharacter(pattern) ? tryParsePattern(pattern) : undefined;
557-
if (!parsed) {
556+
const parsed = tryParsePattern(pattern);
557+
if (parsed === undefined || isString(parsed)) {
558558
return undefined;
559559
}
560560

0 commit comments

Comments
 (0)