Skip to content

perf(pacquet): cut the residual clone fan-out in the resolver tree walk #12

Closed as not planned

Description

@zkochan

Summary

After the #11837 Arc-share wins, the resolver still does a handful of per-recursion clones that show up on profiles: ancestor Vec<String> rebuilds, per-child next_ancestors.clone(), the hoist-loop direct.clone() snapshot, peer-map clones in the hoist pass, and BTreeMap<String, …> allocations on the tree-node children edges. None of them are individually dominant, but together they fan out by (depth × siblings × ancestors) and matter once the bigger fast paths land.

Why this is filed as a follow-up, not a fix

The dominant resolver-side cost on warm-cache alotta-files is still the per-node pick_package walk (~3.1 s of 5.03 s). Until #11843 (peekManifestFromStore), #11844 (leaf-NodeId collapse), and #11845 (lockfile threading) land, optimising these clones is profile-driven premature work — the equilibrium will shift once the resolver stops re-deriving each node from a fetched manifest, and the new dominant cost will tell us which of the items below to tackle first.

This issue is blocked on those three landing so we can profile under the new equilibrium and prioritise correctly. Filing it now so the observations don't get lost.

Concrete clone sites

1. Ancestor chain rebuilt per recursion + cloned per child

resolve_dependency_tree.rs:425-426:

let next_ancestors: Vec<String> =
    ancestor_ids.iter().cloned().chain(std::iter::once(id.clone())).collect();
// ...
let next_ancestors = next_ancestors.clone();  // line 438, once per child

Two allocations per node visit (the new Vec, plus the String clones to fill it), then one Vec + N String clones per child. The chain is immutable once built — every clone is busy-work.

Fix: replace ancestor_ids: &[String] with ancestor_ids: Arc<[Arc<str>]> (or a persistent linked list — im::Vector<Arc<str>>). Per-child becomes an Arc::clone (refcount bump); the chain extension allocates one new slice with N+1 Arc<str> slots, no String cloning. Package ids are already shared at multiple call sites, so promoting them to Arc<str> at the source (the picker's ResolveResult.id field) removes the underlying allocation too.

2. Hoist-loop direct.clone() per snapshot

resolve_importer.rs:175:

let snapshot = ctx.snapshot(direct.clone());

direct: Vec<DirectDep> is cloned once per hoist-loop iteration. DirectDep carries two Strings (alias, id) plus a NodeId, so each clone is O(N) string allocations. The loop iterates O(passes) times.

Fix: ctx.snapshot should take &[DirectDep] and clone internally only the bits it retains (today the whole vec is retained, so the practical fix is to share direct itself via Arc<[DirectDep]> and have the snapshot hold that Arc). Either approach drops the per-pass String storm.

3. Peer hoister's per-edge peer_name.clone() + range.clone()

hoist_peers.rs:83-135 calls peer_name.clone() and range.clone() for every hoisted-peer edge across all variants. Roughly (peer-bearing packages) × (variants per package) String allocations.

Fix: intern peer names and version ranges as Arc<str> at the source. The shape they enter hoist_peers in is already a &str view; the clones are forced by HashMap<String, _> keying. Re-keying on Arc<str> makes inserts cheap and removes the upstream forced ownership.

4. BTreeMap<String, NodeId> on every tree-node's children edges

resolved_tree.rs:144:

pub children: BTreeMap<String, NodeId>,

BTreeMap is the upstream choice (deterministic iteration for peer-suffix construction), but every node carries a fresh map and the inserts are O(log n). Average node has 0-3 children; the BTree overhead per node dominates for the leaf-and-near-leaf majority.

Fix: small-vec optimisation — SmallVec<[(Arc<str>, NodeId); 4]> or similar, with iteration sorted on construction. Keep BTreeMap for the rare node with many children. (This one is cosmetic if #11844's leaf-collapse lands — collapsed leaves stop creating per-occurrence children maps entirely.)

5. all_peer_dep_names: HashSet<String> snapshot

resolve_dependency_tree.rs:257:

all_peer_dep_names: lock_recoverable(&self.all_peer_dep_names).clone(),

The snapshot clones the entire HashSet<String> of all peer dep names every hoist iteration. Same Arc-share fix: hold Arc<HashSet<Arc<str>>> and snapshot via Arc::clone. Mutations require a clone-on-write but those happen once per peer-bearing package, not per iteration.

Prioritisation hint

When this issue unblocks (i.e. #10, pnpm/pnpm#11844, #11 have landed and a fresh warm-cache profile is in hand), tackle in roughly this order:

  1. Ancestor chain Arc<[Arc<str>]> — biggest fan-out (depth × siblings × ancestors), simplest change.
  2. direct.clone() snapshot — per-pass, but the constant factor is high (full String storm).
  3. Peer hoister interning — once the peer-bearing tail is the new hot path, this becomes meaningful.
  4. BTreeMap → SmallVec — measure first; perf(pacquet): collapse leaf-package occurrences in the dependencies tree like pnpm does pnpm#11844's leaf collapse may make this redundant.
  5. all_peer_dep_names Arc — smallest constant; do last or fold into peer-hoister interning.

References


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

Activity

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

    @zkochan
    MemberAuthor

    This waited for #10, #11 and pnpm/pnpm#11844, which have all landed, and most of the clone sites it lists were rewritten or removed since: ancestors are now Arc<Vec<Arc<str>>>, children are TreeChildren (pnpm/pnpm#11915), and hoist_peers.rs is gone.

    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

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions