perf(pkgs-graph): speed up createPkgGraph by using a table for manifest name lookup (#6287)

This commit is contained in:
Jake Bailey
2023-03-27 10:02:31 -07:00
committed by GitHub
parent c45a4211f2
commit 35d98c7a8f
2 changed files with 13 additions and 2 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/workspace.pkgs-graph": patch
---
Speed up createPkgGraph by using a table for manifest name lookup

View File

@@ -36,6 +36,12 @@ export function createPkgGraph<T> (pkgs: Array<Package & T>, opts?: {
} {
const pkgMap = createPkgMap(pkgs)
const pkgMapValues = Object.values(pkgMap)
const pkgMapByManifestName: Record<string, Package[] | undefined> = {}
for (const pkg of pkgMapValues) {
if (pkg.manifest.name) {
(pkgMapByManifestName[pkg.manifest.name] ??= []).push(pkg)
}
}
const unmatched: Array<{ pkgName: string, range: string }> = []
const graph = mapValues((pkg) => ({
dependencies: createNode(pkg),
@@ -76,8 +82,8 @@ export function createPkgGraph<T> (pkgs: Array<Package & T>, opts?: {
if (spec.type !== 'version' && spec.type !== 'range') return ''
const pkgs = pkgMapValues.filter(pkg => pkg.manifest.name === depName)
if (pkgs.length === 0) return ''
const pkgs = pkgMapByManifestName[depName]
if (!pkgs || pkgs.length === 0) return ''
const versions = pkgs.filter(({ manifest }) => manifest.version)
.map(pkg => pkg.manifest.version) as string[]