From 3234d102ed4abbf39c44118ca9c4845553dbb99a Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Tue, 17 Oct 2023 15:08:34 -0700 Subject: [PATCH] Instead of iterating over resolutions to invalidate per global cache pass, invalidate them at usage site --- src/compiler/resolutionCache.ts | 36 +++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/src/compiler/resolutionCache.ts b/src/compiler/resolutionCache.ts index b3b4e3da7e5..504355c2069 100644 --- a/src/compiler/resolutionCache.ts +++ b/src/compiler/resolutionCache.ts @@ -764,6 +764,9 @@ export function createResolutionCache( hasInvalidatedResolutions: path => customHasInvalidatedResolutions(path) || allModuleAndTypeResolutionsAreInvalidated || + resolutionsWithGlobalCachePassAreInvalidated || + resolutionsWithoutGlobalCachePassAreInvalidated || + unresolvedResolutionsWithGlobalCachePassAreInvalidated || !!collected?.has(path), hasInvalidatedLibResolutions: libFileName => customHasInvalidatedLibResolutions(libFileName) || @@ -800,6 +803,9 @@ export function createResolutionCache( function finishCachingPerDirectoryResolution(newProgram: Program | undefined, oldProgram: Program | undefined) { allModuleAndTypeResolutionsAreInvalidated = false; + resolutionsWithGlobalCachePassAreInvalidated = false; + resolutionsWithoutGlobalCachePassAreInvalidated = false; + unresolvedResolutionsWithGlobalCachePassAreInvalidated = false; watchFailedLookupLocationOfNonRelativeModuleResolutions(); // Update file watches if (newProgram !== oldProgram) { @@ -857,6 +863,13 @@ export function createResolutionCache( } } + function isResolutionInvalidatedPerGlobalCacheOptions(resolution: ResolutionWithFailedLookupLocations) { + if (resolutionsWithGlobalCachePassAreInvalidated && resolution.globalCacheResolution) return true; + if (resolutionsWithoutGlobalCachePassAreInvalidated && resolution.globalCacheResolution === false) return true; + if (unresolvedResolutionsWithGlobalCachePassAreInvalidated && resolution.globalCacheResolution && isUnresolvedOrResolvedToJs(resolution as ResolvedModuleWithFailedLookupLocations)) return true; + return false; + } + interface ResolveNamesWithLocalCacheInput { entries: readonly Entry[]; containingFile: string; @@ -902,7 +915,13 @@ export function createResolutionCache( // Resolution is valid if it is present and not invalidated if ( !seenNamesInFile.has(name, mode) && - (allModuleAndTypeResolutionsAreInvalidated || unmatchedRedirects || !resolution || resolution.isInvalidated) + ( + allModuleAndTypeResolutionsAreInvalidated || + unmatchedRedirects || + !resolution || + resolution.isInvalidated || + isResolutionInvalidatedPerGlobalCacheOptions(resolution) + ) ) { const existingResolution = resolution; resolution = loader.resolve(name, mode); @@ -1591,9 +1610,6 @@ export function createResolutionCache( startsWithPathChecks = undefined; isInDirectoryChecks = undefined; affectingPathChecks = undefined; - resolutionsWithGlobalCachePassAreInvalidated = false; - resolutionsWithoutGlobalCachePassAreInvalidated = false; - unresolvedResolutionsWithGlobalCachePassAreInvalidated = false; return true; } let invalidated = false; @@ -1607,11 +1623,7 @@ export function createResolutionCache( affectingPathChecksForFile = undefined; } - if ( - !failedLookupChecks && !startsWithPathChecks && !isInDirectoryChecks && !affectingPathChecks && - !resolutionsWithGlobalCachePassAreInvalidated && !resolutionsWithoutGlobalCachePassAreInvalidated && - !unresolvedResolutionsWithGlobalCachePassAreInvalidated - ) { + if (!failedLookupChecks && !startsWithPathChecks && !isInDirectoryChecks && !affectingPathChecks) { return invalidated; } @@ -1622,9 +1634,6 @@ export function createResolutionCache( isInDirectoryChecks = undefined; invalidated = invalidateResolutions(resolutionsWithOnlyAffectingLocations, canInvalidatedFailedLookupResolutionWithAffectingLocation) || invalidated; affectingPathChecks = undefined; - resolutionsWithGlobalCachePassAreInvalidated = false; - resolutionsWithoutGlobalCachePassAreInvalidated = false; - unresolvedResolutionsWithGlobalCachePassAreInvalidated = false; return invalidated; } @@ -1644,9 +1653,6 @@ export function createResolutionCache( } function canInvalidatedFailedLookupResolutionWithAffectingLocation(resolution: ResolutionWithFailedLookupLocations) { - if (resolutionsWithGlobalCachePassAreInvalidated && resolution.globalCacheResolution) return true; - if (resolutionsWithoutGlobalCachePassAreInvalidated && resolution.globalCacheResolution === false) return true; - if (unresolvedResolutionsWithGlobalCachePassAreInvalidated && resolution.globalCacheResolution && isUnresolvedOrResolvedToJs(resolution as ResolvedModuleWithFailedLookupLocations)) return true; return !!affectingPathChecks && resolution.affectingLocations?.some(location => affectingPathChecks!.has(location)); }