Skip to content

pacquet: peer-resolution machinery dominates large-workspace installs (802 walks / 2.6M node visits, single-threaded, silent) #16

Open

Description

@zkochan

Summary

On a large bit.cloud workspace (114 importers, ~25k graph nodes, ~21k lockfile snapshots — ~/src/sandbox/opus-5), a resolving install spends ~54 of ~63 engine seconds inside the peer-resolution machinery, single-threaded, with no progress output — users see the install "hang" for a minute. Tree resolution, fetching, and linking are all fine.

Phase budget (resolution-only replay via @pnpm/napi with lockfileOnly: true, phase timers around resolve_workspace):

phase time
importer tree inits 3.5 s
prepare initial hoist rounds 5.1 s
complete initial hoist rounds 14–19 s
optional/required hoist loop 4–15 s
merged-tree snapshot 0.6–3 s
final peer pass 13.5 s

Measured structure

  • 802 PeerHoistDiscovery::discover walks per install: 114 importers × ~7 hoist-loop rounds, plus per-importer fixpoint iterations. Every walk re-covers the importer's whole direct forest.
  • 2.6M resolve_node visits for a ~25k-node graph (977k pure-pkg fast-path hits, 1.3M peers-cache hits, 316k fresh walks) — ~100× amplification, ~29 µs/visit.
  • ~250 children-ownership rewrites fire mid-hoist: importer-level hoisted peers claim package children at depth 0 and outrank deeper owners (ChildrenOwner ranks by (depth, importer_order, parent_path)), each transfer runs make_non_owner_nodes_lazy + record_children_rewrite, which invalidates the discovery engine's tree view and caches — forcing everyone back to full walks.
  • Leaf-level sampling (eu-stack on a debuginfo build): SipHash over ~50-char String/NodeId::Leaf(Arc<str>) keys and malloc/free churn dominate; the walker's maps are all default-hasher std::collections.

Optimizations prototyped (bisected with env kill-switches; work stashed on branch perf-opus5)

Combined effect: 77 s → 43.6 s. Each one individually changes some peer-variant bindings in the emitted lockfile (e.g. a [email protected] suffix flipping to 5.9.3, ±42 snapshots) — all still-valid resolutions, but the exhaustive re-walks turn out to be load-bearing for byte-determinism today:

  1. Convergence gate — skip run_required_round for an importer whose previous round ended with no missing required peers (inputs unchanged since). Drift source: owner-scope suppression maps and the preferred-versions fold evolve between rounds.
  2. Incremental discovery — walk only the direct deps added since the importer's last walk (full direct set retained as the importer-parent provider context), accumulating scope-filtered missing reports.
  3. Ownership-rewrite gating — skip the lazy-flip/cache-invalidation when the displaced owner's peer_shadowed set equals the claimant's. Unsound as formulated: child NodeId occurrence identity feeds peer-variant computation, so equal shadow context does not imply identical children at the NodeId level.

Paths forward

  • Fast hasher (rustc-hash/ahash) for the resolver-internal maps — byte-identical by construction, targets the dominant leaf cost across every peer phase.
  • Harden the three structural gates above, accepting a one-time bound-but-valid binding shift (or designing exact replay of the order-sensitive folds).
  • Parallelize per-importer discovery and the final peer pass (single-threaded today; the walker owns a shared mutable tree + caches).
  • Emit a progress/stage event during the peer phase so the reporter shows activity (the "hang" perception), pending TS emission-parity check.

Repro

rm -rf node_modules pnpm-lock.yaml && bd install in the bit.cloud workspace, or resolution-only: dump bit's installOptions via a probe in @teambit/pnpm's lynx.js and call napi.install({...opts, lockfileOnly: true}) directly (~80 s, no linking).


Written by an agent (Claude Code, claude-fable-5).

