Skip to content

Commit ba74ca5

Browse files
jasnelladuh95
authored andcommitted
stream: create stream/iter from() value streams as class instances
from() of a value that needs no normalization (a string, a buffer, a Uint8Array[]) returned an object literal with a null prototype, which V8 creates in dictionary mode, and each of its iterators was an object literal passed to ObjectSetPrototypeOf(), a runtime call. Together they were three quarters of the time to create and read such a stream. Use two classes, BatchSource and BatchIterator, whose prototypes do not inherit from Object.prototype either, as before. Creating a stream from a 16-byte chunk and reading it is about 6.5 times faster, and from a one-chunk array about 5.5 times faster. 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 ccefb83 commit ba74ca5

1 file changed

Lines changed: 65 additions & 45 deletions

File tree

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

Lines changed: 65 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1651,62 +1651,82 @@ function fromSync(input) {
16511651
};
16521652
}
16531653

1654-
/**
1655-
* An async iterable yielding the chunks of `batch`, a Uint8Array[], in
1656-
* batches of at most FROM_BATCH_SIZE chunks, for from() of values that need
1657-
* no normalization. Each iterator does what an async generator yielding the
1658-
* batches would, without the generator object and the promises and frames
1659-
* of resuming it: next() gives the batches, then done; return() and throw()
1660-
* end the iteration, throw() rejecting with its argument.
1661-
* @param {Uint8Array[]} batch
1662-
* @returns {AsyncIterable<Uint8Array[]>}
1663-
*/
1664-
function createBatchSource(batch) {
1665-
return {
1666-
__proto__: null,
1667-
[SymbolAsyncIterator]() {
1668-
return createBatchIterator(batch);
1669-
},
1670-
[kValidatedSource]: true,
1671-
};
1672-
}
1654+
// BatchSource and BatchIterator are classes, rather than object literals
1655+
// with a null prototype, which V8 creates in dictionary mode: from() of a
1656+
// value is often called once per stream. Their prototypes do not inherit
1657+
// from Object.prototype either.
1658+
class BatchIterator {
1659+
#batch;
1660+
#index = 0;
16731661

1674-
function createBatchIterator(batch) {
1675-
let index = 0;
1662+
constructor(batch) {
1663+
this.#batch = batch;
1664+
}
16761665

16771666
// The next batch, or null once done.
1678-
function nextBatch() {
1667+
#nextBatch() {
1668+
const batch = this.#batch;
1669+
const index = this.#index;
16791670
if (index >= batch.length) {
1680-
index = batch.length;
1671+
this.#index = batch.length;
16811672
return null;
16821673
}
16831674
if (index === 0 && batch.length <= FROM_BATCH_SIZE) {
1684-
index = batch.length;
1675+
this.#index = batch.length;
16851676
return batch;
16861677
}
1687-
const start = index;
1688-
index += FROM_BATCH_SIZE;
1689-
return ArrayPrototypeSlice(batch, start, index);
1678+
this.#index = index + FROM_BATCH_SIZE;
1679+
return ArrayPrototypeSlice(batch, index, index + FROM_BATCH_SIZE);
16901680
}
16911681

1692-
return ObjectSetPrototypeOf({
1693-
next() {
1694-
const value = nextBatch();
1695-
return PromiseResolve(value === null ?
1696-
new IterResult(true, undefined) : new IterResult(false, value));
1697-
},
1698-
return(value) {
1699-
index = batch.length;
1700-
return PromiseResolve(new IterResult(true, value));
1701-
},
1702-
throw(error) {
1703-
index = batch.length;
1704-
return PromiseReject(error);
1705-
},
1706-
[SymbolAsyncIterator]() {
1707-
return this;
1708-
},
1709-
}, null);
1682+
next() {
1683+
const value = this.#nextBatch();
1684+
return PromiseResolve(value === null ?
1685+
new IterResult(true, undefined) : new IterResult(false, value));
1686+
}
1687+
1688+
return(value) {
1689+
this.#index = this.#batch.length;
1690+
return PromiseResolve(new IterResult(true, value));
1691+
}
1692+
1693+
throw(error) {
1694+
this.#index = this.#batch.length;
1695+
return PromiseReject(error);
1696+
}
1697+
1698+
[SymbolAsyncIterator]() {
1699+
return this;
1700+
}
1701+
}
1702+
ObjectSetPrototypeOf(BatchIterator.prototype, null);
1703+
1704+
class BatchSource {
1705+
#batch;
1706+
1707+
constructor(batch) {
1708+
this.#batch = batch;
1709+
}
1710+
1711+
[SymbolAsyncIterator]() {
1712+
return new BatchIterator(this.#batch);
1713+
}
1714+
}
1715+
BatchSource.prototype[kValidatedSource] = true;
1716+
ObjectSetPrototypeOf(BatchSource.prototype, null);
1717+
1718+
/**
1719+
* An async iterable yielding the chunks of `batch`, a Uint8Array[], in
1720+
* batches of at most FROM_BATCH_SIZE chunks, for from() of values that need
1721+
* no normalization. Each iterator does what an async generator yielding the
1722+
* batches would, without the generator object and the promises and frames
1723+
* of resuming it: next() gives the batches, then done; return() and throw()
1724+
* end the iteration, throw() rejecting with its argument.
1725+
* @param {Uint8Array[]} batch
1726+
* @returns {AsyncIterable<Uint8Array[]>}
1727+
*/
1728+
function createBatchSource(batch) {
1729+
return new BatchSource(batch);
17101730
}
17111731

17121732
/**

0 commit comments

Comments
 (0)