Skip to content

Commit 6d34354

Browse files
jasnelladuh95
authored andcommitted
stream: append to arrays with indexed stores in stream/iter hot paths
ArrayPrototypePush() is listed among the primordials with known performance issues. Append with an indexed store instead where stream/iter collects chunks or batches: batching sources in from() and fromSync(), flattening transform output in pull() and pullSync(), the batches of push() and of Readable sources, the output of the zlib/iter transforms and the chunks collected by bytes() and similar consumers. Like ArrayPrototypePush(), an indexed store is unaffected by changes to Array.prototype.push and runs setters defined for indices on Array.prototype. bytes() is about 13% faster, pullSync() through a generator transform and push() about 6%, and the other paths up to 4%. Assisted-by: OpenCode Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #66504 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 62ff98d commit 6d34354

6 files changed

Lines changed: 43 additions & 50 deletions

File tree

‎lib/internal/streams/iter/classic.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,14 @@ async function normalizeBatch(raw) {
160160
for (let i = 0; i < raw.length; i++) {
161161
const value = raw[i];
162162
if (isUint8Array(value)) {
163-
ArrayPrototypePush(batch, value);
163+
batch[batch.length] = value;
164164
} else {
165165
// normalizeAsyncValue may await for async protocols (e.g.
166166
// toAsyncStreamable on yielded objects). Stream events during
167167
// the suspension are queued, not lost -- errors will surface
168168
// on the next loop iteration after this yield completes.
169169
for await (const normalized of normalizeAsyncValue(value)) {
170-
ArrayPrototypePush(batch, normalized);
170+
batch[batch.length] = normalized;
171171
}
172172
}
173173
}
@@ -229,7 +229,7 @@ function createBatchedAsyncIterator(stream, normalize) {
229229
stream._readableState?.length > 0) {
230230
const c = stream.read();
231231
if (c === null) break;
232-
ArrayPrototypePush(batch, c);
232+
batch[batch.length] = c;
233233
}
234234
if (normalize !== null) {
235235
const result = await normalize(batch);

‎lib/internal/streams/iter/from.js‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const {
99
ArrayBufferIsView,
1010
ArrayIsArray,
1111
ArrayPrototypeEvery,
12-
ArrayPrototypePush,
1312
ArrayPrototypeSlice,
1413
DataViewPrototypeGetBuffer,
1514
DataViewPrototypeGetByteLength,
@@ -347,7 +346,7 @@ function* normalizeSyncSource(source) {
347346
}
348347
// Fast path 2: value is a single Uint8Array (very common)
349348
if (isUint8Array(value)) {
350-
ArrayPrototypePush(batch, value);
349+
batch[batch.length] = value;
351350
if (batch.length === FROM_BATCH_SIZE) {
352351
yield batch;
353352
batch = [];
@@ -361,7 +360,7 @@ function* normalizeSyncSource(source) {
361360
}
362361
let valueBatch = [];
363362
for (const chunk of normalizeSyncValue(value)) {
364-
ArrayPrototypePush(valueBatch, chunk);
363+
valueBatch[valueBatch.length] = chunk;
365364
if (valueBatch.length === FROM_BATCH_SIZE) {
366365
yield valueBatch;
367366
valueBatch = [];
@@ -658,7 +657,7 @@ async function* normalizeAsyncSourceValue(value, context,
658657
}
659658
continue;
660659
}
661-
ArrayPrototypePush(batch, chunk);
660+
batch[batch.length] = chunk;
662661
if (batch.length === FROM_BATCH_SIZE) {
663662
yield batch;
664663
batch = [];
@@ -898,7 +897,7 @@ function* readSyncSource(source, context) {
898897
}
899898
// Fast path 2: value is a single Uint8Array (very common)
900899
if (isUint8Array(value)) {
901-
ArrayPrototypePush(batch, value);
900+
batch[batch.length] = value;
902901
if (batch.length === FROM_BATCH_SIZE) {
903902
yield batch;
904903
batch = [];

‎lib/internal/streams/iter/pull.js‎

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ function* processTransformResultSync(result) {
264264
const batch = [];
265265
for (const item of result) {
266266
for (const chunk of flattenTransformYieldSync(item)) {
267-
ArrayPrototypePush(batch, chunk);
267+
batch[batch.length] = chunk;
268268
}
269269
}
270270
if (batch.length > 0) {
@@ -290,28 +290,28 @@ function appendTransformResultSync(target, result) {
290290
}
291291
if (isUint8ArrayBatch(result)) {
292292
if (result.length > 0) {
293-
ArrayPrototypePush(target, result);
293+
target[target.length] = result;
294294
}
295295
return;
296296
}
297297
if (isUint8Array(result)) {
298-
ArrayPrototypePush(target, [result]);
298+
target[target.length] = [result];
299299
return;
300300
}
301301
if (typeof result === 'string') {
302-
ArrayPrototypePush(target, [toUint8Array(result)]);
302+
target[target.length] = [toUint8Array(result)];
303303
return;
304304
}
305305
if (isAnyArrayBuffer(result)) {
306-
ArrayPrototypePush(target, [new Uint8Array(result)]);
306+
target[target.length] = [new Uint8Array(result)];
307307
return;
308308
}
309309
if (ArrayBufferIsView(result)) {
310-
ArrayPrototypePush(target, [arrayBufferViewToUint8Array(result)]);
310+
target[target.length] = [arrayBufferViewToUint8Array(result)];
311311
return;
312312
}
313313
for (const batch of processTransformResultSync(result)) {
314-
ArrayPrototypePush(target, batch);
314+
target[target.length] = batch;
315315
}
316316
}
317317

@@ -360,11 +360,11 @@ async function* processTransformResultAsync(result) {
360360
const batch = [];
361361
for await (const item of result) {
362362
if (isUint8Array(item)) {
363-
ArrayPrototypePush(batch, item);
363+
batch[batch.length] = item;
364364
continue;
365365
}
366366
for await (const chunk of flattenTransformYieldAsync(item)) {
367-
ArrayPrototypePush(batch, chunk);
367+
batch[batch.length] = chunk;
368368
}
369369
}
370370
if (batch.length > 0) {
@@ -377,13 +377,13 @@ async function* processTransformResultAsync(result) {
377377
const batch = [];
378378
for (const item of result) {
379379
if (isUint8Array(item)) {
380-
ArrayPrototypePush(batch, item);
380+
batch[batch.length] = item;
381381
continue;
382382
}
383383
// Note: This iteration is synchronous, since async iterables
384384
// may not be nested within sync iterables.
385385
for (const chunk of flattenTransformYieldSync(item)) {
386-
ArrayPrototypePush(batch, chunk);
386+
batch[batch.length] = chunk;
387387
}
388388
}
389389
if (batch.length > 0) {
@@ -410,32 +410,32 @@ function appendTransformResultAsync(target, result) {
410410
}
411411
if (isUint8ArrayBatch(result)) {
412412
if (result.length > 0) {
413-
ArrayPrototypePush(target, result);
413+
target[target.length] = result;
414414
}
415415
return;
416416
}
417417
if (isUint8Array(result)) {
418-
ArrayPrototypePush(target, [result]);
418+
target[target.length] = [result];
419419
return;
420420
}
421421
if (typeof result === 'string') {
422-
ArrayPrototypePush(target, [toUint8Array(result)]);
422+
target[target.length] = [toUint8Array(result)];
423423
return;
424424
}
425425
if (isAnyArrayBuffer(result)) {
426-
ArrayPrototypePush(target, [new Uint8Array(result)]);
426+
target[target.length] = [new Uint8Array(result)];
427427
return;
428428
}
429429
if (ArrayBufferIsView(result)) {
430-
ArrayPrototypePush(target, [arrayBufferViewToUint8Array(result)]);
430+
target[target.length] = [arrayBufferViewToUint8Array(result)];
431431
return;
432432
}
433433
return appendTransformResultAsyncSlow(target, result);
434434
}
435435

436436
async function appendTransformResultAsyncSlow(target, result) {
437437
for await (const batch of processTransformResultAsync(result)) {
438-
ArrayPrototypePush(target, batch);
438+
target[target.length] = batch;
439439
}
440440
}
441441

@@ -533,7 +533,7 @@ function* applyStatefulSyncTransform(source, transform, receiver) {
533533
if (item === null) continue;
534534
const batch = [];
535535
for (const chunk of flattenTransformYieldSync(item)) {
536-
ArrayPrototypePush(batch, chunk);
536+
batch[batch.length] = chunk;
537537
}
538538
if (batch.length > 0) {
539539
yield batch;
@@ -968,7 +968,7 @@ async function* applyStatefulAsyncTransform(
968968
// Slow path: flatten arbitrary transform yield
969969
const batch = [];
970970
for await (const chunk of flattenTransformYieldAsync(item)) {
971-
ArrayPrototypePush(batch, chunk);
971+
batch[batch.length] = chunk;
972972
}
973973
if (batch.length > 0) {
974974
yield batch;

‎lib/internal/streams/iter/push.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ class PushQueue {
508508
for (let i = 0; i < this.#slots.length; i++) {
509509
const batch = validateBatchEntry(this.#slots.get(i));
510510
for (let j = 0; j < batch.length; j++) {
511-
ArrayPrototypePush(result, batch[j]);
511+
result[result.length] = batch[j];
512512
}
513513
}
514514
this.#slots.clear();

‎lib/internal/streams/iter/transform.js‎

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
const {
1212
ArrayPrototypeMap,
13-
ArrayPrototypePush,
1413
ArrayPrototypeSlice,
1514
FunctionPrototypeCall,
1615
MathMax,
@@ -333,18 +332,16 @@ function makeZlibTransform(createHandleFn, processFlag, finishFlag) {
333332
if (have > 0) {
334333
if (bufferExhausted && outOffset === 0) {
335334
// Entire buffer filled from start - yield directly, no copy.
336-
ArrayPrototypePush(pending, outBuf);
335+
pending[pending.length] = outBuf;
337336
} else if (bufferExhausted) {
338337
// Tail of buffer filled and buffer is being replaced -
339338
// subarray is safe since outBuf reference is overwritten below.
340-
ArrayPrototypePush(pending,
341-
outBuf.subarray(outOffset, outOffset + have));
339+
pending[pending.length] =
340+
outBuf.subarray(outOffset, outOffset + have);
342341
} else {
343342
// Partial fill, buffer will be reused - must copy.
344-
ArrayPrototypePush(pending,
345-
TypedArrayPrototypeSlice(outBuf,
346-
outOffset,
347-
outOffset + have));
343+
pending[pending.length] =
344+
TypedArrayPrototypeSlice(outBuf, outOffset, outOffset + have);
348345
}
349346
pendingBytes += have;
350347
outOffset += have;
@@ -642,17 +639,15 @@ function makeZlibTransformSync(createHandleFn, processFlag, finishFlag) {
642639
if (have > 0) {
643640
if (bufferExhausted && outOffset === 0) {
644641
// Entire buffer filled - yield directly, no copy.
645-
ArrayPrototypePush(pending, outBuf);
642+
pending[pending.length] = outBuf;
646643
} else if (bufferExhausted) {
647644
// Tail filled, buffer being replaced - subarray is safe.
648-
ArrayPrototypePush(pending,
649-
outBuf.subarray(outOffset, outOffset + have));
645+
pending[pending.length] =
646+
outBuf.subarray(outOffset, outOffset + have);
650647
} else {
651648
// Partial fill, buffer reused - must copy.
652-
ArrayPrototypePush(pending,
653-
TypedArrayPrototypeSlice(outBuf,
654-
outOffset,
655-
outOffset + have));
649+
pending[pending.length] =
650+
TypedArrayPrototypeSlice(outBuf, outOffset, outOffset + have);
656651
}
657652
pendingBytes += have;
658653
outOffset += have;

‎lib/internal/streams/iter/utils.js‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const {
55
ArrayBufferPrototypeGetByteLength,
66
ArrayBufferPrototypeGetDetached,
77
ArrayBufferPrototypeGetResizable,
8-
ArrayPrototypePush,
98
ArrayPrototypeSlice,
109
FunctionPrototypeCall,
1110
ObjectDefineProperty,
@@ -555,12 +554,12 @@ function validateBudget(budget) {
555554
function recordChunk(chunks, checks, value) {
556555
const buffer = TypedArrayPrototypeGetBuffer(value);
557556
const byteLength = TypedArrayPrototypeGetByteLength(value);
558-
ArrayPrototypePush(chunks, value);
557+
chunks[chunks.length] = value;
559558
if (byteLength === 0 || isSharedArrayBuffer(buffer) ||
560559
ArrayBufferPrototypeGetResizable(buffer)) {
561-
ArrayPrototypePush(checks, snapshotByteView(value));
560+
checks[checks.length] = snapshotByteView(value);
562561
} else {
563-
ArrayPrototypePush(checks, byteLength);
562+
checks[checks.length] = byteLength;
564563
}
565564
return byteLength;
566565
}
@@ -612,14 +611,14 @@ function splitBatchEntry(entry, limit) {
612611
for (let i = 0; i < views.length; i++) {
613612
const view = views[i];
614613
if (current.length > 0 && byteLength + view.byteLength >= limit) {
615-
ArrayPrototypePush(entries, new BatchEntry(current, byteLength));
614+
entries[entries.length] = new BatchEntry(current, byteLength);
616615
current = [];
617616
byteLength = 0;
618617
}
619-
ArrayPrototypePush(current, view);
618+
current[current.length] = view;
620619
byteLength += view.byteLength;
621620
}
622-
ArrayPrototypePush(entries, new BatchEntry(current, byteLength));
621+
entries[entries.length] = new BatchEntry(current, byteLength);
623622
return entries;
624623
}
625624

0 commit comments

Comments
 (0)