Skip to content

Commit b3678ec

Browse files
committed
Remove refCount from resolutions as we dont need it explicitly since its tracked by files it references
1 parent 75f3379 commit b3678ec

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
@@ -180,7 +180,6 @@ export interface ResolutionWithFailedLookupLocations {
180180
failedLookupLocations?: string[];
181181
affectingLocations?: string[];
182182
isInvalidated?: boolean;
183-
refCount?: number;
184183
// Files that have this resolution using
185184
files?: Set<Path>;
186185
node10Result?: string;
@@ -1123,30 +1122,25 @@ export function createResolutionCache(
11231122
getResolutionWithResolvedFileName: GetResolutionWithResolvedFileName<T, R>,
11241123
deferWatchingNonRelativeResolution: boolean,
11251124
) {
1126-
if (resolution.refCount) {
1127-
resolution.refCount++;
1128-
Debug.assertIsDefined(resolution.files);
1125+
(resolution.files ??= new Set()).add(filePath);
1126+
if (resolution.files.size !== 1) return;
1127+
1128+
if (resolution.globalCacheResolution) resolutionsResolvedWithGlobalCache++;
1129+
else if (resolution.globalCacheResolution === false) resolutionsResolvedWithoutGlobalCache++;
1130+
1131+
if (!deferWatchingNonRelativeResolution || isExternalModuleNameRelative(name)) {
1132+
watchFailedLookupLocationOfResolution(resolution);
11291133
}
11301134
else {
1131-
resolution.refCount = 1;
1132-
if (resolution.globalCacheResolution) resolutionsResolvedWithGlobalCache++;
1133-
else if (resolution.globalCacheResolution === false) resolutionsResolvedWithoutGlobalCache++;
1134-
Debug.assert(!resolution.files?.size); // This resolution shouldnt be referenced by any file yet
1135-
if (!deferWatchingNonRelativeResolution || isExternalModuleNameRelative(name)) {
1136-
watchFailedLookupLocationOfResolution(resolution);
1137-
}
1138-
else {
1139-
nonRelativeExternalModuleResolutions.add(resolution);
1140-
}
1141-
const resolved = getResolutionWithResolvedFileName(resolution);
1142-
if (resolved && resolved.resolvedFileName) {
1143-
const key = resolutionHost.toPath(resolved.resolvedFileName);
1144-
let resolutions = resolvedFileToResolution.get(key);
1145-
if (!resolutions) resolvedFileToResolution.set(key, resolutions = new Set());
1146-
resolutions.add(resolution);
1147-
}
1135+
nonRelativeExternalModuleResolutions.add(resolution);
1136+
}
1137+
const resolved = getResolutionWithResolvedFileName(resolution);
1138+
if (resolved && resolved.resolvedFileName) {
1139+
const key = resolutionHost.toPath(resolved.resolvedFileName);
1140+
let resolutions = resolvedFileToResolution.get(key);
1141+
if (!resolutions) resolvedFileToResolution.set(key, resolutions = new Set());
1142+
resolutions.add(resolution);
11481143
}
1149-
(resolution.files ??= new Set()).add(filePath);
11501144
}
11511145

11521146
function watchFailedLookupLocation(failedLookupLocation: string, setAtRoot: boolean) {
@@ -1173,7 +1167,7 @@ export function createResolutionCache(
11731167
}
11741168

11751169
function watchFailedLookupLocationOfResolution(resolution: ResolutionWithFailedLookupLocations) {
1176-
Debug.assert(!!resolution.refCount);
1170+
Debug.assert(!!resolution.files?.size);
11771171

11781172
const { failedLookupLocations, affectingLocations, node10Result } = resolution;
11791173
if (!failedLookupLocations?.length && !affectingLocations?.length && !node10Result) return;
@@ -1194,7 +1188,7 @@ export function createResolutionCache(
11941188
}
11951189

11961190
function watchAffectingLocationsOfResolution(resolution: ResolutionWithFailedLookupLocations, addToResolutionsWithOnlyAffectingLocations: boolean) {
1197-
Debug.assert(!!resolution.refCount);
1191+
Debug.assert(!!resolution.files?.size);
11981192
const { affectingLocations } = resolution;
11991193
if (!affectingLocations?.length) return;
12001194
if (addToResolutionsWithOnlyAffectingLocations) resolutionsWithOnlyAffectingLocations.add(resolution);
@@ -1314,12 +1308,12 @@ export function createResolutionCache(
13141308
syncDirWatcherRemove?: boolean,
13151309
) {
13161310
Debug.checkDefined(resolution.files).delete(filePath);
1317-
resolution.refCount!--;
1318-
if (resolution.refCount) {
1319-
return;
1320-
}
1311+
if (resolution.files!.size) return;
1312+
resolution.files = undefined;
1313+
13211314
if (resolution.globalCacheResolution) resolutionsResolvedWithGlobalCache--;
13221315
if (resolution.globalCacheResolution === false) resolutionsResolvedWithoutGlobalCache--;
1316+
13231317
const resolved = getResolutionWithResolvedFileName(resolution);
13241318
if (resolved && resolved.resolvedFileName) {
13251319
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)