Skip to content

Commit 43c9e17

Browse files
committed
stream: fixup some perf bugs in stream/new
1 parent b9f54ae commit 43c9e17

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

lib/internal/streams/new/broadcast.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ const {
5050
pull: pullWithTransforms,
5151
} = require('internal/streams/new/pull');
5252

53+
const {
54+
allUint8Array,
55+
} = require('internal/streams/new/utils');
56+
5357
const encoder = new TextEncoder();
5458

5559
// Non-exported symbol for internal cancel notification from BroadcastImpl
@@ -400,8 +404,10 @@ class BroadcastWriter {
400404
throw new ERR_INVALID_STATE('Writer is closed');
401405
}
402406

403-
const converted = ArrayPrototypeMap(chunks, (c) =>
404-
(typeof c === 'string' ? encoder.encode(c) : c));
407+
const converted = allUint8Array(chunks)
408+
? ArrayPrototypeSlice(chunks)
409+
: ArrayPrototypeMap(chunks, (c) =>
410+
(typeof c === 'string' ? encoder.encode(c) : c));
405411

406412
if (this._broadcast._write(converted)) {
407413
for (let i = 0; i < converted.length; i++) {
@@ -441,8 +447,10 @@ class BroadcastWriter {
441447
writevSync(chunks) {
442448
if (this._closed || this._aborted) return false;
443449
if (!this._broadcast._canWrite()) return false;
444-
const converted = ArrayPrototypeMap(chunks, (c) =>
445-
(typeof c === 'string' ? encoder.encode(c) : c));
450+
const converted = allUint8Array(chunks)
451+
? ArrayPrototypeSlice(chunks)
452+
: ArrayPrototypeMap(chunks, (c) =>
453+
(typeof c === 'string' ? encoder.encode(c) : c));
446454
if (this._broadcast._write(converted)) {
447455
for (let i = 0; i < converted.length; i++) {
448456
this._totalBytes += converted[i].byteLength;

lib/internal/streams/new/push.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const {
3131

3232
const {
3333
toUint8Array,
34+
allUint8Array,
3435
} = require('internal/streams/new/utils');
3536

3637
const {
@@ -437,9 +438,14 @@ class PushWriter {
437438
}
438439

439440
async writev(chunks, options) {
440-
const bytes = [];
441-
for (let i = 0; i < chunks.length; i++) {
442-
ArrayPrototypePush(bytes, toUint8Array(chunks[i]));
441+
let bytes;
442+
if (allUint8Array(chunks)) {
443+
bytes = ArrayPrototypeSlice(chunks);
444+
} else {
445+
bytes = [];
446+
for (let i = 0; i < chunks.length; i++) {
447+
ArrayPrototypePush(bytes, toUint8Array(chunks[i]));
448+
}
443449
}
444450
await this._queue.writeAsync(bytes, options?.signal);
445451
}
@@ -452,9 +458,14 @@ class PushWriter {
452458

453459
writevSync(chunks) {
454460
if (!this._queue.canWriteSync()) return false;
455-
const bytes = [];
456-
for (let i = 0; i < chunks.length; i++) {
457-
ArrayPrototypePush(bytes, toUint8Array(chunks[i]));
461+
let bytes;
462+
if (allUint8Array(chunks)) {
463+
bytes = ArrayPrototypeSlice(chunks);
464+
} else {
465+
bytes = [];
466+
for (let i = 0; i < chunks.length; i++) {
467+
ArrayPrototypePush(bytes, toUint8Array(chunks[i]));
468+
}
458469
}
459470
return this._queue.writeSync(bytes);
460471
}

lib/internal/streams/new/utils.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ function toUint8Array(chunk) {
2626
return chunk;
2727
}
2828

29+
/**
30+
* Check if all chunks in an array are already Uint8Array (no strings).
31+
* Short-circuits on the first string found.
32+
* @param {Array<Uint8Array|string>} chunks
33+
* @returns {boolean}
34+
*/
35+
function allUint8Array(chunks) {
36+
for (let i = 0; i < chunks.length; i++) {
37+
if (typeof chunks[i] === 'string') return false;
38+
}
39+
return true;
40+
}
41+
2942
/**
3043
* Calculate total byte length of an array of chunks.
3144
* @param {Uint8Array[]} chunks
@@ -100,6 +113,7 @@ function parsePullArgs(args) {
100113

101114
module.exports = {
102115
toUint8Array,
116+
allUint8Array,
103117
concatBytes,
104118
isPullOptions,
105119
parsePullArgs,

0 commit comments

Comments
 (0)