Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
32 changes: 32 additions & 0 deletions benchmark/streams/iter-throughput-share.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

const common = require('../common.js');

const bench = common.createBenchmark(main, {
consumers: [2, 8, 32],
batches: [1e4],
backpressure: ['block'],
n: [5],
}, {
flags: ['--experimental-stream-iter'],
});

async function main({ consumers, batches, backpressure, n }) {
const { share, array } = require('stream/iter');
const chunk = Buffer.alloc(1024);
const totalOps = batches * consumers * n;

async function* source() {
for (let i = 0; i < batches; i++) {
yield [chunk];
}
}

bench.start();
for (let i = 0; i < n; i++) {
const shared = share(source(), { highWaterMark: 64, backpressure });
const readers = Array.from({ length: consumers }, () => array(shared.pull()));
await Promise.all(readers);
}
bench.end(totalOps);
}
3 changes: 2 additions & 1 deletion lib/internal/streams/iter/broadcast.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,9 @@
// Private methods

#recomputeMinCursor() {
this.#cachedMinCursor = getMinCursor(
const [minCursor] = getMinCursor(

Check failure on line 346 in lib/internal/streams/iter/broadcast.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Use object destructuring instead of array destructuring
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
const [minCursor] = getMinCursor(
const { 0: minCursor } = getMinCursor(

this.#consumers, this.#bufferStart + this.#buffer.length);
this.#cachedMinCursor = minCursor;
this.#minCursorDirty = false;
}

Expand Down
142 changes: 114 additions & 28 deletions lib/internal/streams/iter/share.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@
#cancelled = false;
#pulling = false;
#pullWaiters = [];
#cachedMinCursor = 0;
#cachedMinCursorConsumers = 0;

constructor(source, options) {
this.#source = source;
Expand Down Expand Up @@ -114,6 +116,14 @@
};

this.#consumers.add(state);
if (this.#consumers.size === 1) {
this.#cachedMinCursor = state.cursor;
this.#cachedMinCursorConsumers = 1;
} else if (state.cursor === this.#cachedMinCursor) {
this.#cachedMinCursorConsumers++;
} else {
this.#recomputeMinCursor();
}
const self = this;

return {
Expand All @@ -139,22 +149,26 @@

if (self.#cancelled) {
state.detached = true;
self.#consumers.delete(state);
self.#deleteConsumer(state);
return { __proto__: null, done: true, value: undefined };
}

// Check if data is available in buffer
const bufferIndex = state.cursor - self.#bufferStart;
if (bufferIndex < self.#buffer.length) {
const chunk = self.#buffer.get(bufferIndex);
const cursor = state.cursor;
state.cursor++;
self.#tryTrimBuffer();
if (cursor === self.#cachedMinCursor &&
--self.#cachedMinCursorConsumers === 0) {
self.#tryTrimBuffer();
}
return { __proto__: null, done: false, value: chunk };
}

if (self.#sourceExhausted) {
state.detached = true;
self.#consumers.delete(state);
self.#deleteConsumer(state);
if (self.#sourceError) throw self.#sourceError;
return { __proto__: null, done: true, value: undefined };
}
Expand All @@ -163,7 +177,7 @@
const canPull = await self.#waitForBufferSpace();
if (!canPull) {
state.detached = true;
self.#consumers.delete(state);
self.#deleteConsumer(state);
if (self.#sourceError) throw self.#sourceError;
return { __proto__: null, done: true, value: undefined };
}
Expand All @@ -176,17 +190,19 @@
state.detached = true;
state.resolve = null;
state.reject = null;
self.#consumers.delete(state);
self.#tryTrimBuffer();
if (self.#deleteConsumer(state)) {
self.#tryTrimBuffer();
}
return { __proto__: null, done: true, value: undefined };
},

async throw() {
state.detached = true;
state.resolve = null;
state.reject = null;
self.#consumers.delete(state);
self.#tryTrimBuffer();
if (self.#deleteConsumer(state)) {
self.#tryTrimBuffer();
}
return { __proto__: null, done: true, value: undefined };
},
};
Expand Down Expand Up @@ -254,9 +270,11 @@
this.#bufferStart++;
for (const consumer of this.#consumers) {
if (consumer.cursor < this.#bufferStart) {
this.#deleteConsumerFromMin(consumer);
consumer.cursor = this.#bufferStart;
}
}
this.#recomputeMinCursor();
return true;
case 'drop-newest':
return true;
Expand Down Expand Up @@ -324,18 +342,41 @@
}

#tryTrimBuffer() {
const minCursor = getMinCursor(
this.#consumers, this.#bufferStart + this.#buffer.length);
const trimCount = minCursor - this.#bufferStart;
if (this.#cachedMinCursorConsumers === 0) {
this.#recomputeMinCursor();
}
const trimCount = this.#cachedMinCursor - this.#bufferStart;
if (trimCount > 0) {
this.#buffer.trimFront(trimCount);
this.#bufferStart = minCursor;
this.#bufferStart = this.#cachedMinCursor;
for (let i = 0; i < this.#pullWaiters.length; i++) {
this.#pullWaiters[i]();
}
this.#pullWaiters = [];
}
}

#recomputeMinCursor() {
const [minCursor, minCursorConsumers] = getMinCursor(

Check failure on line 360 in lib/internal/streams/iter/share.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Use object destructuring instead of array destructuring
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
const [minCursor, minCursorConsumers] = getMinCursor(
const { 0: minCursor, 1: minCursorConsumers } = getMinCursor(

this.#consumers, this.#bufferStart + this.#buffer.length);
this.#cachedMinCursor = minCursor;
this.#cachedMinCursorConsumers = minCursorConsumers;
}

#deleteConsumerFromMin(consumer) {
if (consumer.cursor === this.#cachedMinCursor) {
this.#cachedMinCursorConsumers--;
}
}

#deleteConsumer(consumer) {
if (this.#consumers.delete(consumer)) {
const wasAtMin = consumer.cursor === this.#cachedMinCursor;
this.#deleteConsumerFromMin(consumer);
return wasAtMin && this.#cachedMinCursorConsumers === 0;
}
return false;
}
Comment on lines +366 to +379
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Deduplication:

Suggested change
#deleteConsumerFromMin(consumer) {
if (consumer.cursor === this.#cachedMinCursor) {
this.#cachedMinCursorConsumers--;
}
}
#deleteConsumer(consumer) {
if (this.#consumers.delete(consumer)) {
const wasAtMin = consumer.cursor === this.#cachedMinCursor;
this.#deleteConsumerFromMin(consumer);
return wasAtMin && this.#cachedMinCursorConsumers === 0;
}
return false;
}
#deleteConsumerFromMin(consumer) {
if (consumer.cursor === this.#cachedMinCursor) {
this.#cachedMinCursorConsumers--;
return true;
}
return false;
}
#deleteConsumer(consumer) {
if (this.#consumers.delete(consumer) &&
this.#deleteConsumerFromMin(consumer) &&
this.#cachedMinCursorConsumers === 0) {
return true;
}
return false;
}

}

