Skip to content

Commit

Permalink
feat(core): improve graph utils matrix generation (#11226)
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Jul 20, 2022
1 parent 8f628cd commit 5f097b9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
42 changes: 22 additions & 20 deletions packages/workspace/src/utils/graph-utils.ts
Expand Up @@ -17,44 +17,46 @@ 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);
}
}
}

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,
Expand Down Expand Up @@ -120,7 +122,7 @@ export function pathExists(
reach.adjList = adjList;
}

return reach.matrix[sourceProjectName][targetProjectName];
return !!reach.matrix[sourceProjectName][targetProjectName];
}

export function checkCircularPath(
Expand Down
1 change: 0 additions & 1 deletion packages/workspace/src/utils/runtime-lint-utils.ts
Expand Up @@ -8,7 +8,6 @@ import {
parseJson,
ProjectGraphExternalNode,
joinPathFragments,
FileData,
} from '@nrwl/devkit';
import { join } from 'path';
import { workspaceRoot } from './app-root';
Expand Down

0 comments on commit 5f097b9

Please sign in to comment.