Skip to content

Commit

Permalink
perf: reduce memory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan committed May 15, 2024
1 parent 918102e commit bfc5c3a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
5 changes: 5 additions & 0 deletions pkg-manager/resolve-dependencies/src/nextNodeId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let nodeIdCounter = 0

export function nextNodeId (): string {
return (++nodeIdCounter).toString()
}
5 changes: 2 additions & 3 deletions pkg-manager/resolve-dependencies/src/resolveDependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,14 @@ import semver from 'semver'
import { encodePkgId } from './encodePkgId'
import { getNonDevWantedDependencies, type WantedDependency } from './getNonDevWantedDependencies'
import { safeIntersect } from './mergePeers'
import { nextNodeId } from './nextNodeId'
import {
nodeIdContainsSequence,
} from './nodeIdUtils'
import { hoistPeers, getHoistableOptionalPeers } from './hoistPeers'
import { wantedDepIsLocallyAvailable } from './wantedDepIsLocallyAvailable'
import { replaceVersionInPref } from './replaceVersionInPref'

let nodeIdCounter = 0

const dependencyResolvedLogger = logger('_dependency_resolved')

const omitDepsFields = omit(['dependencies', 'optionalDependencies', 'peerDependencies', 'peerDependenciesMeta'])
Expand Down Expand Up @@ -1354,7 +1353,7 @@ async function resolveDependency (
// we only ever need to analyze one leaf dep in a graph, so the nodeId can be short and stateless.
const nodeId = pkgIsLeaf(pkg)
? `>${depPath}>`
: (++nodeIdCounter).toString()
: nextNodeId()

const parentIsInstallable = options.parentPkg.installable === undefined || options.parentPkg.installable
const installable = parentIsInstallable && pkgResponse.body.isInstallable !== false
Expand Down
10 changes: 3 additions & 7 deletions pkg-manager/resolve-dependencies/src/resolveDependencyTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import partition from 'ramda/src/partition'
import zipObj from 'ramda/src/zipObj'
import { type WantedDependency } from './getNonDevWantedDependencies'
import { nextNodeId } from './nextNodeId'
import {
nodeIdContainsSequence,
} from './nodeIdUtils'
Expand Down Expand Up @@ -206,7 +207,7 @@ export async function resolveDependencyTree<T> (

ctx.pendingNodes.forEach((pendingNode) => {
ctx.dependenciesTree.set(pendingNode.nodeId, {
children: () => buildTree(ctx, pendingNode.nodeId, pendingNode.resolvedPackage.id,
children: () => buildTree(ctx, pendingNode.resolvedPackage.id,
pendingNode.parentDepPaths,
ctx.childrenByParentDepPath[pendingNode.resolvedPackage.depPath], pendingNode.depth + 1, pendingNode.installable),
depth: pendingNode.depth,
Expand Down Expand Up @@ -259,16 +260,13 @@ export async function resolveDependencyTree<T> (
}
}

let nodeId = 0

function buildTree (
ctx: {
childrenByParentDepPath: ChildrenByParentDepPath
dependenciesTree: DependenciesTree<ResolvedPackage>
resolvedPackagesByDepPath: ResolvedPackagesByDepPath
skipped: Set<string>
},
parentNodeId: string,
parentId: string,
parentDepPaths: string[],
children: Array<{ alias: string, depPath: string }>,
Expand All @@ -284,13 +282,11 @@ function buildTree (
if (nodeIdContainsSequence(parentDepPaths, parentId, child.depPath) || parentId === child.depPath) {
continue
}
--nodeId
const childNodeId = nodeId.toString() // createNodeId(parentNodeId, child.depPath)
const childNodeId = nextNodeId()
childrenNodeIds[child.alias] = childNodeId
installable = installable || !ctx.skipped.has(child.depPath)
ctx.dependenciesTree.set(childNodeId, {
children: () => buildTree(ctx,
childNodeId,
child.depPath,
[...parentDepPaths, child.depPath],
ctx.childrenByParentDepPath[child.depPath],
Expand Down

0 comments on commit bfc5c3a

Please sign in to comment.