// =============================================================================
Expand All @@ -352,6 +393,8 @@
#sourceExhausted = false;
#sourceError = null;
#cancelled = false;
#cachedMinCursor = 0;
#cachedMinCursorConsumers = 0;

constructor(source, options) {
this.#source = source;
Expand Down Expand Up @@ -383,6 +426,14 @@
};

this.#consumers.add(state);
if (this.#consumers.size === 1) {
this.#cachedMinCursor = state.cursor;
this.#cachedMinCursorConsumers = 1;
} else if (state.cursor === this.#cachedMinCursor) {
this.#cachedMinCursorConsumers++;
} else {
this.#recomputeMinCursor();
}
const self = this;

return {
Expand All @@ -396,26 +447,30 @@
}
if (self.#sourceError) {
state.detached = true;
self.#consumers.delete(state);
self.#deleteConsumer(state);
throw self.#sourceError;
}
if (self.#cancelled) {
state.detached = true;
self.#consumers.delete(state);
self.#deleteConsumer(state);
return { __proto__: null, done: true, value: undefined };
}

const bufferIndex = state.cursor - self.#bufferStart;
if (bufferIndex < self.#buffer.length) {
const chunk = self.#buffer.get(bufferIndex);
const cursor = state.cursor;
state.cursor++;
self.#tryTrimBuffer();
if (cursor === self.#cachedMinCursor &&
--self.#cachedMinCursorConsumers === 0) {
self.#tryTrimBuffer();
}
return { __proto__: null, done: false, value: chunk };
}

if (self.#sourceExhausted) {
state.detached = true;
self.#consumers.delete(state);
self.#deleteConsumer(state);
return { __proto__: null, done: true, value: undefined };
}