Activity

  1. github-actions commented on Jul 30, 2026

    @github-actions

    Automated Oz triage is running now. It will classify this issue against the implementation-readiness states — Ready to implement, Ready to spec, Needs info, or Wait to implement — and apply the matching pnpm state: label.

    Follow along in Oz: https://oz.warp.dev/runs/019fb3db-063e-7946-851b-875b7b722c7b


    Written by an agent (Oz, auto).

  2. github-actions commented on Jul 30, 2026

    @github-actions

    Triage result

    • Issue: #13505 — pacquet: peer-resolution machinery dominates large-workspace installs
    • State: Ready to spec
    • Applied label: state: needs design
    • Product / parity: pacquet (the Rust pnpm port). The hot code lives in the pacquet-only peer resolver (pnpm/crates/resolving-deps-resolver/src/resolve_peers.rs, resolve_importer.rs, resolve_dependency_tree.rs); PeerHoistDiscovery::discover, ChildrenOwner, make_non_owner_nodes_lazy, and record_children_rewrite all exist there. This is internal resolution machinery, not shared with the TypeScript CLI's own peer resolver, so no direct install/add/update/remove parity change is mandated — with one caveat: the proposed peer-phase progress/stage event is explicitly "pending TS emission-parity check," and any accepted optimization that shifts emitted peer-variant bindings would change lockfile bytes.
    • Rationale: The goal — cut the ~54s the peer phase burns on large workspaces and stop the silent "hang" — is clearly worthwhile and in scope, so this is not Needs info or Wait to implement. But it is not a bounded one-pass fix either: the issue is a profiling writeup with several competing "paths forward" whose trade-offs are unresolved. The three structural gates each perturb byte-determinism (one is called out as "unsound as formulated"), the fast-hasher swap is safe but only part of the win, and parallelizing the walker requires restructuring today's shared mutable tree + caches. Confirmed against the checkout: no fast hasher (rustc-hash/ahash/FxHashMap) is used anywhere in the Rust stack yet, so that avenue is genuinely open. Choosing which paths to pursue and how to preserve exact lockfile output is a design decision.
    • Next step: Spec the peer-phase performance work — decide the ordered set of changes (start with the byte-identical fast-hasher swap; then design either an exact-replay of the order-sensitive folds or an accepted, bounded one-time binding shift for the convergence/incremental-discovery/ownership-rewrite gates; scope the walker parallelization and the progress-event emission against TS parity) with determinism regression coverage as an acceptance gate.

    Written by an agent (Oz, auto).

  3. zkochan commented on Jul 30, 2026

    @zkochan
    MemberAuthor

    First installment: pnpm/pnpm#13506 lands the convergence gate, incremental discovery, ownership-handover rewrite gating, and an FxHash swap for the resolver-internal maps — resolution on the reference workspace drops from ~77 s to ~36 s (cold-install engine time 2 min → 1 min), with all 7,495 workspace tests passing. Peer bindings can shift once to a different still-range-valid provider; output is deterministic across runs.

    Remaining toward the sub-5 s target, in expected-leverage order:

    1. Parallelize per-importer discovery and the final peer pass — the whole peer phase still runs on one core; the walker owns a shared mutable tree + caches, so this needs either per-importer tree snapshots or a partition of the ownership claims.
    2. Amortize the initial rounds (still 114 full walks + per-importer fixpoint iterations ≈ 19 s of the remaining 36).
    3. Progress/stage events during the peer phase so the reporter shows activity.

    Written by an agent (Claude Code, claude-fable-5).

  4. self-assigned this
    on Aug 1, 2026
  5. zkochan commented on Aug 1, 2026

    @zkochan
    MemberAuthor

    Third installment: pnpm/pnpm#13568 removes the allocating NodeId sort key. Re-profiling the reference workspace first (resolution-only replay, 114 importers, warm cache, offline) showed the phase budget has moved a lot since this issue was filed — the whole resolution now runs ~16s, of which the peer machinery is ~10.5s:

    phase time
    importer inits (tree walk) 2.3s
    prepare initial rounds 1.0s
    complete initial rounds 5.3s (2.2s discovery walks, rest is extend_tree for the hoisted peers)
    optional/required hoist loop 1.2s (0.6s discovery walks)
    merged-tree snapshot 0.2s
    final peer pass 4.6s (2.5s build_final_dep_paths, 1.5–2.3s walk, 0.3s graph rebuild)

    The surprise was build_final_dep_paths: 2.5s of the final pass went into sorting, not peer matching. The passes sort NodeIds for determinism, and the key was a formatted String per element, re-rendered on every comparison by sort_by_key. Deriving Ord and sorting the values drops it to 0.76s (SCC pass 1.10s → 0.12s), taking the whole resolution to ~13.4s and the peer-heavy workspace_full_resolution benchmark from ~1.01s to ~0.85s.

    Remaining toward the sub-5s target, unchanged in order:

    1. Parallelize per-importer discovery and the final peer pass — ~4.6s of discovery walks plus ~2.3s of final walk, still one core.
    2. Amortize the initial rounds.
    3. Progress/stage events during the peer phase.

    Separately, while trying to use lockfile byte-identity as the regression gate for that PR, I found that main is not run-to-run deterministic on this workspace: two consecutive replays of the same binary, same input lockfile, offline: true, emit different peer bindings. Filed as pnpm/pnpm#13567 — it also means byte-comparison can't currently gate resolver changes on a workspace of this shape.


    Written by an agent (Claude Code, claude-opus-5).

  6. 2 remaining items

  7. zkochan commented on Aug 1, 2026

    @zkochan
    MemberAuthor

    Correction to my measurement of pnpm/pnpm#13568, and a benchmark gap worth knowing about.

    The end-to-end figure I quoted for the NodeId sort-key change — "~16.0s → ~13.4s" on the reference workspace — was not measured cleanly: the 16.0s baseline runs restored the input pnpm-lock.yaml and ran offline: true, while several of the 13.4s runs did not. Comparing across protocols like that is worth nothing, and I should not have quoted it. What does hold up for that PR:

    • the in-repo workspace_full_resolution peer-heavy scenario, a clean A/B at the time: 1.00/1.02/1.01s → 0.852/0.856/0.840s (~16%). Today's main, which contains it, measures ~850ms on that scenario, which is consistent;
    • the phase timer it targets: build_final_dep_paths 2.50s → 0.76s, of which the Tarjan pass is 1.10s → 0.12s.

    The CI integrated benchmark did not demonstrate that change either, and I described its output more favourably than it deserved.

    The gap: neither existing scenario of workspace_full_resolution runs the auto-install-peers loop at all. The peer-heavy one hands every importer all the framework packages up front, so no peer is ever missing, the loop converges in one round, and the workspace context is synced exactly once. Anything that changes the cost of a hoist round is invisible to it — I confirmed this by A/B-ing pnpm/pnpm#13571 on it: ~850ms on main and on the branch alike, over 6 interleaved runs. The CI integrated benchmark's peer-heavy scenario is the same fixture shape, plus end-to-end install noise of ±0.4s on a ~1.7s mean.

    pnpm/pnpm#13571 therefore adds a third scenario — peers not provided by the importers, and each importer declaring a different subset of roots, so the hoist loop really runs. It costs ~8x the peer-heavy one (6.9s vs 0.9s) because that loop dominates it, and it separates the two builds cleanly: main 7.68/7.32/7.31s vs branch 7.33/6.78/6.67s, every pair favouring the branch.

    Anyone measuring the remaining items on this issue (parallelizing the peer phase, the children-rewrite re-walks) should use that scenario rather than the peer-heavy one or the integrated benchmark.


    Written by an agent (Claude Code, claude-opus-5).

  8. zkochan commented on Aug 2, 2026

    @zkochan
    MemberAuthor

    Fourth installment: pnpm/pnpm#13579 (the amortizations) and pnpm/pnpm#13581 (a benchmark that can see them).

    Re-profiling first, because the budget in the issue body predates pnpm/pnpm#13571 and pnpm/pnpm#13575 and no longer holds. On the hoist-heavy resolution fixture (331 importers, 5,030 packages), main at 5644b55356 spent:

    item time share
    discovery walks (662) 2.95s 44%
    freeing each round's flattened missing-peer map 1.27s 19%
    re-snapshotting two workspace maps per importer 1.13s 17%
    owner-scope barrier snapshot 0.37s 6%
    final peer pass 0.40s 6%
    everything else 0.53s 8%

    The final peer pass — one of the two parallelization targets listed above — is 6% of the resolve today. The two items above it were both per-importer rescans of the whole workspace, and both came out byte-identically, with no threads added and peak memory down:

    1. Each of first_importer_by_pkg / first_walk_missing_by_pkg now carries the Arc snapshot last projected from it, handed out until a write invalidates it; the writers that usually change nothing test first.
    2. A discovery pass reports its Arc-shared subtree summaries instead of flattening them into an owned pkg id → names map for one consumer to read and drop — 1.6M entries built and freed per wave.

    6.7s → 1.50s (4.4x). Neutral on the 114-importer bit.cloud workspace (10.48 → 10.19s): its importers mostly have their peers provided, so its per-round maps are small. The win needs many importers and many packages.

    The measurement problem, and the fix. Nothing in CI could see any of this. The integrated benchmark's scenarios all converge in one hoist round, and a 40-importer hoist-heavy scenario I built for it measured 820.6ms vs 819.0ms across this very change — a gate that cannot fail. pnpm/pnpm#13581 therefore moved the fixture into the micro-benchmark task, which already builds both sides of a PR and posts their comparison. CI now reports, on its own hardware:

    workspace_resolution/hoist_heavy   main 2.70  661.9±8.70ms   pr 1.00  244.7±0.62ms
    

    ±0.62ms on 244ms — 0.4% variance, against 17–27% for the end-to-end scenario. Anything that reintroduces this cost shows up in that table.

    Remaining, in expected-leverage order:

    1. Parallelize the per-importer discovery walks — still the largest single item, and still single-threaded over a shared mutable tree + caches. Re-profile before starting: this budget has now moved three times.
    2. Progress/stage events during the peer phase, pending the TypeScript emission-parity check.

    Anyone measuring either should use workspace_resolution/hoist_heavy (or --full-workspace-resolution locally); note that lockfile byte-identity still cannot gate resolver changes on a large workspace, per pnpm/pnpm#13567, which I reopened with fresh numbers.


    Written by an agent (Claude Code, claude-opus-5).

  9. zkochan commented on Aug 2, 2026

    @zkochan
    MemberAuthor

    Note for anyone picking up the remaining items here: lockfile byte-identity now works as a regression gate. pnpm/pnpm#13594 made resolution deterministic (8 consecutive replays of the 114-importer workspace byte-identical, where before almost every pair differed), so a resolver change can be verified by comparing emitted lockfiles rather than by arguing equivalence from the code. Several earlier comments on this issue say the opposite; they are obsolete.

    It also removed ~1s from that workspace's resolve (10.2-10.4s to 9.2-9.5s), because the importer races it eliminated were costing redundant subtree walks.

    Remaining, with the fixture caveat that cost me a day here. The synthetic workspace_resolution fixtures and a real workspace disagree completely on resolver internals — 0 tree materializations vs 419,043, 0 multi-item cache buckets vs 90% of lookups, walks at 31% of the resolve vs ~63%. I scoped the walk parallelization as feasible from fixture numbers and had to retract every part of it after measuring the real workspace. Measure there first.

    1. Parallelize the peer walks — still the largest item (~5s of ~9s), but the real workspace shows walks mutating the tree heavily and 27k cache lookups where more than one item matches, so shared concurrent caches are not the small step the fixture suggested.
    2. Progress/stage events during the peer phase, pending the TypeScript emission-parity check.

    Two avenues measured and closed since the last installment: scoping the children-rewrite invalidation (only 41 of 139 forced-full walks touch nothing rewritten, and preserving caches across a rewrite measured slower), and importer inits (96% of it is the tree walk, already concurrent within an importer).


    Written by an agent (Claude Code, claude-opus-5).

  10. zkochan commented on Aug 3, 2026

    @zkochan
    MemberAuthor

    Measured the parallelization item properly, and I would close it as not worth doing in its current form.

    Where the time is (114-importer workspace, ~9.6s resolution replay on 641ccd3df4):

    phase time
    complete initial rounds 3.8s
    final peer pass 3.1s (walks 1.8s, build_final_dep_paths 0.6s, build_final_graph 0.3s)
    prepare initial rounds 1.2s
    hoist loop 0.9s
    importer inits 0.9s

    Why each candidate fails:

    • The peer walks (~6.7s, 67%) — the only target big enough to matter. Parallelizing them means sharing the walker caches concurrently, and dropping that sharing instead costs 65x (202ms to 13.3s, measured with an env-gated kill switch). But shared caches written concurrently have scheduling-dependent contents, which is precisely the class of nondeterminism fix(resolving-deps-resolver): resolve importers' initial waves one at a time pnpm#13594 just removed. Parallelizing here trades the determinism back.
    • build_final_graph (0.3s) — 82% of it is a single order-dependent build loop (173ms of 211ms on the real workspace, and the same ratio on the fixture). Parallelizing the two folds around it saves ~38ms, 0.4%.
    • build_final_dep_paths (0.6s) — a memoized DFS through dependency chains; sequential by construction.
    • Importer inits (0.9s) — already concurrent within each importer: extend_tree resolves siblings in parallel at every level. Only the cross-importer fan-out was removed, deliberately, by 13594.

    Total safe parallel gain: under 1%.

    What would actually unlock the walks is making cached verdicts canonical rather than first-writer-wins, so that concurrent cache writes cannot change the result. That is a redesign of the peers cache, not a parallelization pass, and it should be justified on its own terms rather than as a perf item.

    The remaining item on this issue is the progress/stage events during the peer phase, pending the TypeScript emission-parity check.


    Written by an agent (Claude Code, claude-opus-5).

  11. transferred this issue frompnpm/pnpmon Sep 26, 2026
  12. zkochan commented on Nov 8, 2026

    @zkochan
    MemberAuthor

    Status check against main (2026-11-08).

    The 77 s budget in the body is obsolete: peer resolution fell to about 9.6 s through pnpm/pnpm#13506, pnpm/pnpm#13568, pnpm/pnpm#13579 and pnpm/pnpm#13594, and the comments here rule out parallelizing it.

    Left: progress or stage events during the peer phase. The reporter has none yet.

    Written by an agent (Claude Code, claude-opus-5-5).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions