@@ -42,8 +42,10 @@ const { |
42 | 42 | convertChunks, |
43 | 43 | getWriterSignal, |
44 | 44 | parsePullArgs, |
| 45 | + snapshotByteView, |
45 | 46 | validateBatchEntry, |
46 | 47 | validateBudget, |
| 48 | + validateByteView, |
47 | 49 | newPendingPromise, |
48 | 50 | takeReject, |
49 | 51 | takeResolve, |
@@ -193,11 +195,13 @@ class PushQueue { |
193 | 195 | * Returns true if write completed, false if buffer is full. |
194 | 196 | * @returns {boolean} |
195 | 197 | */ |
196 | | - writeSync(chunks) { |
| 198 | + // `entry` is a BatchEntry, or for a single chunk a FixedByteView (see |
| 199 | + // entryOfChunk()). |
| 200 | + writeSync(entry) { |
197 | 201 | if (this.#writerState !== 'open') return false; |
198 | 202 | if (this.#consumerState !== 'active') return false; |
199 | 203 |
|
200 | | - return this.#writeEntry(createBatchEntry(chunks)); |
| 204 | + return this.#writeEntry(entry); |
201 | 205 | } |
202 | 206 |
|
203 | 207 | #writeEntry(entry) { |
@@ -249,7 +253,7 @@ class PushQueue { |
249 | 253 | * failure. |
250 | 254 | * @returns {Promise<void>} |
251 | 255 | */ |
252 | | - async writeAsync(chunks, signal) { |
| 256 | + async writeAsync(entry, signal) { |
253 | 257 | // Check writer state before signal (spec order: state, then signal) |
254 | 258 | if (this.#writerState === 'closed') { |
255 | 259 | throw new ERR_INVALID_STATE.TypeError('Writer is closed'); |
@@ -268,7 +272,6 @@ class PushQueue { |
268 | 272 | // Check for pre-aborted signal (after state checks per spec) |
269 | 273 | signal?.throwIfAborted(); |
270 | 274 |
|
271 | | - const entry = createBatchEntry(chunks); |
272 | 275 | if (this.#writeEntry(entry)) { |
273 | 276 | return; |
274 | 277 | } |
@@ -509,16 +512,24 @@ class PushQueue { |
509 | 512 | #drain() { |
510 | 513 | try { |
511 | 514 | if (this.#slots.length === 1) { |
512 | | - const result = validateBatchEntry(this.#slots.shift()); |
| 515 | + const entry = this.#slots.shift(); |
| 516 | + const result = entry.views === undefined ? |
| 517 | + [validateByteView(entry)] : validateBatchEntry(entry); |
513 | 518 | this.#bufferedBytes = 0; |
514 | 519 | return result; |
515 | 520 | } |
516 | 521 |
|
| 522 | + // The chunks of every entry, validated straight into one batch. |
517 | 523 | const result = []; |
518 | 524 | for (let i = 0; i < this.#slots.length; i++) { |
519 | | - const batch = validateBatchEntry(this.#slots.get(i)); |
520 | | - for (let j = 0; j < batch.length; j++) { |
521 | | - result[result.length] = batch[j]; |
| 525 | + const entry = this.#slots.get(i); |
| 526 | + const views = entry.views; |
| 527 | + if (views === undefined) { |
| 528 | + result[result.length] = validateByteView(entry); |
| 529 | + continue; |
| 530 | + } |
| 531 | + for (let j = 0; j < views.length; j++) { |
| 532 | + result[result.length] = validateByteView(views[j]); |
522 | 533 | } |
523 | 534 | } |
524 | 535 | this.#slots.clear(); |
@@ -587,7 +598,7 @@ class PushQueue { |
587 | 598 | this.#bufferedBytes < this.#budget) { |
588 | 599 | const pending = this.#pendingWrites.shift(); |
589 | 600 | try { |
590 | | - validateBatchEntry(pending.batch); |
| 601 | + checkEntry(pending.batch); |
591 | 602 | this.#slots.push(pending.batch); |
592 | 603 | this.#bufferedBytes += pending.batch.byteLength; |
593 | 604 | this.#bytesWritten += pending.batch.byteLength; |
@@ -640,6 +651,26 @@ class PushQueue { |
640 | 651 | } |
641 | 652 | } |
642 | 653 |
|
| 654 | +// The buffered entry of a single-chunk write: the chunk's FixedByteView, which |
| 655 | +// has the byteLength a BatchEntry has, instead of a BatchEntry holding an |
| 656 | +// array with it. Writing one chunk is the common case, and this saves the |
| 657 | +// write the BatchEntry, its views array, and the [chunk] array it was made |
| 658 | +// from. Entries are told apart by `views`, which a FixedByteView lacks. |
| 659 | +function entryOfChunk(chunk) { |
| 660 | + return snapshotByteView(chunk); |
| 661 | +} |
| 662 | + |
| 663 | +// Check that the chunks of a buffered entry still have the byteLength they |
| 664 | +// were accepted with, as validateBatchEntry() does, without collecting them. |
| 665 | +function checkEntry(entry) { |
| 666 | + const views = entry.views; |
| 667 | + if (views === undefined) { |
| 668 | + validateByteView(entry); |
| 669 | + return; |
| 670 | + } |
| 671 | + for (let i = 0; i < views.length; i++) validateByteView(views[i]); |
| 672 | +} |
| 673 | + |
643 | 674 | // ============================================================================= |
644 | 675 | // PushWriter Implementation |
645 | 676 | // ============================================================================= |
@@ -663,32 +694,30 @@ class PushWriter { |
663 | 694 | } |
664 | 695 |
|
665 | 696 | write(chunk, options) { |
666 | | - const bytes = toWriterUint8Array(chunk); |
| 697 | + const entry = entryOfChunk(toWriterUint8Array(chunk)); |
667 | 698 | const signal = getWriterSignal(options); |
668 | 699 | if (!signal && this.#queue.canWriteSync()) { |
669 | | - this.#queue.writeSync([bytes]); |
| 700 | + this.#queue.writeSync(entry); |
670 | 701 | return kResolvedPromise; |
671 | 702 | } |
672 | | - return this.#queue.writeAsync([bytes], signal); |
| 703 | + return this.#queue.writeAsync(entry, signal); |
673 | 704 | } |
674 | 705 |
|
675 | 706 | writev(chunks, options) { |
676 | | - const bytes = convertChunks(chunks); |
| 707 | + const entry = createBatchEntry(convertChunks(chunks)); |
677 | 708 | const signal = getWriterSignal(options); |
678 | | - if (!signal && this.#queue.writeSync(bytes)) { |
| 709 | + if (!signal && this.#queue.writeSync(entry)) { |
679 | 710 | return kResolvedPromise; |
680 | 711 | } |
681 | | - return this.#queue.writeAsync(bytes, signal); |
| 712 | + return this.#queue.writeAsync(entry, signal); |
682 | 713 | } |
683 | 714 |
|
684 | 715 | writeSync(chunk) { |
685 | | - const bytes = toWriterUint8Array(chunk); |
686 | | - return this.#queue.writeSync([bytes]); |
| 716 | + return this.#queue.writeSync(entryOfChunk(toWriterUint8Array(chunk))); |
687 | 717 | } |
688 | 718 |
|
689 | 719 | writevSync(chunks) { |
690 | | - const bytes = convertChunks(chunks); |
691 | | - return this.#queue.writeSync(bytes); |
| 720 | + return this.#queue.writeSync(createBatchEntry(convertChunks(chunks))); |
692 | 721 | } |
693 | 722 |
|
694 | 723 | end(options) { |
|