Skip to content
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

stream: improve webstream readable async iterator performance #49662

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 45 additions & 29 deletions lib/internal/webstreams/readablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,12 @@ class ReadableStream {

// eslint-disable-next-line no-use-before-define
const reader = new ReadableStreamDefaultReader(this);
let done = false;

const state = {
rluvaton marked this conversation as resolved.
Show resolved Hide resolved
done: false,
current: undefined,
rluvaton marked this conversation as resolved.
Show resolved Hide resolved
};
let started = false;
let current;

// The nextSteps function is not an async function in order
// to make it more efficient. Because nextSteps explicitly
Expand All @@ -488,7 +491,7 @@ class ReadableStream {
// unnecessary Promise allocations to occur, which just add
// cost.
function nextSteps() {
if (done)
if (state.done)
return PromiseResolve({ done: true, value: undefined });

if (reader[kState].stream === undefined) {
Expand All @@ -498,31 +501,17 @@ class ReadableStream {
}
const promise = createDeferredPromise();

readableStreamDefaultReaderRead(reader, {
[kChunk](chunk) {
current = undefined;
promise.resolve({ value: chunk, done: false });
},
[kClose]() {
current = undefined;
done = true;
readableStreamReaderGenericRelease(reader);
promise.resolve({ done: true, value: undefined });
},
[kError](error) {
current = undefined;
done = true;
readableStreamReaderGenericRelease(reader);
promise.reject(error);
},
});
// eslint-disable-next-line no-use-before-define
const readRequest = new ReadableStreamAsyncIteratorReadRequest(reader, state, promise);

readableStreamDefaultReaderRead(reader, readRequest);
return promise.promise;
}

async function returnSteps(value) {
if (done)
if (state.done)
return { done: true, value }; // eslint-disable-line node-core/avoid-prototype-pollution
done = true;
state.done = true;

if (reader[kState].stream === undefined) {
throw new ERR_INVALID_STATE.TypeError(
Expand Down Expand Up @@ -559,19 +548,19 @@ class ReadableStream {
// need to investigate if it's a bug in our impl or
// the spec.
if (!started) {
current = PromiseResolve();
state.current = PromiseResolve();
started = true;
}
current = current !== undefined ?
PromisePrototypeThen(current, nextSteps, nextSteps) :
state.current = state.current !== undefined ?
PromisePrototypeThen(state.current, nextSteps, nextSteps) :
nextSteps();
return current;
return state.current;
},

return(error) {
return current ?
return state.current ?
PromisePrototypeThen(
current,
state.current,
() => returnSteps(error),
() => returnSteps(error)) :
returnSteps(error);
Expand Down Expand Up @@ -773,6 +762,33 @@ function createReadableStreamBYOBRequest(controller, view) {
return stream;
}

class ReadableStreamAsyncIteratorReadRequest {
constructor(reader, state, promise) {
this.reader = reader;
this.state = state;
this.promise = promise;
}

[kChunk](chunk) {
this.state.current = undefined;
this.promise.resolve({ value: chunk, done: false });
}

[kClose]() {
this.state.current = undefined;
this.state.done = true;
readableStreamReaderGenericRelease(this.reader);
this.promise.resolve({ done: true, value: undefined });
}

[kError](error) {
this.state.current = undefined;
this.state.done = true;
readableStreamReaderGenericRelease(this.reader);
this.promise.reject(error);
}
}

class DefaultReadRequest {
constructor() {
this[kState] = createDeferredPromise();
Expand Down