8000 Start using cache as perDirectory lookup · microsoft/TypeScript@2219b5d · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit 2219b5d

Browse files
committed
Start using cache as perDirectory lookup
1 parent 84cda33 commit 2219b5d

27 files changed

+28088
-13155
lines changed

src/compiler/builder.ts

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,41 +1439,22 @@ function toPerDirectoryAndNonRelativeNameCache<T>(
14391439
cache: ModeAwareCache<T> | undefined,
14401440
fOrPath: SourceFile | Path,
14411441
) {
1442-
if (!cache?.size()) return perDirectoryAndNonRelativeNameCache;
1443-
let dirPath: Path, redirectedReference: ResolvedProjectReference | undefined;
1444-
if (!isString(fOrPath)) {
1445-
redirectedReference = state.program!.getRedirectReferenceForResolution(fOrPath);
1446-
dirPath = getDirectoryPath(fOrPath.path);
1447-
}
1448-
else {
1449-
dirPath = getDirectoryPath(fOrPath);
1450-
}
1451-
const mapForRedirects = perDirectoryAndNonRelativeNameCache?.perDirectory.perDirectoryMap.getMapOfCacheRedirects(redirectedReference);
1452-
let dirCache = mapForRedirects?.get(dirPath);
1453-
cache.forEach((resolution, name, mode) => {
1454-
if (!getResolvedFileName(resolution)) return;
1455-
if (dirCache?.has(name, mode)) return;
1456-
// If there was already external module resolution that is same, set for child directory, dont set resolution for this directory
1457-
if (!isExternalModuleNameRelative(name) &&
1458-
perDirectoryAndNonRelativeNameCache?.nonRelativeName.getFromNonRelativeNameCacheWithPath(name, mode, dirPath, redirectedReference)) {
1459-
return;
1460-
}
1461-
(dirCache ??= (perDirectoryAndNonRelativeNameCache ??= createPerDirectoryAndNonRelativeNameCache(
1462-
state.program!.getCurrentDirectory(),
1463-
state.program!.getCanonicalFileName,
1464-
state.compilerOptions,
1465-
getResolvedFileName,
1466-
)).perDirectory.getOrCreateCacheForDirectoryWithPath(dirPath, redirectedReference)).set(name, mode, resolution);
1467-
// put result in per-module name cache and delete everything that is not needed
1468-
if (!isExternalModuleNameRelative(name)) {
1469-
perDirectoryAndNonRelativeNameCache!.nonRelativeName.getOrCreateCacheForNonRelativeName(name, mode, redirectedReference).setWithPath(
1470-
dirPath,
1471-
resolution,
1472-
ancestorPath => mapForRedirects?.get(ancestorPath)?.delete(name, mode),
1473-
);
1474-
}
1475-
});
1476-
return perDirectoryAndNonRelativeNameCache;
1442+
return ts.toPerDirectoryAndNonRelativeNameCache(
1443+
state.program!,
1444+
perDirectoryAndNonRelativeNameCache,
1445+
getResolvedFileName,
1446+
cache,
1447+
fOrPath,
1448+
noop,
1449+
(resolution, name, mode, dirPath, redirectedReference) =>
1450+
// If this is not resolved, dont put in per dir Cache
1451+
!getResolvedFileName(resolution) ||
1452+
// If there was already external module resolution that is same, set for child directory, dont set resolution for this directory
1453+
(!isExternalModuleNameRelative(name) &&
1454+
!!perDirectoryAndNonRelativeNameCache?.nonRelativeName.getFromNonRelativeNameCacheWithPath(name, mode, dirPath, redirectedReference)),
1455+
// when adding non relative mode resolution, delete everything that is not needed
1456+
(ancestorPath, mapOfRedirects, name, mode) => mapOfRedirects?.get(ancestorPath)?.delete(name, mode),
1457+
);
14771458
}
14781459

14791460
/** @internal */

src/compiler/moduleNameResolver.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ import {
7777
Pattern,
7878
patternText,
7979
perfLogger,
80+
Program,
8081
Push,
8182
readJson,
8283
removeExtension,
@@ -1184,6 +1185,49 @@ export function createPerDirectoryAndNonRelativeNameCache<T>(currentDirectory: s
11841185
}
11851186
}
11861187

1188+
/** @internal */
1189+
export function toPerDirectoryAndNonRelativeNameCache<T, U, V>(
1190+
program: Program,
1191+
perDirectoryAndNonRelativeNameCache: PerDirectoryAndNonRelativeNameCache<T> | undefined,
1192+
getResolvedFileName: (resolved: T) => string | undefined,
1193+
cache: ModeAwareCache<T> | undefined,
1194+
fOrPath: SourceFile | Path,
1195+
withRedirects: (redirectedReference: ResolvedProjectReference | undefined) => U,
1196+
forEachResolution: (r: T, name: string, mode: ResolutionMode, dirPath: Path, redirectedReference: ResolvedProjectReference | undefined, redirectsResult: U) => V | undefined,
1197+
ancestoryWorker: (ancestorPath: Path, mapOfRedirects: Map<Path, ModeAwareCache<T>> | undefined, name: string, mode: ResolutionMode) => void,
1198+
) {
1199+
if (!cache?.size()) return perDirectoryAndNonRelativeNameCache;
1200+
let dirPath: Path, redirectedReference: ResolvedProjectReference | undefined;
1201+
if (!isString(fOrPath)) {
1202+
redirectedReference = program.getRedirectReferenceForResolution(fOrPath);
1203+
dirPath = getDirectoryPath(fOrPath.path);
1204+
}
1205+
else {
1206+
dirPath = getDirectoryPath(fOrPath);
1207+
}
1208+
const redirectsResult = withRedirects(redirectedReference);
1209+
const mapForRedirects = perDirectoryAndNonRelativeNameCache?.perDirectory.perDirectoryMap.getMapOfCacheRedirects(redirectedReference);
1210+
let dirCache = mapForRedirects?.get(dirPath);
1211+
cache.forEach((resolution, name, mode) => {
1212+
if (forEachResolution(resolution, name, mode, dirPath, redirectedReference, redirectsResult)) return;
1213+
if (dirCache?.has(name, mode)) return;
1214+
(dirCache ??= (perDirectoryAndNonRelativeNameCache ??= createPerDirectoryAndNonRelativeNameCache(
1215+
program.getCurrentDirectory(),
1216+
program.getCanonicalFileName,
1217+
program.getCompilerOptions(),
1218+
getResolvedFileName,
1219+
)).perDirectory.getOrCreateCacheForDirectoryWithPath(dirPath, redirectedReference)).set(name, mode, resolution);
1220+
if (!isExternalModuleNameRelative(name)) {
1221+
perDirectoryAndNonRelativeNameCache!.nonRelativeName.getOrCreateCacheForNonRelativeName(name, mode, redirectedReference).setWithPath(
1222+
dirPath,
1223+
resolution,
1224+
ancestoryWorker === noop ? noop : ancestorPath => ancestoryWorker(ancestorPath, mapForRedirects, name, mode),
1225+
);
1226+
}
1227+
});
1228+
5592 return perDirectoryAndNonRelativeNameCache;
1229+
}
1230+
11871231
interface ModuleOrTypeReferenceResolutionCache<T> extends PerDirectoryResolutionCache<T>, NonRelativeNameResolutionCache<T>, PackageJsonInfoCache {
11881232
getPackageJsonInfoCache(): PackageJsonInfoCache;
11891233
clearAllExceptPackageJsonInfoCache(): void;

0 commit comments

Comments
 (0)
0