From 0de95dcde847497b5897c96e10b33b083f7da3a4 Mon Sep 17 00:00:00 2001 From: FrozenPandaz Date: Thu, 27 Jul 2023 21:25:31 -0400 Subject: [PATCH] fix(core): fix performance of hashing external dependencies up front --- .../__snapshots__/task-hasher.spec.ts.snap | 2 +- packages/nx/src/hasher/task-hasher.ts | 26 ++++++++++--------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/packages/nx/src/hasher/__snapshots__/task-hasher.spec.ts.snap b/packages/nx/src/hasher/__snapshots__/task-hasher.spec.ts.snap index d7519823406ffa..955e74dcdc1ff3 100644 --- a/packages/nx/src/hasher/__snapshots__/task-hasher.spec.ts.snap +++ b/packages/nx/src/hasher/__snapshots__/task-hasher.spec.ts.snap @@ -63,7 +63,7 @@ exports[`TaskHasher hashTarget should hash entire subtree of dependencies 1`] = }, "runtime": {}, }, - "value": "17607022607820563118", + "value": "14419327228911184578", } `; diff --git a/packages/nx/src/hasher/task-hasher.ts b/packages/nx/src/hasher/task-hasher.ts index 45c5a0a7f0284a..765e79a551481a 100644 --- a/packages/nx/src/hasher/task-hasher.ts +++ b/packages/nx/src/hasher/task-hasher.ts @@ -186,9 +186,9 @@ class TaskHasherImpl { private runtimeHashes: { [runtime: string]: Promise; } = {}; - private externalDependencyHashes: Map = new Map< + private externalDependencyHashes: Map = new Map< string, - PartialHash + PartialHash[] >(); private allExternalDependenciesHash: PartialHash; private projectRootMappings = createProjectRootMappings( @@ -416,22 +416,23 @@ class TaskHasherImpl { } private getExternalDependencyHash(externalNodeName: string) { - return this.externalDependencyHashes.get(externalNodeName); + return this.combinePartialHashes( + this.externalDependencyHashes.get(externalNodeName) + ); } private hashExternalDependency( externalNodeName: string, visited: Set - ): PartialHash { + ): PartialHash[] { // try to retrieve the hash from cache if (this.externalDependencyHashes.has(externalNodeName)) { return this.externalDependencyHashes.get(externalNodeName); } visited.add(externalNodeName); const node = this.projectGraph.externalNodes[externalNodeName]; - let partialHash: PartialHash; + const partialHashes: PartialHash[] = []; if (node) { - const partialHashes: PartialHash[] = []; if (node.data.hash) { // we already know the hash of this dependency partialHashes.push({ @@ -453,25 +454,26 @@ class TaskHasherImpl { if (this.projectGraph.dependencies[externalNodeName]) { this.projectGraph.dependencies[externalNodeName].forEach((d) => { if (!visited.has(d.target)) { - partialHashes.push(this.hashExternalDependency(d.target, visited)); + partialHashes.push( + ...this.hashExternalDependency(d.target, visited) + ); } }); } - partialHash = this.combinePartialHashes(partialHashes); } else { // unknown dependency // this may occur if dependency is not an npm package // but rather symlinked in node_modules or it's pointing to a remote git repo // in this case we have no information about the versioning of the given package - partialHash = { + partialHashes.push({ value: `__${externalNodeName}__`, details: { [externalNodeName]: `__${externalNodeName}__`, }, - }; + }); } - this.externalDependencyHashes.set(externalNodeName, partialHash); - return partialHash; + this.externalDependencyHashes.set(externalNodeName, partialHashes); + return partialHashes; } private hashTarget(