Skip to content

Commit e024712

Browse files
mcollinaaduh95
authored andcommitted
stream: use the ring buffer for pending BYOB pull-into descriptors
The byte controller's [[pendingPullIntos]] list was still a plain array consumed with ArrayPrototypeShift, while every other per-chunk queue in the WHATWG streams implementation has moved to the Queue ring buffer. BYOB reads push and shift one descriptor per read, and Array.prototype shift has real per-call cost even at length 1. Back the descriptor list with the same lazily materialized Queue used for the request queues, so constructing a byte stream still allocates no descriptor storage. Signed-off-by: Matteo Collina <hello@matteocollina.com> PR-URL: #64818 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Aviv Keller <me@aviv.sh> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
1 parent fafac6e commit e024712

3 files changed

Lines changed: 164 additions & 86 deletions

File tree

benchmark/webstreams/tee.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const { ReadableStream } = require('node:stream/web');
4+
5+
const bench = common.createBenchmark(main, {
6+
n: [1e5],
7+
type: ['normal', 'bytes'],
8+
});
9+
10+
async function main({ n, type }) {
11+
let i = 0;
12+
const source = type === 'bytes' ?
13+
{
14+
type: 'bytes',
15+
pull(controller) {
16+
if (i++ < n) controller.enqueue(new Uint8Array(16));
17+
else controller.close();
18+
},
19+
} :
20+
{
21+
pull(controller) {
22+
if (i++ < n) controller.enqueue('a');
23+
else controller.close();
24+
},
25+
};
26+
27+
const rs = new ReadableStream(source);
28+
const [branch1, branch2] = rs.tee();
29+
const reader1 = branch1.getReader();
30+
const reader2 = branch2.getReader();
31+
let reads = 0;
32+
33+
bench.start();
34+
for (;;) {
35+
const [result1, result2] = await Promise.all([
36+
reader1.read(),
37+
reader2.read(),
38+
]);
39+
if (result1.done || result2.done) break;
40+
reads++;
41+
}
42+
bench.end(reads);
43+
console.assert(reads === n);
44+
}

0 commit comments

Comments
 (0)