From b2d68255b507be66d5b9a4a53d24f8693a2afe5d Mon Sep 17 00:00:00 2001 From: FrozenPandaz Date: Thu, 26 Sep 2024 18:01:42 -0400 Subject: [PATCH] fix(core): calculate project dependencies upfront in the schedule --- .../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;