Skip to content

Commit

Permalink
fix(core): calculate project dependencies upfront in the schedule (#2…
Browse files Browse the repository at this point in the history
…8152)

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

Counting project dependencies during the sort resulted in a lot of
duplicate work.

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

Count project dependencies for all tasks up front.. so that this
information can be easily accessed during the sort.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #

(cherry picked from commit c5cb015)
  • Loading branch information
FrozenPandaz committed Sep 27, 2024
1 parent 0a4c620 commit f2f1419
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions packages/nx/src/tasks-runner/tasks-schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class TasksSchedule {
private completedTasks = new Set<string>();
private scheduleRequestsExecutionChain = Promise.resolve();
private estimatedTaskTimings: Record<string, number> = {};
private projectDependencies: Record<string, number> = {};

constructor(
private readonly projectGraph: ProjectGraph,
Expand All @@ -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() {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit f2f1419

Please sign in to comment.