mirror of
https://github.com/pnpm/pnpm.git
synced 2026-03-12 04:07:30 -04:00
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:
5
.changeset/twelve-gifts-rescue.md
Normal file
5
.changeset/twelve-gifts-rescue.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@pnpm/workspace.pkgs-graph": patch
|
||||
---
|
||||
|
||||
Speed up createPkgGraph when directory specifiers are present
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user