New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf(pkgs-graph): speed up createPkgGraph when directory specifiers are present #6317
Merged
+35
−6
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@pnpm/workspace.pkgs-graph": patch | ||
--- | ||
|
||
Speed up createPkgGraph when directory specifiers are present |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This resolve is defensive; since we're comparing resolved paths above, I wanted to ensure everything went through this algorithm before comparison. It's possible that this is not needed, however, there doesn't seem to be any negative performance hit. |
||
} | ||
return pkgMapByDir | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In theory we could detect whether it is a case-insensitive fs. In a future optimization. I already use this lib for detecting case sensitive dirs: https://github.com/zkochan/packages/tree/main/dir-is-case-sensitive
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's a good idea. Another related optimization would be to ensure that all paths are "resolved", that way all of the path checks can skip re-resolving and instead use plain comparison and
path.join
+path.normalize
.