Skip to content

Commit a6bc4ec

Browse files
authored
Remove refCount from resolutions as we dont need it explicitly since its tracked by files it references (#59041)
1 parent 7c011e7 commit a6bc4ec

File tree

2 files changed

+22
-37
lines changed

2 files changed

+22
-37
lines changed

src/compiler/resolutionCache.ts

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ export interface ResolutionWithFailedLookupLocations {
169169
failedLookupLocations?: string[];
170170
affectingLocations?: string[];
171171
isInvalidated?: boolean;
172-
refCount?: number;
173172
// Files that have this resolution using
174173
files?: Set<Path>;
175174
alternateResult?: string;
@@ -1095,28 +1094,21 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
10951094
getResolutionWithResolvedFileName: GetResolutionWithResolvedFileName<T, R>,
10961095
deferWatchingNonRelativeResolution: boolean,
10971096
) {
1098-
if (resolution.refCount) {
1099-
resolution.refCount++;
1100-
Debug.assertIsDefined(resolution.files);
1097+
(resolution.files ??= new Set()).add(filePath);
1098+
if (resolution.files.size !== 1) return;
1099+
if (!deferWatchingNonRelativeResolution || isExternalModuleNameRelative(name)) {
1100+
watchFailedLookupLocationOfResolution(resolution);
11011101
}
11021102
else {
1103-
resolution.refCount = 1;
1104-
Debug.assert(!resolution.files?.size); // This resolution shouldnt be referenced by any file yet
1105-
if (!deferWatchingNonRelativeResolution || isExternalModuleNameRelative(name)) {
1106-
watchFailedLookupLocationOfResolution(resolution);
1107-
}
1108-
else {
1109-
nonRelativeExternalModuleResolutions.add(name, resolution);
1110-
}
1111-
const resolved = getResolutionWithResolvedFileName(resolution);
1112-
if (resolved && resolved.resolvedFileName) {
1113-
const key = resolutionHost.toPath(resolved.resolvedFileName);
1114-
let resolutions = resolvedFileToResolution.get(key);
1115-
if (!resolutions) resolvedFileToResolution.set(key, resolutions = new Set());
1116-
resolutions.add(resolution);
1117-
}
1103+
nonRelativeExternalModuleResolutions.add(name, resolution);
1104+
}
1105+
const resolved = getResolutionWithResolvedFileName(resolution);
1106+
if (resolved && resolved.resolvedFileName) {
1107+
const key = resolutionHost.toPath(resolved.resolvedFileName);
1108+
let resolutions = resolvedFileToResolution.get(key);
1109+
if (!resolutions) resolvedFileToResolution.set(key, resolutions = new Set());
1110+
resolutions.add(resolution);
11181111
}
1119-
(resolution.files ??= new Set()).add(filePath);
11201112
}
11211113

11221114
function watchFailedLookupLocation(failedLookupLocation: string, setAtRoot: boolean) {
@@ -1144,7 +1136,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
11441136
}
11451137

11461138
function watchFailedLookupLocationOfResolution(resolution: ResolutionWithFailedLookupLocations) {
1147-
Debug.assert(!!resolution.refCount);
1139+
Debug.assert(!!resolution.files?.size);
11481140

11491141
const { failedLookupLocations, affectingLocations, alternateResult } = resolution;
11501142
if (!failedLookupLocations?.length && !affectingLocations?.length && !alternateResult) return;
@@ -1165,7 +1157,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
11651157
}
11661158

11671159
function watchAffectingLocationsOfResolution(resolution: ResolutionWithFailedLookupLocations, addToResolutionsWithOnlyAffectingLocations: boolean) {
1168-
Debug.assert(!!resolution.refCount);
1160+
Debug.assert(!!resolution.files?.size);
11691161
const { affectingLocations } = resolution;
11701162
if (!affectingLocations?.length) return;
11711163
if (addToResolutionsWithOnlyAffectingLocations) resolutionsWithOnlyAffectingLocations.add(resolution);
@@ -1381,10 +1373,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
13811373
syncDirWatcherRemove?: boolean,
13821374
) {
13831375
Debug.checkDefined(resolution.files).delete(filePath);
1384-
resolution.refCount!--;
1385-
if (resolution.refCount) {
1386-
return;
1387-
}
1376+
if (resolution.files!.size) return;
1377+
resolution.files = undefined;
13881378
const resolved = getResolutionWithResolvedFileName(resolution);
13891379
if (resolved && resolved.resolvedFileName) {
13901380
const key = resolutionHost.toPath(resolved.resolvedFileName);

src/harness/incrementalUtils.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -254,15 +254,11 @@ export function verifyResolutionCache(
254254
// Verify ref count
255255
resolutionToRefs.forEach((info, resolution) => {
256256
ts.Debug.assert(
257-
resolution.refCount === info.length,
258-
`${projectName}:: Expected Resolution ref count ${info.length} but got ${resolution.refCount}`,
257+
resolution.files?.size === info.length,
258+
`${projectName}:: Expected Resolution ref count ${info.length} but got ${resolution.files?.size}`,
259259
() =>
260260
`Expected from:: ${JSON.stringify(info, undefined, " ")}` +
261-
`Actual from: ${resolution.refCount}`,
262-
);
263-
ts.Debug.assert(
264-
resolutionToExpected.get(resolution)!.refCount === resolution.refCount,
265-
`${projectName}:: Expected Resolution ref count ${resolutionToExpected.get(resolution)!.refCount} but got ${resolution.refCount}`,
261+
`Actual from: ${resolution.files?.size}`,
266262
);
267263
verifySet(resolutionToExpected.get(resolution)!.files, resolution.files, `${projectName}:: Resolution files`);
268264
});
@@ -280,10 +276,9 @@ export function verifyResolutionCache(
280276
actual.resolvedTypeReferenceDirectives.forEach((_resolutions, path) => expected.removeResolutionsOfFile(path));
281277
expected.finishCachingPerDirectoryResolution(/*newProgram*/ undefined, actualProgram);
282278

283-
resolutionToExpected.forEach(expected => {
284-
ts.Debug.assert(!expected.refCount, `${projectName}:: All the resolution should be released`);
285-
ts.Debug.assert(!expected.files?.size, `${projectName}:: Shouldnt ref to any files`);
286-
});
279+
resolutionToExpected.forEach(
280+
expected => ts.Debug.assert(!expected.files?.size, `${projectName}:: Shouldnt ref to any files`),
281+
);
287282
ts.Debug.assert(expected.resolvedFileToResolution.size === 0, `${projectName}:: resolvedFileToResolution should be released`);
288283
ts.Debug.assert(expected.resolutionsWithFailedLookups.size === 0, `${projectName}:: resolutionsWithFailedLookups should be released`);
289284
ts.Debug.assert(expected.resolutionsWithOnlyAffectingLocations.size === 0, `${projectName}:: resolutionsWithOnlyAffectingLocations should be released`);

0 commit comments

Comments
 (0)