perf(pkgs-graph): speed up createPkgGraph when directory specifiers are present (#6317)

* perf(pkgs-graph): speed up createPkgGraph when directory specifiers are present

* perf(pkgs-graph): make pkgMapByManifestName equally lazy
This commit is contained in:
Jake Bailey
2023-03-29 12:51:10 -07:00
committed by GitHub
parent 518f5e1432
commit 9fd0e375e9
2 changed files with 35 additions and 6 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/workspace.pkgs-graph": patch
---
Speed up createPkgGraph when directory specifiers are present

View File

@@ -36,12 +36,8 @@ 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)
}
}
let pkgMapByManifestName: Record<string, Package[] | undefined> | undefined
let pkgMapByDir: Record<string, Package | undefined> | undefined
const unmatched: Array<{ pkgName: string, range: string }> = []
const graph = mapValues((pkg) => ({
dependencies: createNode(pkg),
@@ -73,15 +69,25 @@ export function createPkgGraph<T> (pkgs: Array<Package & T>, opts?: {
}
if (spec.type === 'directory') {
pkgMapByDir ??= getPkgMapByDir(pkgMapValues)
const resolvedPath = path.resolve(pkg.dir, spec.fetchSpec)
const found = pkgMapByDir[resolvedPath]
if (found) {
return found.dir
}
// Slow path; only needed when there are case mismatches on case-insensitive filesystems.
const matchedPkg = pkgMapValues.find(pkg => path.relative(pkg.dir, spec.fetchSpec) === '')
if (matchedPkg == null) {
return ''
}
pkgMapByDir[resolvedPath] = matchedPkg
return matchedPkg.dir
}
if (spec.type !== 'version' && spec.type !== 'range') return ''
pkgMapByManifestName ??= getPkgMapByManifestName(pkgMapValues)
const pkgs = pkgMapByManifestName[depName]
if (!pkgs || pkgs.length === 0) return ''
const versions = pkgs.filter(({ manifest }) => manifest.version)
@@ -120,3 +126,21 @@ function createPkgMap (pkgs: Package[]): Record<string, Package> {
}
return pkgMap
}
function getPkgMapByManifestName (pkgMapValues: Package[]) {
const pkgMapByManifestName: Record<string, Package[] | undefined> = {}
for (const pkg of pkgMapValues) {
if (pkg.manifest.name) {
(pkgMapByManifestName[pkg.manifest.name] ??= []).push(pkg)
}
}
return pkgMapByManifestName
}
function getPkgMapByDir (pkgMapValues: Package[]) {
const pkgMapByDir: Record<string, Package | undefined> = {}
for (const pkg of pkgMapValues) {
pkgMapByDir[path.resolve(pkg.dir)] = pkg
}
return pkgMapByDir
}