From 5f097b90ff332cdf202b45293615c2cf292a8ad1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Jona=C5=A1?= Date: Wed, 20 Jul 2022 22:48:56 +0200 Subject: [PATCH] feat(core): improve graph utils matrix generation (#11226) --- packages/workspace/src/utils/graph-utils.ts | 42 ++++++++++--------- .../workspace/src/utils/runtime-lint-utils.ts | 1 - 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/packages/workspace/src/utils/graph-utils.ts b/packages/workspace/src/utils/graph-utils.ts index 92aa4b191dc33..dbf88e2f35c4b 100644 --- a/packages/workspace/src/utils/graph-utils.ts +++ b/packages/workspace/src/utils/graph-utils.ts @@ -17,27 +17,26 @@ const reach: Reach = { }; function buildMatrix(graph: ProjectGraph) { - const dependencies = graph.dependencies; const nodes = Object.keys(graph.nodes); + const nodesLength = nodes.length; const adjList = {}; const matrix = {}; - const initMatrixValues = nodes.reduce((acc, value) => { - return { - ...acc, - [value]: false, - }; - }, {}); - - nodes.forEach((v) => { + // create matrix value set + for (let i = 0; i < nodesLength; i++) { + const v = nodes[i]; adjList[v] = []; - matrix[v] = { ...initMatrixValues }; - }); + // meeroslav: turns out this is 10x faster than spreading the pre-generated initMatrixValues + matrix[v] = {}; + } - for (let proj in dependencies) { - for (let dep of dependencies[proj]) { - if (graph.nodes[dep.target]) { - adjList[proj].push(dep.target); + const projectsWithDependencies = Object.keys(graph.dependencies); + for (let i = 0; i < projectsWithDependencies.length; i++) { + const dependencies = graph.dependencies[projectsWithDependencies[i]]; + for (let j = 0; j < dependencies.length; j++) { + const dependency = dependencies[j]; + if (graph.nodes[dependency.target]) { + adjList[dependency.source].push(dependency.target); } } } @@ -45,16 +44,19 @@ function buildMatrix(graph: ProjectGraph) { const traverse = (s: string, v: string) => { matrix[s][v] = true; - for (let adj of adjList[v]) { - if (matrix[s][adj] === false) { + const adjListLength = adjList[v].length; + for (let i = 0; i < adjListLength; i++) { + const adj = adjList[v][i]; + if (!matrix[s][adj]) { traverse(s, adj); } } }; - nodes.forEach((v) => { + for (let i = 0; i < nodesLength; i++) { + const v = nodes[i]; traverse(v, v); - }); + } return { matrix, @@ -120,7 +122,7 @@ export function pathExists( reach.adjList = adjList; } - return reach.matrix[sourceProjectName][targetProjectName]; + return !!reach.matrix[sourceProjectName][targetProjectName]; } export function checkCircularPath( diff --git a/packages/workspace/src/utils/runtime-lint-utils.ts b/packages/workspace/src/utils/runtime-lint-utils.ts index aefdd1a6f8961..ea27a076d2635 100644 --- a/packages/workspace/src/utils/runtime-lint-utils.ts +++ b/packages/workspace/src/utils/runtime-lint-utils.ts @@ -8,7 +8,6 @@ import { parseJson, ProjectGraphExternalNode, joinPathFragments, - FileData, } from '@nrwl/devkit'; import { join } from 'path'; import { workspaceRoot } from './app-root';