mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
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: 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:
1 parent
ccefb83dfe
commit
ba74ca5c5b
1 file changed
+65
-45
@@ -1651,6 +1651,70 @@ function fromSync(input) {
|
||||
};
|
||||
}
|
||||
|
||||
// BatchSource and BatchIterator are classes, rather than object literals
|
||||
// with a null prototype, which V8 creates in dictionary mode: from() of a
|
||||
// value is often called once per stream. Their prototypes do not inherit
|
||||
// from Object.prototype either.
|
||||
class BatchIterator {
|
||||
#batch;
|
||||
#index = 0;
|
||||
|
||||
constructor(batch) {
|
||||
this.#batch = batch;
|
||||
}
|
||||
|
||||
// The next batch, or null once done.
|
||||
#nextBatch() {
|
||||
const batch = this.#batch;
|
||||
const index = this.#index;
|
||||
if (index >= batch.length) {
|
||||
this.#index = batch.length;
|
||||
return null;
|
||||
}
|
||||
if (index === 0 && batch.length <= FROM_BATCH_SIZE) {
|
||||
this.#index = batch.length;
|
||||
return batch;
|
||||
}
|
||||
this.#index = index + FROM_BATCH_SIZE;
|
||||
return ArrayPrototypeSlice(batch, index, index + FROM_BATCH_SIZE);
|
||||
}
|
||||
|
||||
next() {
|
||||
const value = this.#nextBatch();
|
||||
return PromiseResolve(value === null ?
|
||||
new IterResult(true, undefined) : new IterResult(false, value));
|
||||
}
|
||||
|
||||
return(value) {
|
||||
this.#index = this.#batch.length;
|
||||
return PromiseResolve(new IterResult(true, value));
|
||||
}
|
||||
|
||||
throw(error) {
|
||||
this.#index = this.#batch.length;
|
||||
return PromiseReject(error);
|
||||
}
|
||||
|
||||
[SymbolAsyncIterator]() {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
ObjectSetPrototypeOf(BatchIterator.prototype, null);
|
||||
|
||||
class BatchSource {
|
||||
#batch;
|
||||
|
||||
constructor(batch) {
|
||||
this.#batch = batch;
|
||||
}
|
||||
|
||||
[SymbolAsyncIterator]() {
|
||||
return new BatchIterator(this.#batch);
|
||||
}
|
||||
}
|
||||
BatchSource.prototype[kValidatedSource] = true;
|
||||
ObjectSetPrototypeOf(BatchSource.prototype, null);
|
||||
|
||||
/**
|
||||
* An async iterable yielding the chunks of `batch`, a Uint8Array[], in
|
||||
* batches of at most FROM_BATCH_SIZE chunks, for from() of values that need
|
||||
@@ -1662,51 +1726,7 @@ function fromSync(input) {
|
||||
* @returns {AsyncIterable<Uint8Array[]>}
|
||||
*/
|
||||
function createBatchSource(batch) {
|
||||
return {
|
||||
__proto__: null,
|
||||
[SymbolAsyncIterator]() {
|
||||
return createBatchIterator(batch);
|
||||
},
|
||||
[kValidatedSource]: true,
|
||||
};
|
||||
}
|
||||
|
||||
function createBatchIterator(batch) {
|
||||
let index = 0;
|
||||
|
||||
// The next batch, or null once done.
|
||||
function nextBatch() {
|
||||
if (index >= batch.length) {
|
||||
index = batch.length;
|
||||
return null;
|
||||
}
|
||||
if (index === 0 && batch.length <= FROM_BATCH_SIZE) {
|
||||
index = batch.length;
|
||||
return batch;
|
||||
}
|
||||
const start = index;
|
||||
index += FROM_BATCH_SIZE;
|
||||
return ArrayPrototypeSlice(batch, start, index);
|
||||
}
|
||||
|
||||
return ObjectSetPrototypeOf({
|
||||
next() {
|
||||
const value = nextBatch();
|
||||
return PromiseResolve(value === null ?
|
||||
new IterResult(true, undefined) : new IterResult(false, value));
|
||||
},
|
||||
return(value) {
|
||||
index = batch.length;
|
||||
return PromiseResolve(new IterResult(true, value));
|
||||
},
|
||||
throw(error) {
|
||||
index = batch.length;
|
||||
return PromiseReject(error);
|
||||
},
|
||||
[SymbolAsyncIterator]() {
|
||||
return this;
|
||||
},
|
||||
}, null);
|
||||
return new BatchSource(batch);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user