Skip to content

Commit 11fefb1

Browse files
committed
Remove refCount from resolutions
1 parent 3bf578a commit 11fefb1

File tree

2 files changed

+20
-37
lines changed

2 files changed

+20
-37
lines changed

src/compiler/resolutionCache.ts

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ export interface ResolutionWithFailedLookupLocations {
172172
failedLookupLocations?: string[];
173173
affectingLocations?: string[];
174174
isInvalidated?: boolean;
175-
refCount?: number;
176175
// Files that have this resolution using
177176
files?: Set<Path>;
178177
node10Result?: string;
@@ -1072,28 +1071,21 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
10721071
getResolutionWithResolvedFileName: GetResolutionWithResolvedFileName<T, R>,
10731072
deferWatchingNonRelativeResolution: boolean,
10741073
) {
1075-
if (resolution.refCount) {
1076-
resolution.refCount++;
1077-
Debug.assertIsDefined(resolution.files);
1074+
(resolution.files ??= new Set()).add(filePath);
1075+
if (resolution.files.size !== 1) return;
1076+
if (!deferWatchingNonRelativeResolution || isExternalModuleNameRelative(name)) {
1077+
watchFailedLookupLocationOfResolution(resolution);
10781078
}
10791079
else {
1080-
resolution.refCount = 1;
1081-
Debug.assert(!resolution.files?.size); // This resolution shouldnt be referenced by any file yet
1082-
if (!deferWatchingNonRelativeResolution || isExternalModuleNameRelative(name)) {
1083-
watchFailedLookupLocationOfResolution(resolution);
1084-
}
1085-
else {
1086-
nonRelativeExternalModuleResolutions.add(resolution);
1087-
}
1088-
const resolved = getResolutionWithResolvedFileName(resolution);
1089-
if (resolved && resolved.resolvedFileName) {
1090-
const key = resolutionHost.toPath(resolved.resolvedFileName);
1091-
let resolutions = resolvedFileToResolution.get(key);
1092-
if (!resolutions) resolvedFileToResolution.set(key, resolutions = new Set());
1093-
resolutions.add(resolution);
1094-
}
1080+
nonRelativeExternalModuleResolutions.add(resolution);
1081+
}
1082+
const resolved = getResolutionWithResolvedFileName(resolution);
1083+
if (resolved && resolved.resolvedFileName) {
1084+
const key = resolutionHost.toPath(resolved.resolvedFileName);
1085+
let resolutions = resolvedFileToResolution.get(key);
1086+
if (!resolutions) resolvedFileToResolution.set(key, resolutions = new Set());
1087+
resolutions.add(resolution);
10951088
}
1096-
(resolution.files ??= new Set()).add(filePath);
10971089
}
10981090

10991091
function watchFailedLookupLocation(failedLookupLocation: string, setAtRoot: boolean) {
@@ -1120,7 +1112,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
11201112
}
11211113

11221114
function watchFailedLookupLocationOfResolution(resolution: ResolutionWithFailedLookupLocations) {
1123-
Debug.assert(!!resolution.refCount);
1115+
Debug.assert(!!resolution.files?.size);
11241116

11251117
const { failedLookupLocations, affectingLocations, node10Result } = resolution;
11261118
if (!failedLookupLocations?.length && !affectingLocations?.length && !node10Result) return;
@@ -1141,7 +1133,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
11411133
}
11421134

11431135
function watchAffectingLocationsOfResolution(resolution: ResolutionWithFailedLookupLocations, addToResolutionsWithOnlyAffectingLocations: boolean) {
1144-
Debug.assert(!!resolution.refCount);
1136+
Debug.assert(!!resolution.files?.size);
11451137
const { affectingLocations } = resolution;
11461138
if (!affectingLocations?.length) return;
11471139
if (addToResolutionsWithOnlyAffectingLocations) resolutionsWithOnlyAffectingLocations.add(resolution);
@@ -1261,10 +1253,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
12611253
syncDirWatcherRemove?: boolean,
12621254
) {
12631255
Debug.checkDefined(resolution.files).delete(filePath);
1264-
resolution.refCount!--;
1265-
if (resolution.refCount) {
1266-
return;
1267-
}
1256+
if (resolution.files!.size) return;
1257+
resolution.files = undefined;
12681258
const resolved = getResolutionWithResolvedFileName(resolution);
12691259
if (resolved && resolved.resolvedFileName) {
12701260
const key = resolutionHost.toPath(resolved.resolvedFileName);

src/harness/incrementalUtils.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -248,19 +248,15 @@ export function verifyResolutionCache(
248248
// Verify ref count
249249
resolutionToRefs.forEach((info, resolution) => {
250250
ts.Debug.assert(
251-
resolution.refCount === info.length,
252-
`${projectName}:: Expected Resolution ref count ${info.length} but got ${resolution.refCount}`,
251+
resolution.files?.size === info.length,
252+
`${projectName}:: Expected Resolution ref count ${info.length} but got ${resolution.files?.size}`,
253253
() => `Expected from:: ${JSON.stringify(info, undefined, " ")}` +
254-
`Actual from: ${resolution.refCount}`
254+
`Actual from: ${resolution.files?.size} used from ${resolution.files ? JSON.stringify(ts.arrayFrom(resolution.files), undefined, " "): "undefined"}`
255255
);
256256
ts.Debug.assert(
257257
!resolution.isInvalidated,
258258
`${projectName}:: Resolution should not be invalidated`
259259
);
260-
ts.Debug.assert(
261-
resolutionToExpected.get(resolution)!.refCount === resolution.refCount,
262-
`${projectName}:: Expected Resolution ref count ${resolutionToExpected.get(resolution)!.refCount} but got ${resolution.refCount}`
263-
);
264260
verifySet(resolutionToExpected.get(resolution)!.files, resolution.files, `Resolution files`);
265261
});
266262
verifyMapOfResolutionSet(expected.resolvedFileToResolution, actual.resolvedFileToResolution, `resolvedFileToResolution`);
@@ -274,10 +270,7 @@ export function verifyResolutionCache(
274270
actual.resolvedTypeReferenceDirectives.forEach((_resolutions, path) => expected.removeResolutionsOfFile(path));
275271
expected.finishCachingPerDirectoryResolution(/*newProgram*/ undefined, actualProgram);
276272

277-
resolutionToExpected.forEach(expected => {
278-
ts.Debug.assert(!expected.refCount, `${projectName}:: All the resolution should be released`);
279-
ts.Debug.assert(!expected.files?.size, `${projectName}:: Shouldnt ref to any files`);
280-
});
273+
resolutionToExpected.forEach(expected => ts.Debug.assert(!expected.files?.size, `${projectName}:: Shouldnt ref to any files`));
281274
ts.Debug.assert(expected.resolvedFileToResolution.size === 0, `${projectName}:: resolvedFileToResolution should be released`);
282275
ts.Debug.assert(expected.resolutionsWithFailedLookups.size === 0, `${projectName}:: resolutionsWithFailedLookups should be released`);
283276
ts.Debug.assert(expected.resolutionsWithOnlyAffectingLocations.size === 0, `${projectName}:: resolutionsWithOnlyAffectingLocations should be released`);

0 commit comments

Comments
 (0)