Skip to content

Commit

Permalink
fix: out-of-memory error during peers resolution (#8058)
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan committed May 7, 2024
1 parent 43b6bb7 commit 2cb67d7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
6 changes: 6 additions & 0 deletions .changeset/kind-radios-admire.md
@@ -0,0 +1,6 @@
---
"@pnpm/resolve-dependencies": patch
"pnpm": patch
---

Improve the performance of the peers resolution stage by utilizing more cache [#8058](https://github.com/pnpm/pnpm/pull/8058).
54 changes: 33 additions & 21 deletions pkg-manager/resolve-dependencies/src/resolvePeers.ts
Expand Up @@ -350,28 +350,40 @@ async function resolvePeersOfNode<T extends PartialResolvedPackage> (
}))
)
)
const hit = ctx.peersCache.get(resolvedPackage.depPath)?.find((cache) => {
for (const [name, cachedNodeId] of cache.resolvedPeers) {
const parentPkgNodeId = parentPkgs[name]?.nodeId
if (!parentPkgNodeId || !cachedNodeId) return false
if (parentPkgNodeId === cachedNodeId) continue
if (
ctx.pathsByNodeId.has(cachedNodeId) &&
ctx.pathsByNodeId.get(cachedNodeId) === ctx.pathsByNodeId.get(parentPkgNodeId)
) continue
if (!ctx.dependenciesTree.has(parentPkgNodeId) && parentPkgNodeId.startsWith('link:')) {
return false
const hit = findHit(resolvedPackage.depPath)
function findHit (depPath: string, pendingCacheSearches: string[] = []) {
const cacheItems = ctx.peersCache.get(depPath)
if (!cacheItems) return undefined
const newPendingCacheSearches = [...pendingCacheSearches, depPath]
return cacheItems.find((cache) => {
for (const [name, cachedNodeId] of cache.resolvedPeers) {
const parentPkgNodeId = parentPkgs[name]?.nodeId
if (!parentPkgNodeId || !cachedNodeId) return false
if (parentPkgNodeId === cachedNodeId) continue
if (
ctx.pathsByNodeId.has(cachedNodeId) &&
ctx.pathsByNodeId.get(cachedNodeId) === ctx.pathsByNodeId.get(parentPkgNodeId)
) continue
if (!ctx.dependenciesTree.has(parentPkgNodeId) && parentPkgNodeId.startsWith('link:')) {
return false
}
const parentDepPath = (ctx.dependenciesTree.get(parentPkgNodeId)!.resolvedPackage as T).depPath
if (!ctx.purePkgs.has(parentDepPath)) {
if (
newPendingCacheSearches.includes(parentDepPath) ||
findHit(parentDepPath, newPendingCacheSearches)
) continue
return false
}
const cachedDepPath = (ctx.dependenciesTree.get(cachedNodeId)!.resolvedPackage as T).depPath
if (parentDepPath !== cachedDepPath) return false
}
const parentDepPath = (ctx.dependenciesTree.get(parentPkgNodeId)!.resolvedPackage as T).depPath
if (!ctx.purePkgs.has(parentDepPath)) return false
const cachedDepPath = (ctx.dependenciesTree.get(cachedNodeId)!.resolvedPackage as T).depPath
if (parentDepPath !== cachedDepPath) return false
}
for (const missingPeer of cache.missingPeers) {
if (parentPkgs[missingPeer]) return false
}
return true
})
for (const missingPeer of cache.missingPeers) {
if (parentPkgs[missingPeer]) return false
}
return true
})
}
if (hit != null) {
return {
missingPeers: hit.missingPeers,
Expand Down

0 comments on commit 2cb67d7

Please sign in to comment.