Commit 8b13d34c01f for nodejs
commit 8b13d34c01fd0293c02c3ff3c57f4ef9da001f87
Author: James M Snell <jasnell@gmail.com>
Date: Wed Oct 7 01:57:35 2026 +0000
stream: do not retain a promise per read in stream/iter share()
share() read its source by racing the source's next() with a promise
that cancel() resolves, created once for the share. That promise does
not settle until the share is cancelled, so every race added reactions
to it that were kept for as long as the share was in use: about 720
bytes per batch read, including three promises, two promise reactions
and six closures. Sharing a source of 800,000 16-byte chunks between
two consumers kept 574 MiB alive after garbage collection by the end.
Instead, keep the resolver of the pending read in a field, and have
cancel() settle that read with the same result as before. A read still
settles as soon as the share is cancelled, without waiting for the
source.
What a share keeps alive no longer grows with the batches read: the
peak heap sharing 200,000 16-byte chunks between two consumers drops
from 180 MB to 9 MB, and the share is about 3 times faster.
Assisted-by: OpenCode
Signed-off-by: James M Snell <jasnell@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/66504
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
diff --git a/lib/internal/streams/iter/share.js b/lib/internal/streams/iter/share.js
index 937528b3929..484b923f025 100644
--- a/lib/internal/streams/iter/share.js
+++ b/lib/internal/streams/iter/share.js
@@ -12,7 +12,6 @@ const {
PromisePrototypeThen,
PromiseResolve,
PromiseWithResolvers,
- SafePromiseRace,
SafeSet,
Symbol,
SymbolAsyncIterator,
@@ -89,8 +88,9 @@ class ShareImpl {
#cancelled = false;
#pulling = false;
#pullWaiters = [];
- #cancelPromise;
- #resolveCancel;
+ // Settles the pending read of the source with kShareCancelled when the
+ // share is cancelled, or null if no read is pending.
+ #cancelPendingRead = null;
#cancelError = kNoShareError;
#cachedMinCursor = 0;
#cachedMinCursorConsumers = 0;
@@ -101,9 +101,6 @@ class ShareImpl {
constructor(source, options) {
this.#source = source;
this.#options = options;
- const { promise, resolve } = PromiseWithResolvers();
- this.#cancelPromise = promise;
- this.#resolveCancel = resolve;
}
get consumerCount() {
@@ -280,8 +277,11 @@ class ShareImpl {
this.#cancelError = reason;
}
- this.#resolveCancel(kShareCancelled);
- this.#resolveCancel = null;
+ const cancelPendingRead = this.#cancelPendingRead;
+ if (cancelPendingRead !== null) {
+ this.#cancelPendingRead = null;
+ cancelPendingRead(kShareCancelled);
+ }
try {
const returnMethod = this.#sourceIterator?.return;
@@ -416,10 +416,7 @@ class ShareImpl {
}
}
- const result = await SafePromiseRace([
- this.#sourceIterator.next(),
- this.#cancelPromise,
- ]);
+ const result = await this.#readSource();
if (this.#cancelled || result === kShareCancelled) return;
@@ -444,6 +441,27 @@ class ShareImpl {
})();
}
+ // Read the next result of the source, settling early with kShareCancelled
+ // if the share is cancelled first. Racing the read with a promise settled
+ // by cancel() would add reactions to that promise on every read, which
+ // would be kept until the share is cancelled or collected.
+ #readSource() {
+ const next = this.#sourceIterator.next();
+ const { promise, resolve, reject } = PromiseWithResolvers();
+ this.#cancelPendingRead = resolve;
+ PromisePrototypeThen(
+ PromiseResolve(next),
+ (result) => {
+ if (this.#cancelPendingRead === resolve) this.#cancelPendingRead = null;
+ resolve(result);
+ },
+ (error) => {
+ if (this.#cancelPendingRead === resolve) this.#cancelPendingRead = null;
+ reject(error);
+ });
+ return promise;
+ }
+
#bufferBatch(batch) {
const entry = createBatchEntry(batch);
// 'drop-oldest' evicts whole entries. A single pulled batch can be much
diff --git a/test/parallel/test-stream-iter-share-read-retention.js b/test/parallel/test-stream-iter-share-read-retention.js
new file mode 100644
index 00000000000..d95fee33642
--- /dev/null
+++ b/test/parallel/test-stream-iter-share-read-retention.js
@@ -0,0 +1,47 @@
+// Flags: --experimental-stream-iter --expose-gc --no-warnings
+'use strict';
+
+// Reading the source of a share() must not leave objects behind for every
+// read: what a share keeps alive must not grow with the number of batches
+// read from its source.
+
+const common = require('../common');
+const assert = require('assert');
+const { queryObjects } = require('v8');
+const { share } = require('stream/iter');
+
+async function test() {
+ const reads = 4000;
+ const chunk = new Uint8Array(1);
+ let i = 0;
+ const source = {
+ [Symbol.asyncIterator]() { return this; },
+ async next() {
+ return i++ < reads ?
+ { done: false, value: [chunk] } :
+ { done: true, value: undefined };
+ },
+ };
+ const shared = share(source, { backpressure: 'unbounded' });
+ const a = shared.pull()[Symbol.asyncIterator]();
+ const b = shared.pull()[Symbol.asyncIterator]();
+
+ const promises = [];
+ for (let n = 0; n < reads; n++) {
+ assert.strictEqual((await a.next()).done, false);
+ assert.strictEqual((await b.next()).done, false);
+ if (n === 1000 || n === 3000) {
+ globalThis.gc();
+ promises.push(queryObjects(Promise, { format: 'count' }));
+ }
+ }
+ assert.strictEqual((await a.next()).done, true);
+ assert.strictEqual((await b.next()).done, true);
+
+ // 2000 reads happened between the two counts. Retaining even one promise
+ // per read would add 2000.
+ const growth = promises[1] - promises[0];
+ assert.ok(growth < 200, `${growth} promises retained across 2000 reads`);
+}
+
+test().then(common.mustCall());