Skip to content

Add external memory accounting to commonly used stream API objects #4233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 9, 2025

Conversation

jp4a50
Copy link
Contributor

@jp4a50 jp4a50 commented Jun 3, 2025

No description provided.

jp4a50 added 2 commits June 3, 2025 14:39
ReadableStream and WritableStream objects with all their implementations
details use >1kB native memory which has not been counted towards
isolate memory usage until now, allowing isolates to use far more memory
than their intended limit.
@jp4a50 jp4a50 requested a review from jasnell June 3, 2025 13:51
@jp4a50 jp4a50 requested review from a team as code owners June 3, 2025 13:51
Copy link
Contributor

@windsurf-bot windsurf-bot bot left a comment

Choose a reason for hiding this comment

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

Other comments (5)
  • src/workerd/jsg/jsg.c++ (356-356) The change from `kj::atomicAddRef(*this)` to `this->addRefToThis()` should maintain the same thread-safety guarantees. Please ensure that `addRefToThis()` properly handles atomic reference counting if this object can be accessed from multiple threads.
  • src/workerd/api/streams/writable.c++ (296-299) The comment mentions accounting for memory overhead, but the calculation `sizeof(WritableStream) + controller->jsgGetMemorySelfSize()` only adds the controller's size to the WritableStream size. If there's actual memory accounting overhead that should be included, it should be explicitly added to the calculation.
  • src/workerd/api/streams/compression.h (34-34) The change from `kj::Own` to `kj::Arc` for `externalMemoryTarget` suggests a shift from unique to shared ownership. Consider adding a comment explaining why this change is necessary and what implications it has for memory management, especially since this class is handling external memory accounting.
  • src/workerd/jsg/setup.c++ (232-233) The return type of `getExternalMemoryTarget()` has changed from `kj::Own` to `kj::Arc`. Make sure any documentation or comments referencing this method are updated to reflect this change in return type.
  • src/workerd/api/streams/readable.c++ (497-499) While accounting for the ReadableStream and its controller together reduces overhead, this approach won't track any dynamic memory changes in the controller after initialization. Consider whether the controller's memory usage is expected to remain stable throughout its lifetime, or if additional accounting might be needed for significant memory changes.
2 files skipped due to size limits:
  • src/workerd/jsg/jsg.h
  • src/workerd/api/streams/standard.c++

@jp4a50 jp4a50 merged commit 18f3b5d into main Jun 9, 2025
18 checks passed
@jp4a50 jp4a50 deleted the jphillips/account-stream-memory branch June 9, 2025 12:06
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.

3 participants