stream: avoid allocations on every stream/iter push() write

Every write() to a push() or broadcast() writer converted its options
with the WriteOptions dictionary converter to look for a signal, which
creates an empty dictionary when there are no options, and every write
replaced the array of pending drains, even when there were none.

Return no signal for undefined or null options without converting them,
and leave the array of pending drains alone when it is empty. Writing
16-byte chunks to a push() stream with await write() and reading them
is about 30% faster, and about 18% faster when piping them; 64 KiB
chunks are about 11% 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>
This commit is contained in:
James M Snell authored and Antoine du Hamel committed 2026-11-25 18:00:50 +01:00
1 parent 134e1241c2
commit dc3360eef3
3 files changed
+7

No files matched your search

+2
View File
@@ -839,6 +839,7 @@ class BroadcastWriter {
}
#resolvePendingDrains(canWrite) {
if (this.#pendingDrains.length === 0) return;
const drains = this.#pendingDrains;
this.#pendingDrains = [];
for (let i = 0; i < drains.length; i++) {
@@ -847,6 +848,7 @@ class BroadcastWriter {
}
#rejectPendingDrains(error) {
if (this.#pendingDrains.length === 0) return;
const drains = this.#pendingDrains;
this.#pendingDrains = [];
for (let i = 0; i < drains.length; i++) {
+2
View File
@@ -594,6 +594,7 @@ class PushQueue {
}
#resolvePendingDrains(canWrite) {
if (this.#pendingDrains.length === 0) return;
const drains = this.#pendingDrains;
this.#pendingDrains = [];
for (let i = 0; i < drains.length; i++) {
@@ -602,6 +603,7 @@ class PushQueue {
}
#rejectPendingDrains(error) {
if (this.#pendingDrains.length === 0) return;
const drains = this.#pendingDrains;
this.#pendingDrains = [];
for (let i = 0; i < drains.length; i++) {
+3
View File
@@ -707,6 +707,9 @@ function convertChunks(chunks) {
* @returns {AbortSignal|undefined}
*/
function getWriterSignal(options) {
// Writes are hot, and converting undefined or null creates an empty
// dictionary, which has no signal.
if (options === undefined || options === null) return undefined;
return converters.WriteOptions(options, kWriteOptionsContext).signal;
}