Skip to content

Commit

Permalink
fix(core): fix performance of hashing external dependencies up front
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Jul 28, 2023
1 parent 4c2f98e commit 0de95dc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ exports[`TaskHasher hashTarget should hash entire subtree of dependencies 1`] =
},
"runtime": {},
},
"value": "17607022607820563118",
"value": "14419327228911184578",
}
`;

Expand Down
26 changes: 14 additions & 12 deletions packages/nx/src/hasher/task-hasher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ class TaskHasherImpl {
private runtimeHashes: {
[runtime: string]: Promise<PartialHash>;
} = {};
private externalDependencyHashes: Map<string, PartialHash> = new Map<
private externalDependencyHashes: Map<string, PartialHash[]> = new Map<
string,
PartialHash
PartialHash[]
>();
private allExternalDependenciesHash: PartialHash;
private projectRootMappings = createProjectRootMappings(
Expand Down Expand Up @@ -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<string>
): 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({
Expand All @@ -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(
Expand Down

0 comments on commit 0de95dc

Please sign in to comment.