Skip to content

Commit

Permalink
Avoid leaning on the WeakMap when we can cache on ConfigFileSpecs.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielRosenwasser committed Jun 28, 2024
1 parent 3d4bc20 commit 8919029
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3042,6 +3042,7 @@ function parseJsonConfigFileContentWorker(
validatedFilesSpecBeforeSubstitution,
validatedIncludeSpecsBeforeSubstitution,
validatedExcludeSpecsBeforeSubstitution,
pathPatterns: undefined, // Initialized on first use
isDefaultIncludeSpec,
};
}
Expand Down
12 changes: 10 additions & 2 deletions src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
OptionsNameMap,
PackageJsonInfo,
PackageJsonInfoCache,
ParsedPatterns,
Pattern,
SymlinkCache,
ThisContainer,
Expand Down Expand Up @@ -7512,6 +7513,7 @@ export interface ConfigFileSpecs {
validatedFilesSpecBeforeSubstitution: readonly string[] | undefined;
validatedIncludeSpecsBeforeSubstitution: readonly string[] | undefined;
validatedExcludeSpecsBeforeSubstitution: readonly string[] | undefined;
pathPatterns: ParsedPatterns | undefined;
isDefaultIncludeSpec: boolean;
}

Expand Down
26 changes: 15 additions & 11 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10020,10 +10020,13 @@ const parsedPatternsCache = new WeakMap<MapLike<string[]>, ParsedPatterns>();
*
* @internal
*/
export function tryParsePatterns(paths: MapLike<string[]>, sortByAggregateLength: boolean = false): ParsedPatterns {
let result = parsedPatternsCache.get(paths);
if (result !== undefined) {
return result;
export function tryParsePatterns(paths: MapLike<string[]>, 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<string> | undefined;
Expand Down Expand Up @@ -10051,13 +10054,14 @@ export function tryParsePatterns(paths: MapLike<string[]>, sortByAggregateLength
return prefixComparison;
});

parsedPatternsCache.set(
paths,
result = {
matchableStringSet,
sortedPatterns,
},
);
result = {
matchableStringSet,
sortedPatterns,
};

if (shouldCache) {
parsedPatternsCache.set(paths, result);
}

return result;
}
Expand Down

0 comments on commit 8919029

Please sign in to comment.