Skip to content

Commit cb82726

Browse files
fix(core): performance improvements (#38)
* fix(core): performance improvements * test for repeating visits
1 parent 0e1c7f5 commit cb82726

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

libs/core/src/true-affected.spec.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,10 @@ describe('trueAffected', () => {
459459
filePath: 'proj1/index.ts',
460460
changedLines: [2],
461461
},
462+
{
463+
filePath: 'proj2/index.ts',
464+
changedLines: [6],
465+
},
462466
];
463467
jest.spyOn(git, 'getChangedFiles').mockReturnValue(changedFiles);
464468

@@ -512,10 +516,26 @@ describe('trueAffected', () => {
512516
`Added package proj1 to affected packages for changed line ${changedFiles[0].changedLines[0]} in ${changedFiles[0].filePath}`
513517
);
514518
expect(debug).toHaveBeenCalledWith(
515-
expect.stringMatching(
516-
new RegExp(`^Found identifier .* in .*${changedFiles[0].filePath}$`)
517-
)
519+
`Added package proj2 to affected packages for changed line ${changedFiles[1].changedLines[0]} in ${changedFiles[1].filePath}`
520+
);
521+
522+
const project1VisitingLogs = debug.mock.calls.filter(
523+
([arg]) => expect.stringMatching(
524+
new RegExp(`^Visiting .* in .*${changedFiles[0].filePath}$`)
525+
).asymmetricMatch(arg)
518526
);
527+
528+
expect(project1VisitingLogs).toHaveLength(1);
529+
530+
const project2VisitingLogs = debug.mock.calls.filter(
531+
([arg]) => expect.stringMatching(
532+
new RegExp(`^Visiting .* in .*${changedFiles[1].filePath}$`)
533+
).asymmetricMatch(arg)
534+
);
535+
// would have duplicates if visited multiple times
536+
const uniqueProject2VisitingLogs = new Set(project2VisitingLogs.map(([arg]) => arg));
537+
expect(uniqueProject2VisitingLogs.size).toBe(project2VisitingLogs.length);
538+
519539
expect(debug).toHaveBeenCalledWith(
520540
'Added package proj2 to affected packages'
521541
);

libs/core/src/true-affected.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ export const trueAffected = async ({
240240
}
241241

242242
const affectedPackages = new Set<string>(changedIncludedFilesPackages);
243-
const visitedIdentifiers = new Map<string, string[]>();
243+
const visitedIdentifiers = new Map<string, Set<string>>();
244244

245245
const findReferencesLibs = (node: Node<ts.Node>) => {
246246
const rootNode = findRootNode(node);
@@ -269,7 +269,6 @@ export const trueAffected = async ({
269269
/* istanbul ignore next */
270270
if (identifier == null) return;
271271

272-
const refs = identifier.findReferencesAsNodes();
273272
const identifierName = identifier.getText();
274273
const path = rootNode.getSourceFile().getFilePath();
275274

@@ -278,22 +277,27 @@ export const trueAffected = async ({
278277
);
279278

280279
if (identifierName && path) {
281-
const visited = visitedIdentifiers.get(path) ?? [];
282-
if (visited.includes(identifierName)) {
280+
if (!visitedIdentifiers.has(path)) {
281+
visitedIdentifiers.set(path, new Set());
282+
}
283+
const visited = visitedIdentifiers.get(path)!;
284+
if (visited.has(identifierName)) {
283285
logger.debug(
284286
`Already visited ${chalk.bold(identifierName)} in ${chalk.bold(path)}`
285287
);
286288

287289
return;
288290
}
289291

290-
visitedIdentifiers.set(path, [...visited, identifierName]);
292+
visited.add(identifierName);
291293

292294
logger.debug(
293295
`Visiting ${chalk.bold(identifierName)} in ${chalk.bold(path)}`
294296
);
295297
}
296298

299+
const refs = identifier.findReferencesAsNodes();
300+
297301
refs.forEach((node) => {
298302
const sourceFile = node.getSourceFile();
299303
const pkg = getPackageNameByPath(sourceFile.getFilePath(), projects);

0 commit comments

Comments
 (0)