Skip to content

Report a buffer-backed body's bytes as external memory - #8828

Open
git-bruh wants to merge 1 commit into
mainfrom
pkhanna/response-body-external-memory
Open

git-bruh wants to merge 1 commit into
mainfrom
pkhanna/response-body-external-memory

Conversation

@git-bruh

Copy link
Copy Markdown
Member

A Request or Response built from a string, BufferSource, Blob, FormData or URLSearchParams keeps a kj-heap copy of its bytes in a JsReadableStream::Buffer. V8 never learned of that copy: to it each body was a small wrapper. A Worker that kept making bodies from a large buffer left the dead copies waiting on a GC that their size never prompted, and they weren't counted against the isolate's memory limit.

Buffer now holds an ExternalMemoryAdjustment for its bytes. Clones and tee branches share the Buffer, so they don't count it again. The adjustment is safe to release without the isolate lock, as happens when the stream's native source drops its reference.

@git-bruh
git-bruh requested review from a team as code owners November 23, 2026 13:26

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 6faee2ad.

This review, of ce613262

✅ Approved — The Buffer change is small and correct. Both constructors report the owned bytes once, shared clones and tee branches don't count them again, and the new tests cover these paths.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

📋 Code Quality: ✅ LGTM

No defects found in the three changed files.

  • Construction: Both Buffer constructors take jsg::Lock&. The Blob constructor delegates to the array constructor, so both build the adjustment from owned.size(). owned is declared, and so initialised, before externalMemoryAdjustment. That means the size is read after the move into owned.
  • Sharing: Clones and tee branches share the kj::Rc<Buffer>, so each buffer is counted only once.
  • Destruction: The adjustment is the last member declared, so it is destroyed first. ExternalMemoryAdjustment defers the update when the isolate lock isn't held, which matches the description. A zero-size body reports nothing.
  • Tests: The new tests check that the bytes are counted once, that clones and tees don't count them again, and that the memory is released when the buffer is destroyed.

📝 Documentation: ✅ LGTM

The new comment on externalMemoryAdjustment describes how the code works now and explains why the bytes are reported.


Reviewed commit: ce613262

A Request or Response built from a string, BufferSource, Blob,
FormData or URLSearchParams keeps a kj-heap copy of its bytes in a
JsReadableStream::Buffer. V8 never learned of that copy: to it each
body was a small wrapper. A Worker that kept making bodies from a
large buffer left the dead copies waiting on a GC that their size
never prompted, and they weren't counted against the isolate's
memory limit. A production heap profile showed 799 live copies of
one 25 MB body, 20 GB in all.

Buffer now holds an ExternalMemoryAdjustment for its bytes. Clones
and tee branches share the Buffer, so they don't count it again.
The adjustment is safe to release without the isolate lock, as
happens when the stream's native source drops its reference.

Since a Worker that keeps large bodies alive may now exceed its
memory limit, and GCs become more frequent for Workers that make
many bodies, the BODY_BUFFER_EXTERNAL_MEMORY autogate guards the
report. With it off, the adjustment is zero.
@git-bruh
git-bruh force-pushed the pkhanna/response-body-external-memory branch from ce61326 to 6faee2a Compare November 23, 2026 13:34

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved — No issues found. The BODY_BUFFER_EXTERNAL_MEMORY autogate is set up correctly. With the gate off, the adjustment is zero; with it on, each Buffer's owned bytes are reported once.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

📋 Code Quality: ✅ LGTM

The new autogate is set up correctly, and the gated path behaves as intended whether the gate is off or on.

  • Autogate: BODY_BUFFER_EXTERNAL_MEMORY is added to WORKERD_AUTOGATES. Its kebab-case name is derived automatically, as it is for the existing gates.
  • Gate off: the ExternalMemoryAdjustment is still constructed, but with amount 0, so it reports nothing.
  • Gate on: the adjustment is built from owned.size(). owned is declared, and so initialised, before externalMemoryAdjustment, and the adjustment is destroyed first. Clones and tee branches share the kj::Rc<Buffer>, so the bytes are counted only once.
  • Callers: every place that constructs a Buffer now passes jsg::Lock&.
  • Tests: the gate-on tests enable the gate through autogateKeys. The gate-off test sets ignoreAllAutogatesEnv, so it still holds in the @all-autogates variant.

📝 Documentation: ✅ LGTM

The comments on externalMemoryAdjustment and on the new autogate describe the current behaviour accurately, including that nothing is reported unless the gate is enabled.


Reviewed commit: 6faee2ad

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