Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[WIP] Server shared resolutions #55067

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Resolution reuse handling
  • Loading branch information
sheetalkamat committed Jul 31, 2023
commit de2f76a7c4a43caa3a564d2b0fc762603ceed367
150 changes: 79 additions & 71 deletions 150 src/compiler/program.ts

Large diffs are not rendered by default.

133 changes: 116 additions & 17 deletions 133 src/compiler/resolutionCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
clearMap,
closeFileWatcher,
closeFileWatcherOf,
CompilerHostSupportingResolutionCache,
CompilerOptions,
createModeAwareCache,
createModuleResolutionCache,
Expand All @@ -22,6 +23,7 @@ import {
FileWatcher,
FileWatcherCallback,
firstDefinedIterator,
getAutomaticTypeDirectiveContainingFile,
GetCanonicalFileName,
getDirectoryPath,
getEffectiveTypeRoots,
Expand Down Expand Up @@ -63,6 +65,7 @@ import {
resolutionExtensionIsTSOrJson,
ResolutionLoader,
ResolutionMode,
ResolutionNameAndModeGetter,
ResolvedModuleWithFailedLookupLocations,
ResolvedProjectReference,
ResolvedTypeReferenceDirectiveWithFailedLookupLocations,
Expand All @@ -75,6 +78,7 @@ import {
stringContains,
StringLiteralLike,
trace,
typeReferenceResolutionNameAndModeGetter,
updateResolutionField,
WatchDirectoryFlags,
} from "./_namespaces/ts";
Expand All @@ -89,7 +93,7 @@ export interface HasInvalidatedFromResolutionCache {
*
* @internal
*/
export interface ResolutionCache {
export interface ResolutionCache extends Required<CompilerHostSupportingResolutionCache> {
rootDirForResolution: string;
resolvedModuleNames: Map<Path, ModeAwareCache<CachedResolvedModuleWithFailedLookupLocations>>;
resolvedTypeReferenceDirectives: Map<Path, ModeAwareCache<CachedResolvedTypeReferenceDirectiveWithFailedLookupLocations>>;
Expand Down Expand Up @@ -117,6 +121,7 @@ export interface ResolutionCache {
options: CompilerOptions,
containingSourceFile: SourceFile,
reusedNames: readonly StringLiteralLike[] | undefined,
ambientModuleNames: readonly StringLiteralLike[] | undefined,
): readonly ResolvedModuleWithFailedLookupLocations[];
resolveTypeReferenceDirectiveReferences<T extends FileReference | string>(
typeDirectiveReferences: readonly T[],
Expand Down Expand Up @@ -574,6 +579,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
resolveSingleModuleNameWithoutWatching,
removeResolutionsFromProjectReferenceRedirects,
removeResolutionsOfFile,
reusedModuleResolutions,
reusedTypeReferenceDirectiveResolutions,
hasChangedAutomaticTypeDirectiveNames: () => hasChangedAutomaticTypeDirectiveNames,
invalidateResolutionOfFile,
invalidateResolutionsOfFailedLookupLocations,
Expand Down Expand Up @@ -741,7 +748,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
containingSourceFile: SourceFile;
redirectedReference: ResolvedProjectReference | undefined;
options: CompilerOptions;
reusedNames?: readonly Entry[];
reusedNames: readonly Entry[] | undefined;
ambientModuleNames?: readonly Entry[] | undefined,
perFileCache: Map<Path, ModeAwareCache<T>>;
loader: ResolutionLoader<Entry, T, SourceFile>;
getResolutionWithResolvedFileName: GetResolutionWithResolvedFileName<T, R>;
Expand All @@ -751,7 +759,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
}
function resolveNamesWithLocalCache<Entry, SourceFile, T extends ResolutionWithFailedLookupLocations, R extends ResolutionWithResolvedFileName>({
entries, containingFile, containingSourceFile, redirectedReference, options,
perFileCache, reusedNames,
perFileCache, reusedNames, ambientModuleNames,
loader, getResolutionWithResolvedFileName, deferWatchingNonRelativeResolution,
shouldRetryResolution, logChanges,
}: ResolveNamesWithLocalCacheInput<Entry, SourceFile, T, R>): readonly T[] {
Expand Down Expand Up @@ -824,20 +832,16 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
seenNamesInFile.set(name, mode, true);
resolvedModules.push(resolution);
}
reusedNames?.forEach(entry => seenNamesInFile.set(
loader.nameAndMode.getName(entry),
loader.nameAndMode.getMode(entry, containingSourceFile),
true,
));
if (resolutionsInFile.size() !== seenNamesInFile.size()) {
// Stop watching and remove the unused name
resolutionsInFile.forEach((resolution, name, mode) => {
if (!seenNamesInFile.has(name, mode)) {
stopWatchFailedLookupLocationOfResolution(resolution, path, getResolutionWithResolvedFileName);
resolutionsInFile.delete(name, mode);
}
});
}
reuseResolutionsWorker(
reusedNames,
containingSourceFile,
path,
resolutionsInFile,
seenNamesInFile,
loader.nameAndMode,
getResolutionWithResolvedFileName,
ambientModuleNames,
);
return resolvedModules;

function resolutionIsEqualTo(oldResolution: T | undefined, newResolution: T | undefined): boolean {
Expand Down Expand Up @@ -895,6 +899,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
options: CompilerOptions,
containingSourceFile: SourceFile,
reusedNames: readonly StringLiteralLike[] | undefined,
ambientModuleNames: readonly StringLiteralLike[] | undefined,
): readonly ResolvedModuleWithFailedLookupLocations[] {
return resolveNamesWithLocalCache({
entries: moduleLiterals,
Expand All @@ -903,6 +908,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
redirectedReference,
options,
reusedNames,
ambientModuleNames,
perFileCache: resolvedModuleNames,
loader: createModuleResolutionLoaderUsingGlobalCache(
containingFile,
Expand Down Expand Up @@ -956,6 +962,99 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
return resolution;
}

function reuseResolutionsWorker<Entry, SourceFile, T extends ResolutionWithFailedLookupLocations, R extends ResolutionWithResolvedFileName>(
reusedNames: readonly Entry[] | undefined,
containingSourceFile: SourceFile,
path: Path,
resolutionsInFile: ModeAwareCache<T>,
seenNamesInFile: ModeAwareCache<true>,
nameAndModeGetter: ResolutionNameAndModeGetter<Entry, SourceFile>,
getResolutionWithResolvedFileName: GetResolutionWithResolvedFileName<T, R>,
ambientModuleNames: readonly Entry[] | undefined,
) {
reusedNames?.forEach(entry => seenNamesInFile.set(
nameAndModeGetter.getName(entry),
nameAndModeGetter.getMode(entry, containingSourceFile),
true,
));
// For ambient module names, if its not invalidated keep it
ambientModuleNames?.forEach(entry => {
const name = nameAndModeGetter.getName(entry);
const mode = nameAndModeGetter.getMode(entry, containingSourceFile);
if (!seenNamesInFile.has(name, mode)) {
const resolution = resolutionsInFile.get(name, mode);
// Keep this resolution from old time for ambient module names
if (resolution && !resolution.isInvalidated) {
seenNamesInFile.set(name, mode, true);
}
}
});
if (resolutionsInFile.size() !== seenNamesInFile.size()) {
// Stop watching and remove the unused name
resolutionsInFile.forEach((resolution, name, mode) => {
if (!seenNamesInFile.has(name, mode)) {
stopWatchFailedLookupLocationOfResolution(resolution, path, getResolutionWithResolvedFileName);
resolutionsInFile.delete(name, mode);
}
});
}
}

function reuseResolutions<Entry, SourceFile, T extends ResolutionWithFailedLookupLocations, R extends ResolutionWithResolvedFileName>(
reusedNames: readonly Entry[] | undefined,
containingSourceFile: SourceFile,
path: Path,
perFileCache: Map<Path, ModeAwareCache<T>>,
nameAndModeGetter: ResolutionNameAndModeGetter<Entry, SourceFile>,
getResolutionWithResolvedFileName: GetResolutionWithResolvedFileName<T, R>,
ambientModuleNames?: readonly Entry[] | undefined,
) {
const resolutionsInFile = perFileCache.get(path);
if (!resolutionsInFile) return;
const seenNamesInFile = createModeAwareCache<true>();
reuseResolutionsWorker(
reusedNames,
containingSourceFile,
path, resolutionsInFile,
seenNamesInFile,
nameAndModeGetter,
getResolutionWithResolvedFileName,
ambientModuleNames,
);
}

function reusedModuleResolutions(
reusedNames: readonly StringLiteralLike[] | undefined,
containingSourceFile: SourceFile,
ambientModuleNames: readonly StringLiteralLike[] | undefined,
) {
reuseResolutions(
reusedNames,
containingSourceFile,
containingSourceFile.path,
resolvedModuleNames,
moduleResolutionNameAndModeGetter,
getResolvedModule,
ambientModuleNames,
);
}

function reusedTypeReferenceDirectiveResolutions<T extends FileReference | string>(
reusedNames: readonly T[] | undefined,
containingSourceFile: SourceFile | undefined,
) {
reuseResolutions(
reusedNames,
containingSourceFile,
containingSourceFile ?
containingSourceFile.path :
resolutionHost.toPath(getAutomaticTypeDirectiveContainingFile(resolutionHost.getCompilationSettings(), getCurrentDirectory())),
resolvedTypeReferenceDirectives,
typeReferenceResolutionNameAndModeGetter,
getResolvedTypeReferenceDirective,
);
}

function resolveSingleModuleNameWithoutWatching(moduleName: string, containingFile: string) {
const path = resolutionHost.toPath(containingFile);
const resolutionsInFile = resolvedModuleNames.get(path);
Expand Down
17 changes: 17 additions & 0 deletions 17 src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7800,6 +7800,7 @@ export interface CompilerHost extends ModuleResolutionHost {
options: CompilerOptions,
containingSourceFile: SourceFile,
reusedNames: readonly StringLiteralLike[] | undefined,
ambientModuleNames: readonly StringLiteralLike[] | undefined,
): readonly ResolvedModuleWithFailedLookupLocations[];
resolveTypeReferenceDirectiveReferences?<T extends FileReference | string>(
typeDirectiveReferences: readonly T[],
Expand Down Expand Up @@ -7840,6 +7841,22 @@ export interface CompilerHost extends ModuleResolutionHost {
/** @internal */ getBuildInfo?(fileName: string, configFilePath: string | undefined): BuildInfo | undefined;
}

/** @internal */
export interface CompilerHostSupportingResolutionCache {
reusedModuleResolutions?(
reusedNames: readonly StringLiteralLike[] | undefined,
containingSourceFile: SourceFile,
ambientModuleNames: readonly StringLiteralLike[] | undefined,
): void;
reusedTypeReferenceDirectiveResolutions?<T extends FileReference | string>(
reusedNames: readonly T[] | undefined,
containingSourceFile: SourceFile | undefined,
): void;
}
/** @internal */
export interface CompilerHost extends CompilerHostSupportingResolutionCache {
}

/** true if --out otherwise source file name *
* @internal
*/
Expand Down
5 changes: 4 additions & 1 deletion 5 src/compiler/watchPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ export interface ProgramHost<T extends BuilderProgram> {
options: CompilerOptions,
containingSourceFile: SourceFile,
reusedNames: readonly StringLiteralLike[] | undefined,
ambientModuleNames: readonly StringLiteralLike[] | undefined,
): readonly ResolvedModuleWithFailedLookupLocations[];
resolveTypeReferenceDirectiveReferences?<T extends FileReference | string>(
typeDirectiveReferences: readonly T[],
Expand Down Expand Up @@ -514,11 +515,13 @@ export function createWatchProgram<T extends BuilderProgram>(host: WatchCompiler
compilerHost.resolveModuleNames = maybeBind(host, host.resolveModuleNames);
if (!compilerHost.resolveModuleNameLiterals && !compilerHost.resolveModuleNames) {
compilerHost.resolveModuleNameLiterals = resolutionCache.resolveModuleNameLiterals.bind(resolutionCache);
compilerHost.reusedModuleResolutions = resolutionCache.reusedModuleResolutions.bind(resolutionCache);
}
compilerHost.resolveTypeReferenceDirectiveReferences = maybeBind(host, host.resolveTypeReferenceDirectiveReferences);
compilerHost.resolveTypeReferenceDirectives = maybeBind(host, host.resolveTypeReferenceDirectives);
if (!compilerHost.resolveTypeReferenceDirectiveReferences && !compilerHost.resolveTypeReferenceDirectives) {
compilerHost.resolveTypeReferenceDirectiveReferences = resolutionCache.resolveTypeReferenceDirectiveReferences.bind(resolutionCache);
compilerHost.resolveTypeReferenceDirectiveReferences = resolutionCache.resolveTypeReferenceDirectiveReferences.bind(resolutionCache);
compilerHost.reusedTypeReferenceDirectiveResolutions = resolutionCache.reusedTypeReferenceDirectiveResolutions.bind(resolutionCache);
}
compilerHost.resolveLibrary = !host.resolveLibrary ?
resolutionCache.resolveLibrary.bind(resolutionCache) :
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.