Skip to content

Commit e98d6ec

Browse files
committed
Remove refCount from resolutions as we dont need it explicitly since its tracked by files it references
1 parent 9f09d09 commit e98d6ec

File tree

2 files changed

+26
-39
lines changed

2 files changed

+26
-39
lines changed

src/compiler/resolutionCache.ts

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ export interface ResolutionWithFailedLookupLocations {
181181
failedLookupLocations?: string[];
182182
affectingLocations?: string[];
183183
isInvalidated?: boolean;
184-
refCount?: number;
185184
// Files that have this resolution using
186185
files?: Set<Path>;
187186
node10Result?: string;
@@ -1124,30 +1123,25 @@ export function createResolutionCache(
11241123
getResolutionWithResolvedFileName: GetResolutionWithResolvedFileName<T, R>,
11251124
deferWatchingNonRelativeResolution: boolean,
11261125
) {
1127-
if (resolution.refCount) {
1128-
resolution.refCount++;
1129-
Debug.assertIsDefined(resolution.files);
1126+
(resolution.files ??= new Set()).add(filePath);
1127+
if (resolution.files.size !== 1) return;
1128+
1129+
if (resolution.globalCacheResolution) resolutionsResolvedWithGlobalCache++;
1130+
else if (resolution.globalCacheResolution === false) resolutionsResolvedWithoutGlobalCache++;
1131+
1132+
if (!deferWatchingNonRelativeResolution || isExternalModuleNameRelative(name)) {
1133+
watchFailedLookupLocationOfResolution(resolution);
11301134
}
11311135
else {
1132-
resolution.refCount = 1;
1133-
if (resolution.globalCacheResolution) resolutionsResolvedWithGlobalCache++;
1134-
else if (resolution.globalCacheResolution === false) resolutionsResolvedWithoutGlobalCache++;
1135-
Debug.assert(!resolution.files?.size); // This resolution shouldnt be referenced by any file yet
1136-
if (!deferWatchingNonRelativeResolution || isExternalModuleNameRelative(name)) {
1137-
watchFailedLookupLocationOfResolution(resolution);
1138-
}
1139-
else {
1140-
nonRelativeExternalModuleResolutions.add(resolution);
1141-
}
1142-
const resolved = getResolutionWithResolvedFileName(resolution);
1143-
if (resolved && resolved.resolvedFileName) {
1144-
const key = resolutionHost.toPath(resolved.resolvedFileName);
1145-
let resolutions = resolvedFileToResolution.get(key);
1146-
if (!resolutions) resolvedFileToResolution.set(key, resolutions = new Set());
1147-
resolutions.add(resolution);
1148-
}
1136+
nonRelativeExternalModuleResolutions.add(resolution);
1137+
}
1138+
const resolved = getResolutionWithResolvedFileName(resolution);
1139+
if (resolved && resolved.resolvedFileName) {
1140+
const key = resolutionHost.toPath(resolved.resolvedFileName);
1141+
let resolutions = resolvedFileToResolution.get(key);
1142+
if (!resolutions) resolvedFileToResolution.set(key, resolutions = new Set());
1143+
resolutions.add(resolution);
11491144
}
1150-
(resolution.files ??= new Set()).add(filePath);
11511145
}
11521146

11531147
function watchFailedLookupLocation(failedLookupLocation: string, setAtRoot: boolean) {
@@ -1174,7 +1168,7 @@ export function createResolutionCache(
11741168
}
11751169

11761170
function watchFailedLookupLocationOfResolution(resolution: ResolutionWithFailedLookupLocations) {
1177-
Debug.assert(!!resolution.refCount);
1171+
Debug.assert(!!resolution.files?.size);
11781172

11791173
const { failedLookupLocations, affectingLocations, node10Result } = resolution;
11801174
if (!failedLookupLocations?.length && !affectingLocations?.length && !node10Result) return;
@@ -1195,7 +1189,7 @@ export function createResolutionCache(
11951189
}
11961190

11971191
function watchAffectingLocationsOfResolution(resolution: ResolutionWithFailedLookupLocations, addToResolutionsWithOnlyAffectingLocations: boolean) {
1198-
Debug.assert(!!resolution.refCount);
1192+
Debug.assert(!!resolution.files?.size);
11991193
const { affectingLocations } = resolution;
12001194
if (!affectingLocations?.length) return;
12011195
if (addToResolutionsWithOnlyAffectingLocations) resolutionsWithOnlyAffectingLocations.add(resolution);
@@ -1315,12 +1309,12 @@ export function createResolutionCache(
13151309
syncDirWatcherRemove?: boolean,
13161310
) {
13171311
Debug.checkDefined(resolution.files).delete(filePath);
1318-
resolution.refCount!--;
1319-
if (resolution.refCount) {
1320-
return;
1321-
}
1312+
if (resolution.files!.size) return;
1313+
resolution.files = undefined;
1314+
13221315
if (resolution.globalCacheResolution) resolutionsResolvedWithGlobalCache--;
13231316
if (resolution.globalCacheResolution === false) resolutionsResolvedWithoutGlobalCache--;
1317+
13241318
const resolved = getResolutionWithResolvedFileName(resolution);
13251319
if (resolved && resolved.resolvedFileName) {
13261320
const key = resolutionHost.toPath(resolved.resolvedFileName);

src/harness/incrementalUtils.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -294,20 +294,16 @@ export function verifyResolutionCache(
294294
// Verify ref count
295295
resolutionToRefs.forEach((info, resolution) => {
296296
ts.Debug.assert(
297-
resolution.refCount === info.length,
298-
`${projectName}:: Expected Resolution ref count ${info.length} but got ${resolution.refCount}`,
297+
resolution.files?.size === info.length,
298+
`${projectName}:: Expected Resolution ref count ${info.length} but got ${resolution.files?.size}`,
299299
() =>
300300
`Expected from:: ${JSON.stringify(info, undefined, " ")}` +
301-
`Actual from: ${resolution.refCount}`,
301+
`Actual from: ${resolution.files?.size} used from ${resolution.files ? JSON.stringify(ts.arrayFrom(resolution.files), undefined, " ") : "undefined"}`,
302302
);
303303
ts.Debug.assert(
304304
!resolution.isInvalidated,
305305
`${projectName}:: Resolution should not be invalidated`,
306306
);
307-
ts.Debug.assert(
308-
resolutionToExpected.get(resolution)!.refCount === resolution.refCount,
309-
`${projectName}:: Expected Resolution ref count ${resolutionToExpected.get(resolution)!.refCount} but got ${resolution.refCount}`,
310-
);
311307
verifySet(resolutionToExpected.get(resolution)!.files, resolution.files, `${projectName}:: Resolution files`);
312308
});
313309
verifyMapOfResolutionSet(expected.resolvedFileToResolution, actual.resolvedFileToResolution, `resolvedFileToResolution`);
@@ -330,10 +326,7 @@ export function verifyResolutionCache(
330326
actual.resolvedTypeReferenceDirectives.forEach((_resolutions, path) => expected.removeResolutionsOfFile(path));
331327
expected.finishCachingPerDirectoryResolution(/*newProgram*/ undefined, actualProgram);
332328

333-
resolutionToExpected.forEach(expected => {
334-
ts.Debug.assert(!expected.refCount, `${projectName}:: All the resolution should be released`);
335-
ts.Debug.assert(!expected.files?.size, `${projectName}:: Shouldnt ref to any files`);
336-
});
329+
resolutionToExpected.forEach(expected => ts.Debug.assert(!expected.files?.size, `${projectName}:: Shouldnt ref to any files`));
337330
ts.Debug.assert(expected.resolvedFileToResolution.size === 0, `${projectName}:: resolvedFileToResolution should be released`);
338331
ts.Debug.assert(expected.resolutionsWithFailedLookups.size === 0, `${projectName}:: resolutionsWithFailedLookups should be released`);
339332
ts.Debug.assert(expected.resolutionsWithOnlyAffectingLocations.size === 0, `${projectName}:: resolutionsWithOnlyAffectingLocations should be released`);

0 commit comments

Comments
 (0)