@@ -1651,62 +1651,82 @@ function fromSync(input) { |
1651 | 1651 | }; |
1652 | 1652 | } |
1653 | 1653 |
|
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; |
1673 | 1661 |
|
1674 | | -function createBatchIterator(batch) { |
1675 | | - let index = 0; |
| 1662 | + constructor(batch) { |
| 1663 | + this.#batch = batch; |
| 1664 | + } |
1676 | 1665 |
|
1677 | 1666 | // The next batch, or null once done. |
1678 | | - function nextBatch() { |
| 1667 | + #nextBatch() { |
| 1668 | + const batch = this.#batch; |
| 1669 | + const index = this.#index; |
1679 | 1670 | if (index >= batch.length) { |
1680 | | - index = batch.length; |
| 1671 | + this.#index = batch.length; |
1681 | 1672 | return null; |
1682 | 1673 | } |
1683 | 1674 | if (index === 0 && batch.length <= FROM_BATCH_SIZE) { |
1684 | | - index = batch.length; |
| 1675 | + this.#index = batch.length; |
1685 | 1676 | return batch; |
1686 | 1677 | } |
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); |
1690 | 1680 | } |
1691 | 1681 |
|
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); |
1710 | 1730 | } |
1711 | 1731 |
|
1712 | 1732 | /** |
|