Commit b2c5fd1f5f2 for nodejs

commit b2c5fd1f5f28a8984faf870e0d5c8ad401cdadc3
Author: James M Snell <jasnell@gmail.com>
Date:   Wed Oct 7 10:32:53 2026 +0000

    stream: read stream/iter share() consumers without a chain per read

    Every next() of a share() consumer chained on the consumer's previous
    next() and ran an async function, also when the batch was already
    buffered, as it is for every consumer but the first to read it. Every
    read of the source ran an async function, and each consumer waiting
    for it queued a promise of its own. Recomputing the slowest consumer's
    cursor, once per batch with several consumers, allocated an object.

    A next() that finds its batch buffered, or the end, now settles at
    once, without waiting for anything. Only a next() that waits for the
    source is waited for by the next one; when there is room in the buffer
    it waits for the read with one reaction, without an async function.
    Consumers waiting for the same read of the source share one promise,
    which a cancellation resolves at once, and the read is handled by
    functions created once per share. getMinCursor() reuses its result.

    With two consumers of a share of 16-byte chunks one per batch, reading
    a sync generator is about 1.8 times faster, and an async generator about
    1.7 times faster, with 60% less allocated per chunk.

    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 484b923f025..ba457e9023c 100644
--- a/lib/internal/streams/iter/share.js
+++ b/lib/internal/streams/iter/share.js
@@ -10,6 +10,7 @@ const {
   FunctionPrototypeCall,
   ObjectSetPrototypeOf,
   PromisePrototypeThen,
+  PromiseReject,
   PromiseResolve,
   PromiseWithResolvers,
   SafeSet,
@@ -73,7 +74,8 @@ const { markPromiseAsHandled } = internalBinding('util');
 // =============================================================================

 const kNoShareError = Symbol('kNoShareError');
-const kShareCancelled = Symbol('kShareCancelled');
+// Returned by #tryRead() when the consumer must wait for the source.
+const kPull = Symbol('kPull');
 const kSetFactorySignal = Symbol('kSetFactorySignal');

 class ShareImpl {
@@ -88,9 +90,15 @@ class ShareImpl {
   #cancelled = false;
   #pulling = false;
   #pullWaiters = [];
-  // Settles the pending read of the source with kShareCancelled when the
-  // share is cancelled, or null if no read is pending.
-  #cancelPendingRead = null;
+  // While the source is read: the promise that every consumer waiting for
+  // the read waits for, the function resolving it, and whether the batch
+  // read is to be discarded ('drop-newest'). A cancellation resolves it at
+  // once: 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.
+  #pullDone = null;
+  #resolvePull = null;
+  #pullDiscard = false;
   #cancelError = kNoShareError;
   #cachedMinCursor = 0;
   #cachedMinCursorConsumers = 0;
@@ -148,7 +156,14 @@ class ShareImpl {
       reject: null,
       detached: false,
       error: kNoShareError,
-      pendingNext: PromiseResolve(),
+      // Set by #readAsync() for next(): the read waits for the source.
+      waiting: false,
+      // Created by #readAfterPull() on first use.
+      afterPull: undefined,
+      // The last pending next() of the consumer, if it is waiting for the
+      // source: the next one waits for it, as next() calls of an async
+      // generator do.
+      pendingNext: null,
     }, null);

     this.#consumers.add(state);
@@ -165,82 +180,39 @@ class ShareImpl {
     return {
       __proto__: null,
       [SymbolAsyncIterator]() {
-        const getNext = async () => {
-          // Loop until we get data, source is exhausted, or
-          // consumer is detached. Multiple consumers may be woken
-          // after a single pull - those that find no data at their
-          // cursor must re-pull rather than terminating prematurely.
-          for (;;) {
-            if (state.detached) {
-              if (state.error !== kNoShareError) throw state.error;
-              return new IterResult(true, undefined);
-            }
-
-            if (self.#cancelled) {
-              state.detached = true;
-              state.error = self.#cancelError;
-              self.#deleteConsumer(state);
-              if (state.error !== kNoShareError) throw state.error;
-              return new IterResult(true, undefined);
-            }
-
-            // Check if data is available in buffer
-            const bufferIndex = state.cursor - self.#bufferStart;
-            if (bufferIndex < self.#buffer.length) {
-              const chunk = self.#readEntry(self.#buffer.get(bufferIndex));
-              const cursor = state.cursor;
-              state.cursor++;
-              if (cursor === self.#cachedMinCursor &&
-                  --self.#cachedMinCursorConsumers === 0) {
-                self.#tryTrimBuffer();
-              }
-              return new IterResult(false, chunk);
-            }
-
-            if (self.#sourceExhausted) {
-              state.detached = true;
-              self.#deleteConsumer(state);
-              if (self.#sourceError !== kNoShareError) {
-                state.error = self.#sourceError;
-                throw state.error;
-              }
-              return new IterResult(true, undefined);
-            }
-
-            // Need to pull from source - check buffer limit
-            let shouldBuffer;
-            try {
-              shouldBuffer = await self.#waitForBufferSpace();
-            } catch (error) {
-              state.detached = true;
-              if (self.#deleteConsumer(state)) {
-                self.#tryTrimBuffer();
-              }
-              throw error;
-            }
-            if (shouldBuffer === null) {
-              state.detached = true;
-              state.error = self.#cancelError;
-              self.#deleteConsumer(state);
-              if (state.error !== kNoShareError) throw state.error;
-              return new IterResult(true, undefined);
-            }
-
-            await self.#pullFromSource(!shouldBuffer);
-            if (!shouldBuffer) {
-              await self.#waitForBufferSpaceAfterDrop();
-            }
+        // A read that finds data, or the end, settles at once; one that
+        // waits for the source becomes pendingNext.
+        const read = () => {
+          let result;
+          try {
+            result = self.#tryRead(state);
+          } catch (error) {
+            return PromiseReject(error);
           }
+          if (result !== kPull) return PromiseResolve(result);
+          return self.#readAsync(state);
+        };
+        const clearPending = (pending) => {
+          const onSettled = () => {
+            if (state.pendingNext === pending) state.pendingNext = null;
+          };
+          PromisePrototypeThen(pending, onSettled, onSettled);
         };

         return ObjectSetPrototypeOf({
           next() {
-            const next = PromisePrototypeThen(
-              state.pendingNext,
-              getNext,
-              getNext);
+            let next;
+            if (state.pendingNext !== null) {
+              next = PromisePrototypeThen(state.pendingNext, read, read);
+            } else {
+              state.waiting = false;
+              next = read();
+              // Settled at once: nothing for a later next() to wait for.
+              if (!state.waiting) return next;
+            }
             state.pendingNext = next;
-            markPromiseAsHandled(state.pendingNext);
+            markPromiseAsHandled(next);
+            clearPending(next);
             return next;
           },

@@ -277,11 +249,7 @@ class ShareImpl {
       this.#cancelError = reason;
     }

-    const cancelPendingRead = this.#cancelPendingRead;
-    if (cancelPendingRead !== null) {
-      this.#cancelPendingRead = null;
-      cancelPendingRead(kShareCancelled);
-    }
+    if (this.#resolvePull !== null) this.#finishPull();

     try {
       const returnMethod = this.#sourceIterator?.return;
@@ -327,6 +295,105 @@ class ShareImpl {

   // Internal methods

+  // Read the consumer's next batch if it is buffered, or the end: returns
+  // its result, or kPull if the source must be read first.
+  #tryRead(state) {
+    if (state.detached) {
+      if (state.error !== kNoShareError) throw state.error;
+      return new IterResult(true, undefined);
+    }
+
+    if (this.#cancelled) {
+      state.detached = true;
+      state.error = this.#cancelError;
+      this.#deleteConsumer(state);
+      if (state.error !== kNoShareError) throw state.error;
+      return new IterResult(true, undefined);
+    }
+
+    // Check if data is available in buffer
+    const bufferIndex = state.cursor - this.#bufferStart;
+    if (bufferIndex < this.#buffer.length) {
+      const chunk = this.#readEntry(this.#buffer.get(bufferIndex));
+      const cursor = state.cursor;
+      state.cursor++;
+      if (cursor === this.#cachedMinCursor &&
+          --this.#cachedMinCursorConsumers === 0) {
+        this.#tryTrimBuffer();
+      }
+      return new IterResult(false, chunk);
+    }
+
+    if (this.#sourceExhausted) {
+      state.detached = true;
+      this.#deleteConsumer(state);
+      if (this.#sourceError !== kNoShareError) {
+        state.error = this.#sourceError;
+        throw state.error;
+      }
+      return new IterResult(true, undefined);
+    }
+    return kPull;
+  }
+
+  // Read the consumer's next batch once the source has been read. Loops
+  // until it gets data, the source is exhausted, or the consumer is
+  // detached: multiple consumers may be woken after a single pull, and
+  // those that find no data at their cursor must pull again rather than
+  // terminate prematurely.
+  #readAsync(state) {
+    state.waiting = true;
+    if (this.#bufferedBytes < this.#options.budget) {
+      return this.#readAfterPull(state);
+    }
+    return this.#readAsyncLoop(state);
+  }
+
+  async #readAsyncLoop(state) {
+    for (;;) {
+      // Need to pull from source - check buffer limit
+      let shouldBuffer;
+      if (this.#bufferedBytes < this.#options.budget) {
+        shouldBuffer = true;
+      } else {
+        try {
+          shouldBuffer = await this.#waitForBufferSpace();
+        } catch (error) {
+          state.detached = true;
+          if (this.#deleteConsumer(state)) {
+            this.#tryTrimBuffer();
+          }
+          throw error;
+        }
+      }
+      if (shouldBuffer === null) {
+        state.detached = true;
+        state.error = this.#cancelError;
+        this.#deleteConsumer(state);
+        if (state.error !== kNoShareError) throw state.error;
+        return new IterResult(true, undefined);
+      }
+
+      await this.#pullFromSource(!shouldBuffer);
+      if (!shouldBuffer) {
+        await this.#waitForBufferSpaceAfterDrop();
+      }
+
+      const result = this.#tryRead(state);
+      if (result !== kPull) return result;
+    }
+  }
+
+  // The common case of #readAsyncLoop(), without an async function: there
+  // is room in the buffer, so read the source and then the consumer's batch.
+  #readAfterPull(state) {
+    state.afterPull ??= () => {
+      const result = this.#tryRead(state);
+      return result !== kPull ? result : this.#readAsyncLoop(state);
+    };
+    return PromisePrototypeThen(this.#pullFromSource(false), state.afterPull);
+  }
+
   async #waitForBufferSpace() {
     while (this.#bufferedBytes >= this.#options.budget) {
       if (this.#cancelled ||
@@ -384,82 +451,85 @@ class ShareImpl {
       return PromiseResolve();
     }

-    if (this.#pulling) {
-      const { promise, resolve } = PromiseWithResolvers();
-      ArrayPrototypePush(this.#pullWaiters, resolve);
-      return promise;
-    }
+    if (this.#pulling) return this.#pullDone;

     this.#pulling = true;
+    const { promise, resolve } = PromiseWithResolvers();
+    this.#pullDone = promise;
+    this.#resolvePull = resolve;
+    this.#pullDiscard = discard;

-    return (async () => {
-      try {
-        if (!this.#sourceIterator) {
-          if (isAsyncIterable(this.#source)) {
-            this.#sourceIterator =
-              this.#source[SymbolAsyncIterator]();
-          } else if (isSyncIterable(this.#source)) {
-            const syncIterator =
-              this.#source[SymbolIterator]();
-            this.#sourceIterator = ObjectSetPrototypeOf({
-              async next() {
-                return syncIterator.next();
-              },
-              async return() {
-                return syncIterator.return?.() ??
-                       new IterResult(true, undefined);
-              },
-            }, null);
-          } else {
-            throw new ERR_INVALID_ARG_TYPE(
-              'source', ['AsyncIterable', 'Iterable'], this.#source);
-          }
+    let next;
+    try {
+      if (!this.#sourceIterator) {
+        if (isAsyncIterable(this.#source)) {
+          this.#sourceIterator =
+            this.#source[SymbolAsyncIterator]();
+        } else if (isSyncIterable(this.#source)) {
+          const syncIterator =
+            this.#source[SymbolIterator]();
+          this.#sourceIterator = ObjectSetPrototypeOf({
+            // Its result is passed to PromiseResolve().
+            next() {
+              return syncIterator.next();
+            },
+            async return() {
+              return syncIterator.return?.() ??
+                     new IterResult(true, undefined);
+            },
+          }, null);
+        } else {
+          throw new ERR_INVALID_ARG_TYPE(
+            'source', ['AsyncIterable', 'Iterable'], this.#source);
         }
+      }
+      next = this.#sourceIterator.next();
+    } catch (error) {
+      this.#onReadError(error);
+      return promise;
+    }
+    PromisePrototypeThen(PromiseResolve(next), this.#onReadResult,
+                         this.#onReadError);
+    return promise;
+  }

-        const result = await this.#readSource();
-
-        if (this.#cancelled || result === kShareCancelled) return;
-
-        if (result.done) {
-          this.#sourceExhausted = true;
-        } else if (!discard) {
-          this.#bufferBatch(result.value);
-        }
-      } catch (error) {
-        this.#sourceError = error;
+  // The handlers of a read of the source, created once per share. A read
+  // ended by a cancellation is ignored.
+  #onReadResult = (result) => {
+    if (this.#resolvePull === null || this.#cancelled) return;
+    try {
+      if (result.done) {
         this.#sourceExhausted = true;
-      } finally {
-        if (this.#sourceExhausted && this.#consumers.size === 0) {
-          this.#cleanupFactorySignal();
-        }
-        this.#pulling = false;
-        for (let i = 0; i < this.#pullWaiters.length; i++) {
-          this.#pullWaiters[i]();
-        }
-        this.#pullWaiters = [];
+      } else if (!this.#pullDiscard) {
+        this.#bufferBatch(result.value);
       }
-    })();
-  }
-
-  // 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;
+    } catch (error) {
+      this.#sourceError = error;
+      this.#sourceExhausted = true;
+    }
+    this.#finishPull();
+  };
+
+  #onReadError = (error) => {
+    if (this.#resolvePull === null || this.#cancelled) return;
+    this.#sourceError = error;
+    this.#sourceExhausted = true;
+    this.#finishPull();
+  };
+
+  #finishPull() {
+    const resolve = this.#resolvePull;
+    this.#resolvePull = null;
+    this.#pullDone = null;
+    if (this.#sourceExhausted && this.#consumers.size === 0) {
+      this.#cleanupFactorySignal();
+    }
+    this.#pulling = false;
+    for (let i = 0; i < this.#pullWaiters.length; i++) {
+      this.#pullWaiters[i]();
+    }
+    this.#pullWaiters = [];
+    resolve();
   }

   #bufferBatch(batch) {
diff --git a/lib/internal/streams/iter/utils.js b/lib/internal/streams/iter/utils.js
index 9a94b0db2b6..7dd6a3fe172 100644
--- a/lib/internal/streams/iter/utils.js
+++ b/lib/internal/streams/iter/utils.js
@@ -468,6 +468,11 @@ function createAbortableIterator(source, signal) {
  * @param {number} fallback - Cursor to return when set is empty
  * @returns {{ minCursor: number, minCursorConsumers: number }}
  */
+// The result of getMinCursor(), reused: it runs whenever the slowest
+// consumer of a share or broadcast advances, which can be once per batch,
+// and its callers read the result at once.
+const minCursorResult = { __proto__: null, minCursor: 0, minCursorConsumers: 0 };
+
 function getMinCursor(consumers, fallback) {
   let minCursor = fallback;
   let minCursorConsumers = 0;
@@ -479,7 +484,9 @@ function getMinCursor(consumers, fallback) {
       minCursorConsumers++;
     }
   }
-  return { __proto__: null, minCursor, minCursorConsumers };
+  minCursorResult.minCursor = minCursor;
+  minCursorResult.minCursorConsumers = minCursorConsumers;
+  return minCursorResult;
 }

 /**