Skip to content

Account for external memory usage internal streams - #2185

Merged
jasnell merged 2 commits into
mainfrom
jsnell/streams-adjust-external-memory
May 30, 2024
Merged

Account for external memory usage internal streams#2185
jasnell merged 2 commits into
mainfrom
jsnell/streams-adjust-external-memory

Conversation

@jasnell

@jasnell jasnell commented May 29, 2024

Copy link
Copy Markdown
Collaborator

These changes improve the account for external memory usage in internal readable and writable streams. JS-backed streams will be handled in a separate PR.

When a v8::BackingStore is detached from the v8::ArrayBuffer, it is no longer accounted for in the external memory reporting for a given isolate. Here we implement manual adjustments for both pending read into buffers and enqueued data.

Comment thread src/workerd/api/streams/queue.c++ Outdated
@jasnell
jasnell marked this pull request as draft May 29, 2024 16:51
jasnell added 2 commits May 30, 2024 09:46
Switching using an v8::ArrayBuffer rather than a raw v8::BackingStore
will ensure that the external memory is actively tracked by the isolate
while the read is pending.
@jasnell
jasnell force-pushed the jsnell/streams-adjust-external-memory branch from 05fbc20 to 306ecf7 Compare May 30, 2024 16:56
@jasnell jasnell changed the title Account for external memory usage in BYOB ReadableStreams Account for external memory usage internal streams May 30, 2024
@jasnell
jasnell marked this pull request as ready for review May 30, 2024 16:57
@jasnell

jasnell commented May 30, 2024

Copy link
Copy Markdown
Collaborator Author

Ok, this is ready for review. I've limited this PR to only touch the api/streams/internal.* bits. I'll handle the JS-backed streams separately just to make things easier to review.

@a-robinson
a-robinson removed their request for review May 30, 2024 17:54
@jasnell jasnell added the memory label May 30, 2024
byteLength = errorCase ? 0 : UnderlyingSource::DEFAULT_AUTO_ALLOCATE_CHUNK_SIZE;
store = v8::ArrayBuffer::NewBackingStore(js.v8Isolate, byteLength);

if (!v8::ArrayBuffer::MaybeNew(js.v8Isolate, byteLength).ToLocal(&store)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why do we use MaybeNew here but New here?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

If I'm following the link there correctly, this one is actually allocating new space, the other is just wrapping around the existing allocation.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah, of course, so the one that wraps an existing allocation can't fail which is why it's New and not MaybeNew. Makes sense.

@jasnell
jasnell merged commit a20abf8 into main May 30, 2024
@jasnell
jasnell deleted the jsnell/streams-adjust-external-memory branch May 30, 2024 20:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants