Skip to content

Follow-up pnpr + pnpm install optimizations: precalculated bins/shebangs, tarball size stats, and abbreviated packument coverage #178

Closed
pnpm/pnpm
#17186

Description

@zkochan

Summary

Follow-up to #177 (dist.pnpr.requiresBuild).

While pnpr is already streaming .tar archives on publish (pnpr/crates/pnpr/src/server/publishing/attachments.rs) and on first upstream decode (pnpr/crates/pnpr/src/server/tar_responses.rs), and projecting packuments in pnpr/crates/upstream/src/abbreviate.rs, there are three related optimizations that can be evaluated and benchmarked independently:


1. Precalculated bins + shebang runtime (dist.pnpr.bins)

Problem today

  1. When a package uses "directories": { "bin": "./bin" }, the packument does not list the command names — commands_from_directories_bin (pnpm/crates/cmd-shim/src/bin_resolver.rs) has to walk the directory on disk after extraction.
  2. On a cold install, CreateVirtualStoreOutput::package_manifests (pnpm/crates/deps-restorer/src/create_virtual_store.rs) only holds warm store-index manifests, so link_direct_dep_bins_prefetched (pnpm/crates/deps-restorer/src/link_bins/direct.rs) falls back to reading and parsing package.json from disk for every cold package with hasBin: true.
  3. For every bin target, read_shebang (pnpm/crates/cmd-shim/src/shim.rs) opens the file on disk and reads its first 512 bytes to detect #!/usr/bin/env node (or other interpreters) before writing .bin shims.

Proposed change & verification

  • While streaming the .tar, pnpr sees package/package.json, all regular files under directories.bin, and the first line of each bin target.
  • Precalculating dist.pnpr.bins (command name → relative path + parsed shebang prog/args) allows pnpm to write .bin shims for requiresBuild: false packages with zero disk reads of package.json or bin scripts (and potentially overlap .bin shim creation with tarball extraction).
  • Benchmark verification needed: Measure the wall-time savings on cold installs (especially on Windows where every bin writes .cmd/.ps1/shell wrappers and on macOS/Linux with preferSymlinkedExecutables: false or extendNodePath: true) vs. the small metadata payload increase for packages with hasBin: true.

2. Backfilling verified fileCount and unpackedSize

Problem today

Packages published before npm 6 (and many non-npm tarballs) omit dist.fileCount and dist.unpackedSize. Without them:

  • extract_tarball_entries (pnpm/crates/tarball/src/extract.rs) starts Vec<PendingFile> with capacity 0 (tar::Entries::size_hint() is always (0, None)), causing repeated reallocations during extraction.
  • decompress_gzip and should_stream_extract (pnpm/crates/tarball/src/extract.rs) lack an accurate size hint.
  • ObservedDistStats (pnpm/crates/resolving-npm-resolver/src/create_npm_resolution_verifier.rs), which the pnpr frozen fast path uses to schedule the heaviest tarball downloads first, has None for those packages.

Proposed change & verification

  • Count regular files and sum their byte sizes while streaming the .tar in pnpr, and populate dist.fileCount and dist.unpackedSize whenever they are missing (or replace untrusted publisher claims with the verified values).
  • Benchmark verification needed: Measure extraction allocation savings and download-scheduling makespan improvements on graphs containing older packages.

3. Eliminating full-packument (application/json) fallbacks when resolving against pnpr

Full packuments (application/json) are 10–50× larger than abbreviated packuments (application/vnd.npm.install-v1+json) because they carry every version's readme, maintainers, full scripts, etc. Currently pnpm still falls back to full packuments in two places that pnpr's abbreviated packument can satisfy:

  • optionalDependencies (PickState::metadata_shape in pnpm/crates/resolving-npm-resolver/src/pick_package/state.rs):
    • pnpm forces full_metadata = true whenever opts.request.optional is true because the public npm registry's abbreviated format drops libc (pnpm/pnpm#9950).
    • However, pnpr's ABBREVIATED_VERSION_FIELDS (pnpr/crates/upstream/src/abbreviate.rs) already preserves "libc", "cpu", and "os". Teaching PickState::metadata_shape that a pnpr registry does not need full metadata for optionalDependencies avoids full-packument downloads for platform-specific optional dependencies (@swc/core-*, @esbuild/*, @rollup/rollup-*).
  • trustPolicy: no-downgrade (TrustEvidence in pnpm/crates/resolving-npm-resolver/src/trust_checks.rs):
    • pnpr's abbreviate_packument (pnpr/crates/upstream/src/abbreviate.rs) already includes coarsened time (so minimumReleaseAge works on abbreviated packuments), but ABBREVIATED_VERSION_FIELDS strips _npmUser (_npmUser.trustedPublisher and _npmUser.approver).
    • Preserving a minimal _npmUser ({ trustedPublisher, approver } when present) in pnpr's abbreviated packument lets pnpm run trustPolicy: no-downgrade on abbreviated packuments without ever upgrading to full packuments.
  • Preserve "bundledDependencies" in ABBREVIATED_VERSION_FIELDS (pnpr/crates/upstream/src/abbreviate.rs):
    • ABBREVIATED_VERSION_FIELDS currently copies "bundleDependencies" but omits "bundledDependencies" (with a d), which BundledDependencies::from_manifest (pnpm/crates/lockfile/src/package_metadata.rs) checks first.
  • Benchmark verification needed: Measure resolution time and metadata transfer byte reduction on cold-cache resolutions with optionalDependencies and trustPolicy: no-downgrade.

Written by an agent (Antigravity, Gemini 4 Argon).

Activity

  1. zkochan commented on Oct 21, 2026

    @zkochan
    MemberAuthor

    Opened pnpm/pnpm#17186 to implement Section 1 (Precalculated bins + shebang runtime (dist.pnpr.bins)), completing the remaining work in this issue following the merge of pnpm/pnpm#11988 for Section 2:

    • pnpr: Computes dist.pnpr.bins (command_name -> { path, prog, args }) during .tar streaming in inspect_tar when requiresBuild is false and the package exposes bins (bin string/object or directories.bin), reading the first 256 bytes of each bin entry to extract any #! shebang runtime, and caches it alongside requiresBuild in TarMetadataCache.
    • pnpm: Threads cold-extracted package.json manifests and precalculated bins from tarball extraction through CreateVirtualStoreOutput (package_manifests and package_bins) into LinkVirtualStoreBins and link_direct_dep_bins_prefetched, and verifies warm-cache dist.pnpr.bins against the store index's authoritative manifest and cas_paths before writing .bin shims via create_cmd_shim_with_runtime with zero disk reads of package.json, directories.bin, or bin script shebangs.

    Written by an agent (Antigravity, Gemini 4 Argon).

  2. added a commit that references this issue on Oct 21, 2026
    e2a11f5
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