Expand All @@ -436,13 +491,15 @@
self.#bufferStart++;
for (const consumer of self.#consumers) {
if (consumer.cursor < self.#bufferStart) {
self.#deleteConsumerFromMin(consumer);
consumer.cursor = self.#bufferStart;
}
}
self.#recomputeMinCursor();
break;
case 'drop-newest':
state.detached = true;
self.#consumers.delete(state);
self.#deleteConsumer(state);
return { __proto__: null, done: true, value: undefined };
}
}
Expand All @@ -451,21 +508,25 @@

if (self.#sourceError) {
state.detached = true;
self.#consumers.delete(state);
self.#deleteConsumer(state);
throw self.#sourceError;
}

const newBufferIndex = state.cursor - self.#bufferStart;
if (newBufferIndex < self.#buffer.length) {
const chunk = self.#buffer.get(newBufferIndex);
const cursor = state.cursor;
state.cursor++;
self.#tryTrimBuffer();
if (cursor === self.#cachedMinCursor &&
--self.#cachedMinCursorConsumers === 0) {
self.#tryTrimBuffer();
}
return { __proto__: null, done: false, value: chunk };
}

if (self.#sourceExhausted) {
state.detached = true;
self.#consumers.delete(state);
self.#deleteConsumer(state);
return { __proto__: null, done: true, value: undefined };
}

Expand All @@ -474,15 +535,17 @@

return() {
state.detached = true;
self.#consumers.delete(state);
self.#tryTrimBuffer();
if (self.#deleteConsumer(state)) {
self.#tryTrimBuffer();
}
return { __proto__: null, done: true, value: undefined };
},

throw() {
state.detached = true;
self.#consumers.delete(state);
self.#tryTrimBuffer();
if (self.#deleteConsumer(state)) {
self.#tryTrimBuffer();
}
return { __proto__: null, done: true, value: undefined };
},
};
Expand Down Expand Up @@ -532,13 +595,36 @@
}

#tryTrimBuffer() {
const minCursor = getMinCursor(
this.#consumers, this.#bufferStart + this.#buffer.length);
const trimCount = minCursor - this.#bufferStart;
if (this.#cachedMinCursorConsumers === 0) {
this.#recomputeMinCursor();
}
const trimCount = this.#cachedMinCursor - this.#bufferStart;
if (trimCount > 0) {
this.#buffer.trimFront(trimCount);
this.#bufferStart = minCursor;
this.#bufferStart = this.#cachedMinCursor;
}
}

#recomputeMinCursor() {
const [minCursor, minCursorConsumers] = getMinCursor(

Check failure on line 609 in lib/internal/streams/iter/share.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Use object destructuring instead of array destructuring
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
const [minCursor, minCursorConsumers] = getMinCursor(
const { 0: minCursor, 1: minCursorConsumers } = getMinCursor(

this.#consumers, this.#bufferStart + this.#buffer.length);
this.#cachedMinCursor = minCursor;
this.#cachedMinCursorConsumers = minCursorConsumers;
}

#deleteConsumerFromMin(consumer) {
if (consumer.cursor === this.#cachedMinCursor) {
this.#cachedMinCursorConsumers--;
}
}

#deleteConsumer(consumer) {
if (this.#consumers.delete(consumer)) {
const wasAtMin = consumer.cursor === this.#cachedMinCursor;
this.#deleteConsumerFromMin(consumer);
return wasAtMin && this.#cachedMinCursorConsumers === 0;
}
return false;
}
}

Expand Down
20 changes: 12 additions & 8 deletions lib/internal/streams/iter/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,24 @@ function onSignalAbort(signal, handler) {
}

/**
* Compute the minimum cursor across a set of consumers.
* Returns fallback if the set is empty.
* Compute the minimum cursor across a set of consumers and count how many
* consumers are at that cursor.
* @param {Set} consumers - Set of objects with a `cursor` property
* @param {number} fallback - Value to return when set is empty
* @returns {number}
* @param {number} fallback - Cursor to return when set is empty
* @returns {[number, number]}
*/
function getMinCursor(consumers, fallback) {
let min = Infinity;
let minCursor = fallback;
let minCursorConsumers = 0;
for (const consumer of consumers) {
if (consumer.cursor < min) {
min = consumer.cursor;
if (consumer.cursor < minCursor) {
minCursor = consumer.cursor;
minCursorConsumers = 1;
} else if (consumer.cursor === minCursor) {
minCursorConsumers++;
}
}
return min === Infinity ? fallback : min;
return [minCursor, minCursorConsumers];
}

/**
Expand Down
Loading