From c5cb01527603dea385d698f9cf4c7d2c17dca616 Mon Sep 17 00:00:00 2001 From: Jason Jean Date: Fri, 27 Sep 2024 10:04:38 -0400 Subject: [PATCH] fix(core): calculate project dependencies upfront in the schedule (#28152) ## Current Behavior Counting project dependencies during the sort resulted in a lot of duplicate work. ## Expected Behavior Count project dependencies for all tasks up front.. so that this information can be easily accessed during the sort. ## Related Issue(s) Fixes # --- .../nx/src/tasks-runner/tasks-schedule.ts | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/packages/nx/src/tasks-runner/tasks-schedule.ts b/packages/nx/src/tasks-runner/tasks-schedule.ts index 97ce3f6304f52..3374c536314a1 100644 --- a/packages/nx/src/tasks-runner/tasks-schedule.ts +++ b/packages/nx/src/tasks-runner/tasks-schedule.ts @@ -28,6 +28,7 @@ export class TasksSchedule { private completedTasks = new Set(); private scheduleRequestsExecutionChain = Promise.resolve(); private estimatedTaskTimings: Record = {}; + private projectDependencies: Record = {}; constructor( private readonly projectGraph: ProjectGraph, @@ -42,6 +43,15 @@ export class TasksSchedule { Object.values(this.taskGraph.tasks).map((t) => t.target) ); } + + for (const project of Object.values(this.taskGraph.tasks).map( + (t) => t.target.project + )) { + this.projectDependencies[project] ??= findAllProjectNodeDependencies( + project, + this.reverseProjectGraph + ).length; + } } public async scheduleNextTasks() { @@ -125,14 +135,8 @@ export class TasksSchedule { const project1 = this.taskGraph.tasks[taskId1].target.project; const project2 = this.taskGraph.tasks[taskId2].target.project; - const project1NodeDependencies = findAllProjectNodeDependencies( - project1, - this.reverseProjectGraph - ).length; - const project2NodeDependencies = findAllProjectNodeDependencies( - project2, - this.reverseProjectGraph - ).length; + const project1NodeDependencies = this.projectDependencies[project1]; + const project2NodeDependencies = this.projectDependencies[project2]; const dependenciesDiff = project2NodeDependencies - project1NodeDependencies;