Repository navigation
Conversation
There was a problem hiding this comment.
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
Bufferconstructors takejsg::Lock&. The Blob constructor delegates to the array constructor, so both build the adjustment fromowned.size().ownedis declared, and so initialised, beforeexternalMemoryAdjustment. That means the size is read after the move intoowned. - 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.
ExternalMemoryAdjustmentdefers 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.
ce61326 to
6faee2a
Compare
There was a problem hiding this comment.
✅ 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_MEMORYis added toWORKERD_AUTOGATES. Its kebab-case name is derived automatically, as it is for the existing gates. - Gate off: the
ExternalMemoryAdjustmentis still constructed, but with amount 0, so it reports nothing. - Gate on: the adjustment is built from
owned.size().ownedis declared, and so initialised, beforeexternalMemoryAdjustment, and the adjustment is destroyed first. Clones and tee branches share thekj::Rc<Buffer>, so the bytes are counted only once. - Callers: every place that constructs a
Buffernow passesjsg::Lock&. - Tests: the gate-on tests enable the gate through
autogateKeys. The gate-off test setsignoreAllAutogatesEnv, so it still holds in the@all-autogatesvariant.
📝 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
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.