Skip to content

Commit 6e1f284

Browse files
committed
fix: clean up idealTree code
1 parent 3fd5213 commit 6e1f284

1 file changed

Lines changed: 35 additions & 13 deletions

File tree

workspaces/arborist/lib/arborist/build-ideal-tree.js

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,30 +56,52 @@ const _global = Symbol.for('global')
5656
const _idealTreePrune = Symbol.for('idealTreePrune')
5757

5858
// Push items in, pop them sorted by depth and then path
59+
// Sorts physically shallower deps up to the front of the queue, because
60+
// they'll affect things deeper in, then alphabetical for consistency between
61+
// installs
5962
class DepsQueue {
63+
// [{ sorted, items }] indexed by depth
6064
#deps = []
6165
#sorted = true
66+
#minDepth = 0
67+
#length = 0
6268

6369
get length () {
64-
return this.#deps.length
70+
return this.#length
6571
}
6672

6773
push (item) {
68-
if (!this.#deps.includes(item)) {
69-
this.#sorted = false
70-
this.#deps.push(item)
74+
if (!this.#deps[item.depth]) {
75+
this.#length++
76+
this.#deps[item.depth] = { sorted: true, items: [item] }
77+
// no minDepth check needed, this branch is only reached when we are in
78+
// the middle of a shallower depth and creating a new one
79+
return
80+
}
81+
if (!this.#deps[item.depth].items.includes(item)) {
82+
this.#length++
83+
this.#deps[item.depth].sorted = false
84+
this.#deps[item.depth].items.push(item)
85+
if (item.depth < this.#minDepth) {
86+
this.#minDepth = item.depth
87+
}
7188
}
7289
}
7390

7491
pop () {
75-
if (!this.#sorted) {
76-
// sort physically shallower deps up to the front of the queue, because
77-
// they'll affect things deeper in, then alphabetical
78-
this.#deps.sort((a, b) =>
79-
(a.depth - b.depth) || localeCompare(a.path, b.path))
80-
this.#sorted = true
92+
let depth
93+
while (!depth?.items.length) {
94+
depth = this.#deps[this.#minDepth]
95+
if (!depth?.items.length) {
96+
this.#minDepth++
97+
}
98+
}
99+
if (!depth.sorted) {
100+
depth.items.sort((a, b) => localeCompare(a.path, b.path))
101+
depth.sorted = true
81102
}
82-
return this.#deps.shift()
103+
this.#length--
104+
return depth.items.shift()
83105
}
84106
}
85107

@@ -1016,7 +1038,7 @@ This is a one-time fix-up, please be patient...
10161038
// may well be an optional dep that has gone missing. it'll
10171039
// fail later anyway.
10181040
for (const e of this.#problemEdges(placed)) {
1019-
promises.push(
1041+
promises.push(() =>
10201042
this.#fetchManifest(npa.resolve(e.name, e.spec, fromPath(placed, e)))
10211043
.catch(er => null)
10221044
)
@@ -1031,7 +1053,7 @@ This is a one-time fix-up, please be patient...
10311053
}
10321054
}
10331055

1034-
await Promise.all(promises)
1056+
await promiseCallLimit(promises)
10351057
return this.#buildDepStep()
10361058
}
10371059

0 commit comments

Comments
 (0)