Skip to content

Reduce per-request CPU/memory in workspace-package check and tarball fetch#49

Merged
fengmk2 merged 3 commits into
mainfrom
perf/reduce-cpu-memory
Jul 1, 2026
Merged

Reduce per-request CPU/memory in workspace-package check and tarball fetch#49
fengmk2 merged 3 commits into
mainfrom
perf/reduce-cpu-memory

Conversation

@fengmk2

@fengmk2 fengmk2 commented Jul 1, 2026

Copy link
Copy Markdown
Member

Summary

  • isWorkspacePackage re-split/trimmed WORKSPACE_PACKAGES on every call even though it's a static Worker var for the isolate's lifetime; memoize it, sharing the split/trim/filter parse with parseConfiguredPreviewRefs via a new splitCsv util.
  • fetchUpstreamTarball collected chunks into an array then copied them into a second, final buffer. Upstream responses always declare Content-Length, so it now streams straight into one pre-sized buffer in that case, halving peak memory for the common path (falls back to the old approach when it's absent).

Test plan

  • pnpm typecheck
  • pnpm test (82/82 passing, 5 new tests covering the pre-sized/fallback paths and all size-guard cases)

fengmk2 added 3 commits July 1, 2026 22:06
…ball downloads into one buffer

isWorkspacePackage re-split/trimmed WORKSPACE_PACKAGES on every call even
though it's a static Worker var; memoize it (and share the split/trim/filter
parse with parseConfiguredPreviewRefs via a new splitCsv util).

fetchUpstreamTarball collected chunks into an array then copied them into a
final buffer. When Content-Length is known (always, from pkg.pr.new), stream
straight into one pre-sized buffer instead, halving peak memory for that path.
… cap

The xhigh code review flagged that the pre-sized buffer path enforced the
upstream's Content-Length header as an exact byte ceiling, so a legitimate
small tarball could be rejected with a spurious 413 if the header slightly
under-reported the real body (a re-chunking proxy, an off-by-a-few-bytes
value), or if the header was blank (Number('') coerces to 0, not NaN).

Unify the pre-sized and fallback paths into one growable buffer: pre-size
from Content-Length as a hint (validated as a strict non-negative integer),
but only ever reject on the actual maxBytes limit, growing the buffer when
the declared size undershoots. Rebuilt the action bundle (publish-preview
imports isWorkspacePackage/parseConfiguredPreviewRefs, both changed in the
prior commit).
Codex review flagged that unifying both paths into one growable buffer made
the truly-unknown-length case (no Content-Length at all) start from a 64KiB
guess and double up to the maxBytes cap before slicing back down, so a body
just over half of maxBytes could transiently allocate close to 2x maxBytes,
worse than the original chunk-array approach's ~2x actual-size peak.

Keep the pre-sized/grow-on-undershoot buffer only for when a Content-Length
hint exists (where a wrong guess is expected to be a small correction, not an
order-of-magnitude gap); the no-hint case goes back to collecting chunks and
copying once into an exactly-sized buffer at the end, so peak memory tracks
the actual body size instead of a doubling sequence that can overshoot it.
@fengmk2 fengmk2 merged commit d47efdf into main Jul 1, 2026
4 checks passed
@fengmk2 fengmk2 deleted the perf/reduce-cpu-memory branch July 1, 2026 15:50
fengmk2 added a commit that referenced this pull request Jul 1, 2026
Reverts #49 to restore service. See PR description for the ERR_PNPM_TARBALL_EXTRACT report that triggered this.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant