Commit cbb2568

Browse files
anonrigaduh95
authored andcommitted
stream: use ring buffer for WHATWG stream queues
The [[queue]] backing every default readable/writable controller was a plain array of { value, size } wrappers consumed with ArrayPrototypeShift, so each buffered chunk allocated a wrapper object and each dequeue moved (or forced the engine to re-linearize) the remaining elements; the byte controller queue paid the same shift cost for its chunk descriptor records. Replace the array with a power-of-two ring buffer. Default controller queues store each entry as (value, size) in two consecutive slots, so the per-chunk wrapper allocation disappears; the byte controller keeps its descriptor records (they are mutated in place at the head) in single slots. Controllers start from (and are reset to) a shared immutable empty queue, so constructing a stream allocates no queue storage until a chunk is actually buffered. Enqueues measured by the internal default size algorithm (never observable by user code, always returns 1, cannot throw) skip the algorithm call and its try/catch entirely. The layout mirrors what Bun/WebKit use for the same spec structure: [[queue]] as a ring-buffer deque (WTF::Deque in Bun's src/jsc/bindings/webcore/streams/StreamQueue.h), the pure-JS ring buffer in Bun's src/js/internal/fifo.ts, and the trivial-size-algorithm bypass in src/jsc/bindings/webcore/streams/JSReadableStreamDefaultController.cpp. benchmark/compare.js against the unmodified baseline (30-run capture plus an independent 15-run repeat, Welch t-test, all p < 1e-5): webstreams/pipe-to.js +12-17% across all sixteen high-water-mark configurations, readable-read-buffered +20% (bufferSize=1) to +49% (bufferSize=1000), readable-async-iterator +21%. No stable significant regression across the rest of the webstreams suite: the creation.js and readable-read.js deltas seen in the full-suite capture disappear in isolated 60-run rechecks. Refs: https://github.com/oven-sh/bun/blob/main/src/js/internal/fifo.ts Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com> Assisted-by: Grok (Grok Build) PR-URL: #64312 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 67688cc commit cbb2568

4 files changed

Lines changed: 352 additions & 25 deletions

File tree

‎lib/internal/webstreams/readablestream.js‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,11 @@ const {
109109
extractSizeAlgorithm,
110110
getNonWritablePropertyDescriptor,
111111
isBrandCheck,
112+
kEmptyQueue,
112113
kState,
113114
kType,
114115
lazyTransfer,
116+
materializeQueue,
115117
nonOpCancel,
116118
nonOpPull,
117119
nonOpStart,
@@ -2519,6 +2521,11 @@ function readableStreamDefaultControllerEnqueue(controller, chunk) {
25192521
reader[kType]==='ReadableStreamDefaultReader'&&
25202522
reader[kState].readRequests.length){
25212523
readableStreamFulfillReadRequest(stream,chunk,false);
2524+
}elseif(controllerState.sizeAlgorithm===defaultSizeAlgorithm){
2525+
// The internal default size algorithm is never observable by user
2526+
// code, always returns 1, and cannot throw: enqueue with the
2527+
// constant instead of calling it.
2528+
enqueueValueWithSize(controller,chunk,1);
25222529
}else{
25232530
try{
25242531
constchunkSize=
@@ -2676,7 +2683,7 @@ function setupReadableStreamDefaultController(
26762683
pulling: false,
26772684
pullFulfilled: undefined,
26782685
pullRejected: undefined,
2679-
queue: [],
2686+
queue: kEmptyQueue,
26802687
queueTotalSize: 0,
26812688
started: false,
26822689
sizeAlgorithm,
@@ -3151,14 +3158,13 @@ function readableByteStreamControllerEnqueueChunkToQueue(
31513158
buffer,
31523159
byteOffset,
31533160
byteLength){
3154-
ArrayPrototypePush(
3155-
controller[kState].queue,
3156-
{
3157-
buffer,
3158-
byteOffset,
3159-
byteLength,
3160-
});
3161-
controller[kState].queueTotalSize+=byteLength;
3161+
conststate=controller[kState];
3162+
materializeQueue(state).push({
3163+
buffer,
3164+
byteOffset,
3165+
byteLength,
3166+
});
3167+
state.queueTotalSize+=byteLength;
31623168
}
31633169

31643170
functionreadableByteStreamControllerEnqueueDetachedPullIntoToQueue(
@@ -3213,7 +3219,7 @@ function readableByteStreamControllerFillPullIntoDescriptorFromQueue(
32133219
}=controller[kState];
32143220

32153221
while(totalBytesToCopyRemaining){
3216-
constheadOfQueue=queue[0];
3222+
constheadOfQueue=queue.peek();
32173223
constbytesToCopy=MathMin(
32183224
totalBytesToCopyRemaining,
32193225
headOfQueue.byteLength);
@@ -3231,7 +3237,7 @@ function readableByteStreamControllerFillPullIntoDescriptorFromQueue(
32313237
headOfQueue.byteOffset,
32323238
bytesToCopy);
32333239
if(headOfQueue.byteLength===bytesToCopy){
3234-
ArrayPrototypeShift(queue);
3240+
queue.shift();
32353241
}else{
32363242
headOfQueue.byteOffset+=bytesToCopy;
32373243
headOfQueue.byteLength-=bytesToCopy;
@@ -3447,7 +3453,7 @@ function readableByteStreamControllerDequeueChunk(controller) {
34473453
buffer,
34483454
byteOffset,
34493455
byteLength,
3450-
}=ArrayPrototypeShift(controller[kState].queue);
3456+
}=controller[kState].queue.shift();
34513457

34523458
controller[kState].queueTotalSize-=byteLength;
34533459
readableByteStreamControllerHandleQueueDrain(controller);
@@ -3543,7 +3549,7 @@ function setupReadableByteStreamController(
35433549
pullRejected: undefined,
35443550
started: false,
35453551
stream,
3546-
queue: [],
3552+
queue: kEmptyQueue,
35473553
queueTotalSize: 0,
35483554
highWaterMark,
35493555
pullAlgorithm,

‎lib/internal/webstreams/util.js‎

Lines changed: 148 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
'use strict';
22

33
const{
4+
Array,
45
ArrayBufferPrototypeGetByteLength,
56
ArrayBufferPrototypeGetDetached,
67
ArrayBufferPrototypeSlice,
7-
ArrayPrototypePush,
8-
ArrayPrototypeShift,
98
AsyncIteratorPrototype,
109
DataViewPrototypeGetBuffer,
1110
DataViewPrototypeGetByteLength,
1211
DataViewPrototypeGetByteOffset,
1312
FunctionPrototypeCall,
1413
MathMax,
1514
NumberIsNaN,
15+
ObjectFreeze,
1616
PromisePrototypeThen,
1717
PromiseReject,
1818
PromiseResolve,
@@ -152,32 +152,167 @@ function isBrandCheck(brand) {
152152
};
153153
}
154154

155+
// Backing store for the spec's [[queue]]: a power-of-two ring buffer
156+
// instead of a plain array. Entries are pushed at the tail and consumed
157+
// at the head, so a plain array either moves every element or forces the
158+
// engine to re-linearize on each shift, and the default controllers would
159+
// additionally have to allocate a { value, size } wrapper object per
160+
// chunk just to keep the pair together. Default readable/writable
161+
// controller queues store each entry as (value, size) in two consecutive
162+
// slots via the *Pair methods; the readable byte controller queue stores
163+
// its chunk descriptor records in single slots via push/shift/peek. A
164+
// given instance only ever uses one of the two access patterns, so
165+
// head/tail stay aligned to the entry stride.
166+
classQueue{
167+
constructor(listLength=8){
168+
this.head=0;
169+
this.tail=0;
170+
// Number of logical entries currently in the queue: (value, size)
171+
// pairs for default controller queues, descriptor records for byte
172+
// controller queues.
173+
this.length=0;
174+
this.capacityMask=listLength-1;
175+
this.list=newArray(listLength);
176+
this.dequeuedSize=0;
177+
}
178+
179+
// Single-slot entries (readable byte controller chunk records).
180+
181+
push(entry){
182+
consttail=this.tail;
183+
this.list[tail]=entry;
184+
this.tail=(tail+1)&this.capacityMask;
185+
this.length++;
186+
if(this.tail===this.head)
187+
this.grow();
188+
}
189+
190+
shift(){
191+
consthead=this.head;
192+
constlist=this.list;
193+
constentry=list[head];
194+
list[head]=undefined;
195+
this.head=(head+1)&this.capacityMask;
196+
if(--this.length===0)
197+
this.rewind();
198+
returnentry;
199+
}
200+
201+
peek(){
202+
returnthis.list[this.head];
203+
}
204+
205+
// Two-slot (value, size) entries (default controller queues). The
206+
// stride is always 2 and capacities are even, so `tail + 1`/`head + 1`
207+
// never need to wrap.
208+
209+
pushPair(value,size){
210+
consttail=this.tail;
211+
constlist=this.list;
212+
list[tail]=value;
213+
list[tail+1]=size;
214+
this.tail=(tail+2)&this.capacityMask;
215+
this.length++;
216+
if(this.tail===this.head)
217+
this.grow();
218+
}
219+
220+
// Returns the dequeued value; the size of the same entry is left in
221+
// `this.dequeuedSize` so that callers can update [[queueTotalSize]]
222+
// without a per-entry wrapper object having to exist.
223+
shiftPair(){
224+
consthead=this.head;
225+
constlist=this.list;
226+
constvalue=list[head];
227+
this.dequeuedSize=list[head+1];
228+
list[head]=undefined;
229+
list[head+1]=undefined;
230+
this.head=(head+2)&this.capacityMask;
231+
if(--this.length===0)
232+
this.rewind();
233+
returnvalue;
234+
}
235+
236+
peekPairValue(){
237+
returnthis.list[this.head];
238+
}
239+
240+
// The ring is completely full (the post-push tail caught up with the
241+
// head): double the capacity, re-linearizing from the head so index
242+
// arithmetic stays trivial.
243+
grow(){
244+
constlist=this.list;
245+
constcapacity=list.length;
246+
consthead=this.head;
247+
if(head!==0){
248+
constrelinearized=newArray(capacity*2);
249+
letn=0;
250+
for(leti=head;i<capacity;i++)
251+
relinearized[n++]=list[i];
252+
for(leti=0;i<head;i++)
253+
relinearized[n++]=list[i];
254+
this.list=relinearized;
255+
this.head=0;
256+
}else{
257+
list.length=capacity*2;
258+
}
259+
this.tail=capacity;
260+
this.capacityMask=(capacity*2)-1;
261+
}
262+
263+
// The queue just became empty: restart at slot 0 so shallow queues net
264+
// sequential slot access, and drop the enlarged backing store after a
265+
// large burst has fully drained.
266+
rewind(){
267+
this.head=0;
268+
this.tail=0;
269+
if(this.list.length>1024){
270+
this.list.length=8;
271+
this.capacityMask=0b111;
272+
}
273+
}
274+
}
275+
276+
// Controllers start out with (and are reset to) this shared immutable
277+
// empty queue, so constructing a stream never allocates queue storage;
278+
// a real Queue is materialized by the enqueue paths on first use. All
279+
// dequeue/peek paths are guarded by `.length` (or the equivalent
280+
// [[queueTotalSize]]) checks, so they can never observe the sentinel in
281+
// a mutating way; it never stores entries, so it gets a zero-length
282+
// backing list.
283+
constkEmptyQueue=ObjectFreeze(newQueue(0));
284+
285+
functionmaterializeQueue(state){
286+
constqueue=state.queue;
287+
if(queue===kEmptyQueue)
288+
returnstate.queue=newQueue();
289+
returnqueue;
290+
}
291+
155292
// The queue helpers below run once per chunk on the hot paths of every
156293
// default readable/writable stream, so they load the controller state a
157294
// single time and don't assert the existence of the queue fields (both
158295
// are unconditionally initialized during controller setup and only ever
159296
// replaced wholesale).
160297
functiondequeueValue(controller){
161298
conststate=controller[kState];
162-
assert(state.queue.length);
163-
const{
164-
value,
165-
size,
166-
}=ArrayPrototypeShift(state.queue);
167-
state.queueTotalSize=MathMax(0,state.queueTotalSize-size);
299+
constqueue=state.queue;
300+
assert(queue.length);
301+
constvalue=queue.shiftPair();
302+
state.queueTotalSize=MathMax(0,state.queueTotalSize-queue.dequeuedSize);
168303
returnvalue;
169304
}
170305

171306
functionresetQueue(controller){
172307
conststate=controller[kState];
173-
state.queue=[];
308+
state.queue=kEmptyQueue;
174309
state.queueTotalSize=0;
175310
}
176311

177312
functionpeekQueueValue(controller){
178313
conststate=controller[kState];
179314
assert(state.queue.length);
180-
returnstate.queue[0].value;
315+
returnstate.queue.peekPairValue();
181316
}
182317

183318
functionenqueueValueWithSize(controller,value,size){
@@ -188,7 +323,7 @@ function enqueueValueWithSize(controller, value, size) {
188323
coercedSize===Infinity){
189324
thrownewERR_INVALID_ARG_VALUE.RangeError('size',size);
190325
}
191-
ArrayPrototypePush(state.queue,{value,size: coercedSize});
326+
materializeQueue(state).pushPair(value,coercedSize);
192327
state.queueTotalSize+=coercedSize;
193328
}
194329

@@ -284,9 +419,11 @@ module.exports = {
284419
getNonWritablePropertyDescriptor,
285420
isBrandCheck,
286421
isPromisePending,
422+
kEmptyQueue,
287423
kState,
288424
kType,
289425
lazyTransfer,
426+
materializeQueue,
290427
nonOpCancel,
291428
nonOpFlush,
292429
nonOpPull,

‎lib/internal/webstreams/writablestream.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ const {
6767
getNonWritablePropertyDescriptor,
6868
isBrandCheck,
6969
isPromisePending,
70+
kEmptyQueue,
7071
kState,
7172
kType,
7273
lazyTransfer,
@@ -1177,6 +1178,11 @@ function writableStreamDefaultControllerGetChunkSize(controller, chunk) {
11771178
return1;
11781179
}
11791180

1181+
// The internal default size algorithm is never observable by user
1182+
// code, always returns 1, and cannot throw: skip the call.
1183+
if(sizeAlgorithm===defaultSizeAlgorithm)
1184+
return1;
1185+
11801186
try{
11811187
returnFunctionPrototypeCall(
11821188
sizeAlgorithm,
@@ -1293,7 +1299,7 @@ function setupWritableStreamDefaultController(
12931299
abortAlgorithm,
12941300
closeAlgorithm,
12951301
highWaterMark,
1296-
queue: [],
1302+
queue: kEmptyQueue,
12971303
queueTotalSize: 0,
12981304
abortController: newAbortController(),
12991305
sizeAlgorithm,

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Commit cbb2568

Browse files
anonrigaduh95
authored andcommitted
stream: use ring buffer for WHATWG stream queues
The [[queue]] backing every default readable/writable controller was a plain array of { value, size } wrappers consumed with ArrayPrototypeShift, so each buffered chunk allocated a wrapper object and each dequeue moved (or forced the engine to re-linearize) the remaining elements; the byte controller queue paid the same shift cost for its chunk descriptor records. Replace the array with a power-of-two ring buffer. Default controller queues store each entry as (value, size) in two consecutive slots, so the per-chunk wrapper allocation disappears; the byte controller keeps its descriptor records (they are mutated in place at the head) in single slots. Controllers start from (and are reset to) a shared immutable empty queue, so constructing a stream allocates no queue storage until a chunk is actually buffered. Enqueues measured by the internal default size algorithm (never observable by user code, always returns 1, cannot throw) skip the algorithm call and its try/catch entirely. The layout mirrors what Bun/WebKit use for the same spec structure: [[queue]] as a ring-buffer deque (WTF::Deque in Bun's src/jsc/bindings/webcore/streams/StreamQueue.h), the pure-JS ring buffer in Bun's src/js/internal/fifo.ts, and the trivial-size-algorithm bypass in src/jsc/bindings/webcore/streams/JSReadableStreamDefaultController.cpp. benchmark/compare.js against the unmodified baseline (30-run capture plus an independent 15-run repeat, Welch t-test, all p < 1e-5): webstreams/pipe-to.js +12-17% across all sixteen high-water-mark configurations, readable-read-buffered +20% (bufferSize=1) to +49% (bufferSize=1000), readable-async-iterator +21%. No stable significant regression across the rest of the webstreams suite: the creation.js and readable-read.js deltas seen in the full-suite capture disappear in isolated 60-run rechecks. Refs: https://github.com/oven-sh/bun/blob/main/src/js/internal/fifo.ts Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com> Assisted-by: Grok (Grok Build) PR-URL: #64312 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 67688cc commit cbb2568

4 files changed

Lines changed: 352 additions & 25 deletions

File tree

‎lib/internal/webstreams/readablestream.js‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,11 @@ const {
109109
extractSizeAlgorithm,
110110
getNonWritablePropertyDescriptor,
111111
isBrandCheck,
112+
kEmptyQueue,
112113
kState,
113114
kType,
114115
lazyTransfer,
116+
materializeQueue,
115117
nonOpCancel,
116118
nonOpPull,
117119
nonOpStart,
@@ -2519,6 +2521,11 @@ function readableStreamDefaultControllerEnqueue(controller, chunk) {
25192521
reader[kType]==='ReadableStreamDefaultReader'&&
25202522
reader[kState].readRequests.length){
25212523
readableStreamFulfillReadRequest(stream,chunk,false);
2524+
}elseif(controllerState.sizeAlgorithm===defaultSizeAlgorithm){
2525+
// The internal default size algorithm is never observable by user
2526+
// code, always returns 1, and cannot throw: enqueue with the
2527+
// constant instead of calling it.
2528+
enqueueValueWithSize(controller,chunk,1);
25222529
}else{
25232530
try{
25242531
constchunkSize=
@@ -2676,7 +2683,7 @@ function setupReadableStreamDefaultController(
26762683
pulling: false,
26772684
pullFulfilled: undefined,
26782685
pullRejected: undefined,
2679-
queue: [],
2686+
queue: kEmptyQueue,
26802687
queueTotalSize: 0,
26812688
started: false,
26822689
sizeAlgorithm,
@@ -3151,14 +3158,13 @@ function readableByteStreamControllerEnqueueChunkToQueue(
31513158
buffer,
31523159
byteOffset,
31533160
byteLength){
3154-
ArrayPrototypePush(
3155-
controller[kState].queue,
3156-
{
3157-
buffer,
3158-
byteOffset,
3159-
byteLength,
3160-
});
3161-
controller[kState].queueTotalSize+=byteLength;
3161+
conststate=controller[kState];
3162+
materializeQueue(state).push({
3163+
buffer,
3164+
byteOffset,
3165+
byteLength,
3166+
});
3167+
state.queueTotalSize+=byteLength;
31623168
}
31633169

31643170
functionreadableByteStreamControllerEnqueueDetachedPullIntoToQueue(
@@ -3213,7 +3219,7 @@ function readableByteStreamControllerFillPullIntoDescriptorFromQueue(
32133219
}=controller[kState];
32143220

32153221
while(totalBytesToCopyRemaining){
3216-
constheadOfQueue=queue[0];
3222+
constheadOfQueue=queue.peek();
32173223
constbytesToCopy=MathMin(
32183224
totalBytesToCopyRemaining,
32193225
headOfQueue.byteLength);
@@ -3231,7 +3237,7 @@ function readableByteStreamControllerFillPullIntoDescriptorFromQueue(
32313237
headOfQueue.byteOffset,
32323238
bytesToCopy);
32333239
if(headOfQueue.byteLength===bytesToCopy){
3234-
ArrayPrototypeShift(queue);
3240+
queue.shift();
32353241
}else{
32363242
headOfQueue.byteOffset+=bytesToCopy;
32373243
headOfQueue.byteLength-=bytesToCopy;
@@ -3447,7 +3453,7 @@ function readableByteStreamControllerDequeueChunk(controller) {
34473453
buffer,
34483454
byteOffset,
34493455
byteLength,
3450-
}=ArrayPrototypeShift(controller[kState].queue);
3456+
}=controller[kState].queue.shift();
34513457

34523458
controller[kState].queueTotalSize-=byteLength;
34533459
readableByteStreamControllerHandleQueueDrain(controller);
@@ -3543,7 +3549,7 @@ function setupReadableByteStreamController(
35433549
pullRejected: undefined,
35443550
started: false,
35453551
stream,
3546-
queue: [],
3552+
queue: kEmptyQueue,
35473553
queueTotalSize: 0,
35483554
highWaterMark,
35493555
pullAlgorithm,

‎lib/internal/webstreams/util.js‎

Lines changed: 148 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
'use strict';
22

33
const{
4+
Array,
45
ArrayBufferPrototypeGetByteLength,
56
ArrayBufferPrototypeGetDetached,
67
ArrayBufferPrototypeSlice,
7-
ArrayPrototypePush,
8-
ArrayPrototypeShift,
98
AsyncIteratorPrototype,
109
DataViewPrototypeGetBuffer,
1110
DataViewPrototypeGetByteLength,
1211
DataViewPrototypeGetByteOffset,
1312
FunctionPrototypeCall,
1413
MathMax,
1514
NumberIsNaN,
15+
ObjectFreeze,
1616
PromisePrototypeThen,
1717
PromiseReject,
1818
PromiseResolve,
@@ -152,32 +152,167 @@ function isBrandCheck(brand) {
152152
};
153153
}
154154

155+
// Backing store for the spec's [[queue]]: a power-of-two ring buffer
156+
// instead of a plain array. Entries are pushed at the tail and consumed
157+
// at the head, so a plain array either moves every element or forces the
158+
// engine to re-linearize on each shift, and the default controllers would
159+
// additionally have to allocate a { value, size } wrapper object per
160+
// chunk just to keep the pair together. Default readable/writable
161+
// controller queues store each entry as (value, size) in two consecutive
162+
// slots via the *Pair methods; the readable byte controller queue stores
163+
// its chunk descriptor records in single slots via push/shift/peek. A
164+
// given instance only ever uses one of the two access patterns, so
165+
// head/tail stay aligned to the entry stride.
166+
classQueue{
167+
constructor(listLength=8){
168+
this.head=0;
169+
this.tail=0;
170+
// Number of logical entries currently in the queue: (value, size)
171+
// pairs for default controller queues, descriptor records for byte
172+
// controller queues.
173+
this.length=0;
174+
this.capacityMask=listLength-1;
175+
this.list=newArray(listLength);
176+
this.dequeuedSize=0;
177+
}
178+
179+
// Single-slot entries (readable byte controller chunk records).
180+
181+
push(entry){
182+
consttail=this.tail;
183+
this.list[tail]=entry;
184+
this.tail=(tail+1)&this.capacityMask;
185+
this.length++;
186+
if(this.tail===this.head)
187+
this.grow();
188+
}
189+
190+
shift(){
191+
consthead=this.head;
192+
constlist=this.list;
193+
constentry=list[head];
194+
list[head]=undefined;
195+
this.head=(head+1)&this.capacityMask;
196+
if(--this.length===0)
197+
this.rewind();
198+
returnentry;
199+
}
200+
201+
peek(){
202+
returnthis.list[this.head];
203+
}
204+
205+
// Two-slot (value, size) entries (default controller queues). The
206+
// stride is always 2 and capacities are even, so `tail + 1`/`head + 1`
207+
// never need to wrap.
208+
209+
pushPair(value,size){
210+
consttail=this.tail;
211+
constlist=this.list;
212+
list[tail]=value;
213+
list[tail+1]=size;
214+
this.tail=(tail+2)&this.capacityMask;
215+
this.length++;
216+
if(this.tail===this.head)
217+
this.grow();
218+
}
219+
220+
// Returns the dequeued value; the size of the same entry is left in
221+
// `this.dequeuedSize` so that callers can update [[queueTotalSize]]
222+
// without a per-entry wrapper object having to exist.
223+
shiftPair(){
224+
consthead=this.head;
225+
constlist=this.list;
226+
constvalue=list[head];
227+
this.dequeuedSize=list[head+1];
228+
list[head]=undefined;
229+
list[head+1]=undefined;
230+
this.head=(head+2)&this.capacityMask;
231+
if(--this.length===0)
232+
this.rewind();
233+
returnvalue;
234+
}
235+
236+
peekPairValue(){
237+
returnthis.list[this.head];
238+
}
239+
240+
// The ring is completely full (the post-push tail caught up with the
241+
// head): double the capacity, re-linearizing from the head so index
242+
// arithmetic stays trivial.
243+
grow(){
244+
constlist=this.list;
245+
constcapacity=list.length;
246+
consthead=this.head;
247+
if(head!==0){
248+
constrelinearized=newArray(capacity*2);
249+
letn=0;
250+
for(leti=head;i<capacity;i++)
251+
relinearized[n++]=list[i];
252+
for(leti=0;i<head;i++)
253+
relinearized[n++]=list[i];
254+
this.list=relinearized;
255+
this.head=0;
256+
}else{
257+
list.length=capacity*2;
258+
}
259+
this.tail=capacity;
260+
this.capacityMask=(capacity*2)-1;
261+
}
262+
263+
// The queue just became empty: restart at slot 0 so shallow queues net
264+
// sequential slot access, and drop the enlarged backing store after a
265+
// large burst has fully drained.
266+
rewind(){
267+
this.head=0;
268+
this.tail=0;
269+
if(this.list.length>1024){
270+
this.list.length=8;
271+
this.capacityMask=0b111;
272+
}
273+
}
274+
}
275+
276+
// Controllers start out with (and are reset to) this shared immutable
277+
// empty queue, so constructing a stream never allocates queue storage;
278+
// a real Queue is materialized by the enqueue paths on first use. All
279+
// dequeue/peek paths are guarded by `.length` (or the equivalent
280+
// [[queueTotalSize]]) checks, so they can never observe the sentinel in
281+
// a mutating way; it never stores entries, so it gets a zero-length
282+
// backing list.
283+
constkEmptyQueue=ObjectFreeze(newQueue(0));
284+
285+
functionmaterializeQueue(state){
286+
constqueue=state.queue;
287+
if(queue===kEmptyQueue)
288+
returnstate.queue=newQueue();
289+
returnqueue;
290+
}
291+
155292
// The queue helpers below run once per chunk on the hot paths of every
156293
// default readable/writable stream, so they load the controller state a
157294
// single time and don't assert the existence of the queue fields (both
158295
// are unconditionally initialized during controller setup and only ever
159296
// replaced wholesale).
160297
functiondequeueValue(controller){
161298
conststate=controller[kState];
162-
assert(state.queue.length);
163-
const{
164-
value,
165-
size,
166-
}=ArrayPrototypeShift(state.queue);
167-
state.queueTotalSize=MathMax(0,state.queueTotalSize-size);
299+
constqueue=state.queue;
300+
assert(queue.length);
301+
constvalue=queue.shiftPair();
302+
state.queueTotalSize=MathMax(0,state.queueTotalSize-queue.dequeuedSize);
168303
returnvalue;
169304
}
170305

171306
functionresetQueue(controller){
172307
conststate=controller[kState];
173-
state.queue=[];
308+
state.queue=kEmptyQueue;
174309
state.queueTotalSize=0;
175310
}
176311

177312
functionpeekQueueValue(controller){
178313
conststate=controller[kState];
179314
assert(state.queue.length);
180-
returnstate.queue[0].value;
315+
returnstate.queue.peekPairValue();
181316
}
182317

183318
functionenqueueValueWithSize(controller,value,size){
@@ -188,7 +323,7 @@ function enqueueValueWithSize(controller, value, size) {
188323
coercedSize===Infinity){
189324
thrownewERR_INVALID_ARG_VALUE.RangeError('size',size);
190325
}
191-
ArrayPrototypePush(state.queue,{value,size: coercedSize});
326+
materializeQueue(state).pushPair(value,coercedSize);
192327
state.queueTotalSize+=coercedSize;
193328
}
194329

@@ -284,9 +419,11 @@ module.exports = {
284419
getNonWritablePropertyDescriptor,
285420
isBrandCheck,
286421
isPromisePending,
422+
kEmptyQueue,
287423
kState,
288424
kType,
289425
lazyTransfer,
426+
materializeQueue,
290427
nonOpCancel,
291428
nonOpFlush,
292429
nonOpPull,

‎lib/internal/webstreams/writablestream.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ const {
6767
getNonWritablePropertyDescriptor,
6868
isBrandCheck,
6969
isPromisePending,
70+
kEmptyQueue,
7071
kState,
7172
kType,
7273
lazyTransfer,
@@ -1177,6 +1178,11 @@ function writableStreamDefaultControllerGetChunkSize(controller, chunk) {
11771178
return1;
11781179
}
11791180

1181+
// The internal default size algorithm is never observable by user
1182+
// code, always returns 1, and cannot throw: skip the call.
1183+
if(sizeAlgorithm===defaultSizeAlgorithm)
1184+
return1;
1185+
11801186
try{
11811187
returnFunctionPrototypeCall(
11821188
sizeAlgorithm,
@@ -1293,7 +1299,7 @@ function setupWritableStreamDefaultController(
12931299
abortAlgorithm,
12941300
closeAlgorithm,
12951301
highWaterMark,
1296-
queue: [],
1302+
queue: kEmptyQueue,
12971303
queueTotalSize: 0,
12981304
abortController: newAbortController(),
12991305
sizeAlgorithm,

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit cbb2568

Browse files
anonrigaduh95
authored andcommitted
stream: use ring buffer for WHATWG stream queues
The [[queue]] backing every default readable/writable controller was a plain array of { value, size } wrappers consumed with ArrayPrototypeShift, so each buffered chunk allocated a wrapper object and each dequeue moved (or forced the engine to re-linearize) the remaining elements; the byte controller queue paid the same shift cost for its chunk descriptor records. Replace the array with a power-of-two ring buffer. Default controller queues store each entry as (value, size) in two consecutive slots, so the per-chunk wrapper allocation disappears; the byte controller keeps its descriptor records (they are mutated in place at the head) in single slots. Controllers start from (and are reset to) a shared immutable empty queue, so constructing a stream allocates no queue storage until a chunk is actually buffered. Enqueues measured by the internal default size algorithm (never observable by user code, always returns 1, cannot throw) skip the algorithm call and its try/catch entirely. The layout mirrors what Bun/WebKit use for the same spec structure: [[queue]] as a ring-buffer deque (WTF::Deque in Bun's src/jsc/bindings/webcore/streams/StreamQueue.h), the pure-JS ring buffer in Bun's src/js/internal/fifo.ts, and the trivial-size-algorithm bypass in src/jsc/bindings/webcore/streams/JSReadableStreamDefaultController.cpp. benchmark/compare.js against the unmodified baseline (30-run capture plus an independent 15-run repeat, Welch t-test, all p < 1e-5): webstreams/pipe-to.js +12-17% across all sixteen high-water-mark configurations, readable-read-buffered +20% (bufferSize=1) to +49% (bufferSize=1000), readable-async-iterator +21%. No stable significant regression across the rest of the webstreams suite: the creation.js and readable-read.js deltas seen in the full-suite capture disappear in isolated 60-run rechecks. Refs: https://github.com/oven-sh/bun/blob/main/src/js/internal/fifo.ts Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com> Assisted-by: Grok (Grok Build) PR-URL: #64312 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 67688cc commit cbb2568

4 files changed

Lines changed: 352 additions & 25 deletions

File tree

‎lib/internal/webstreams/readablestream.js‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,11 @@ const {
109109
extractSizeAlgorithm,
110110
getNonWritablePropertyDescriptor,
111111
isBrandCheck,
112+
kEmptyQueue,
112113
kState,
113114
kType,
114115
lazyTransfer,
116+
materializeQueue,
115117
nonOpCancel,
116118
nonOpPull,
117119
nonOpStart,
@@ -2519,6 +2521,11 @@ function readableStreamDefaultControllerEnqueue(controller, chunk) {
25192521
reader[kType]==='ReadableStreamDefaultReader'&&
25202522
reader[kState].readRequests.length){
25212523
readableStreamFulfillReadRequest(stream,chunk,false);
2524+
}elseif(controllerState.sizeAlgorithm===defaultSizeAlgorithm){
2525+
// The internal default size algorithm is never observable by user
2526+
// code, always returns 1, and cannot throw: enqueue with the
2527+
// constant instead of calling it.
2528+
enqueueValueWithSize(controller,chunk,1);
25222529
}else{
25232530
try{
25242531
constchunkSize=
@@ -2676,7 +2683,7 @@ function setupReadableStreamDefaultController(
26762683
pulling: false,
26772684
pullFulfilled: undefined,
26782685
pullRejected: undefined,
2679-
queue: [],
2686+
queue: kEmptyQueue,
26802687
queueTotalSize: 0,
26812688
started: false,
26822689
sizeAlgorithm,
@@ -3151,14 +3158,13 @@ function readableByteStreamControllerEnqueueChunkToQueue(
31513158
buffer,
31523159
byteOffset,
31533160
byteLength){
3154-
ArrayPrototypePush(
3155-
controller[kState].queue,
3156-
{
3157-
buffer,
3158-
byteOffset,
3159-
byteLength,
3160-
});
3161-
controller[kState].queueTotalSize+=byteLength;
3161+
conststate=controller[kState];
3162+
materializeQueue(state).push({
3163+
buffer,
3164+
byteOffset,
3165+
byteLength,
3166+
});
3167+
state.queueTotalSize+=byteLength;
31623168
}
31633169

31643170
functionreadableByteStreamControllerEnqueueDetachedPullIntoToQueue(
@@ -3213,7 +3219,7 @@ function readableByteStreamControllerFillPullIntoDescriptorFromQueue(
32133219
}=controller[kState];
32143220

32153221
while(totalBytesToCopyRemaining){
3216-
constheadOfQueue=queue[0];
3222+
constheadOfQueue=queue.peek();
32173223
constbytesToCopy=MathMin(
32183224
totalBytesToCopyRemaining,
32193225
headOfQueue.byteLength);
@@ -3231,7 +3237,7 @@ function readableByteStreamControllerFillPullIntoDescriptorFromQueue(
32313237
headOfQueue.byteOffset,
32323238
bytesToCopy);
32333239
if(headOfQueue.byteLength===bytesToCopy){
3234-
ArrayPrototypeShift(queue);
3240+
queue.shift();
32353241
}else{
32363242
headOfQueue.byteOffset+=bytesToCopy;
32373243
headOfQueue.byteLength-=bytesToCopy;
@@ -3447,7 +3453,7 @@ function readableByteStreamControllerDequeueChunk(controller) {
34473453
buffer,
34483454
byteOffset,
34493455
byteLength,
3450-
}=ArrayPrototypeShift(controller[kState].queue);
3456+
}=controller[kState].queue.shift();
34513457

34523458
controller[kState].queueTotalSize-=byteLength;
34533459
readableByteStreamControllerHandleQueueDrain(controller);
@@ -3543,7 +3549,7 @@ function setupReadableByteStreamController(
35433549
pullRejected: undefined,
35443550
started: false,
35453551
stream,
3546-
queue: [],
3552+
queue: kEmptyQueue,
35473553
queueTotalSize: 0,
35483554
highWaterMark,
35493555
pullAlgorithm,

‎lib/internal/webstreams/util.js‎

Lines changed: 148 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
'use strict';
22

33
const{
4+
Array,
45
ArrayBufferPrototypeGetByteLength,
56
ArrayBufferPrototypeGetDetached,
67
ArrayBufferPrototypeSlice,
7-
ArrayPrototypePush,
8-
ArrayPrototypeShift,
98
AsyncIteratorPrototype,
109
DataViewPrototypeGetBuffer,
1110
DataViewPrototypeGetByteLength,
1211
DataViewPrototypeGetByteOffset,
1312
FunctionPrototypeCall,
1413
MathMax,
1514
NumberIsNaN,
15+
ObjectFreeze,
1616
PromisePrototypeThen,
1717
PromiseReject,
1818
PromiseResolve,
@@ -152,32 +152,167 @@ function isBrandCheck(brand) {
152152
};
153153
}
154154

155+
// Backing store for the spec's [[queue]]: a power-of-two ring buffer
156+
// instead of a plain array. Entries are pushed at the tail and consumed
157+
// at the head, so a plain array either moves every element or forces the
158+
// engine to re-linearize on each shift, and the default controllers would
159+
// additionally have to allocate a { value, size } wrapper object per
160+
// chunk just to keep the pair together. Default readable/writable
161+
// controller queues store each entry as (value, size) in two consecutive
162+
// slots via the *Pair methods; the readable byte controller queue stores
163+
// its chunk descriptor records in single slots via push/shift/peek. A
164+
// given instance only ever uses one of the two access patterns, so
165+
// head/tail stay aligned to the entry stride.
166+
classQueue{
167+
constructor(listLength=8){
168+
this.head=0;
169+
this.tail=0;
170+
// Number of logical entries currently in the queue: (value, size)
171+
// pairs for default controller queues, descriptor records for byte
172+
// controller queues.
173+
this.length=0;
174+
this.capacityMask=listLength-1;
175+
this.list=newArray(listLength);
176+
this.dequeuedSize=0;
177+
}
178+
179+
// Single-slot entries (readable byte controller chunk records).
180+
181+
push(entry){
182+
consttail=this.tail;
183+
this.list[tail]=entry;
184+
this.tail=(tail+1)&this.capacityMask;
185+
this.length++;
186+
if(this.tail===this.head)
187+
this.grow();
188+
}
189+
190+
shift(){
191+
consthead=this.head;
192+
constlist=this.list;
193+
constentry=list[head];
194+
list[head]=undefined;
195+
this.head=(head+1)&this.capacityMask;
196+
if(--this.length===0)
197+
this.rewind();
198+
returnentry;
199+
}
200+
201+
peek(){
202+
returnthis.list[this.head];
203+
}
204+
205+
// Two-slot (value, size) entries (default controller queues). The
206+
// stride is always 2 and capacities are even, so `tail + 1`/`head + 1`
207+
// never need to wrap.
208+
209+
pushPair(value,size){
210+
consttail=this.tail;
211+
constlist=this.list;
212+
list[tail]=value;
213+
list[tail+1]=size;
214+
this.tail=(tail+2)&this.capacityMask;
215+
this.length++;
216+
if(this.tail===this.head)
217+
this.grow();
218+
}
219+
220+
// Returns the dequeued value; the size of the same entry is left in
221+
// `this.dequeuedSize` so that callers can update [[queueTotalSize]]
222+
// without a per-entry wrapper object having to exist.
223+
shiftPair(){
224+
consthead=this.head;
225+
constlist=this.list;
226+
constvalue=list[head];
227+
this.dequeuedSize=list[head+1];
228+
list[head]=undefined;
229+
list[head+1]=undefined;
230+
this.head=(head+2)&this.capacityMask;
231+
if(--this.length===0)
232+
this.rewind();
233+
returnvalue;
234+
}
235+
236+
peekPairValue(){
237+
returnthis.list[this.head];
238+
}
239+
240+
// The ring is completely full (the post-push tail caught up with the
241+
// head): double the capacity, re-linearizing from the head so index
242+
// arithmetic stays trivial.
243+
grow(){
244+
constlist=this.list;
245+
constcapacity=list.length;
246+
consthead=this.head;
247+
if(head!==0){
248+
constrelinearized=newArray(capacity*2);
249+
letn=0;
250+
for(leti=head;i<capacity;i++)
251+
relinearized[n++]=list[i];
252+
for(leti=0;i<head;i++)
253+
relinearized[n++]=list[i];
254+
this.list=relinearized;
255+
this.head=0;
256+
}else{
257+
list.length=capacity*2;
258+
}
259+
this.tail=capacity;
260+
this.capacityMask=(capacity*2)-1;
261+
}
262+
263+
// The queue just became empty: restart at slot 0 so shallow queues net
264+
// sequential slot access, and drop the enlarged backing store after a
265+
// large burst has fully drained.
266+
rewind(){
267+
this.head=0;
268+
this.tail=0;
269+
if(this.list.length>1024){
270+
this.list.length=8;
271+
this.capacityMask=0b111;
272+
}
273+
}
274+
}
275+
276+
// Controllers start out with (and are reset to) this shared immutable
277+
// empty queue, so constructing a stream never allocates queue storage;
278+
// a real Queue is materialized by the enqueue paths on first use. All
279+
// dequeue/peek paths are guarded by `.length` (or the equivalent
280+
// [[queueTotalSize]]) checks, so they can never observe the sentinel in
281+
// a mutating way; it never stores entries, so it gets a zero-length
282+
// backing list.
283+
constkEmptyQueue=ObjectFreeze(newQueue(0));
284+
285+
functionmaterializeQueue(state){
286+
constqueue=state.queue;
287+
if(queue===kEmptyQueue)
288+
returnstate.queue=newQueue();
289+
returnqueue;
290+
}
291+
155292
// The queue helpers below run once per chunk on the hot paths of every
156293
// default readable/writable stream, so they load the controller state a
157294
// single time and don't assert the existence of the queue fields (both
158295
// are unconditionally initialized during controller setup and only ever
159296
// replaced wholesale).
160297
functiondequeueValue(controller){
161298
conststate=controller[kState];
162-
assert(state.queue.length);
163-
const{
164-
value,
165-
size,
166-
}=ArrayPrototypeShift(state.queue);
167-
state.queueTotalSize=MathMax(0,state.queueTotalSize-size);
299+
constqueue=state.queue;
300+
assert(queue.length);
301+
constvalue=queue.shiftPair();
302+
state.queueTotalSize=MathMax(0,state.queueTotalSize-queue.dequeuedSize);
168303
returnvalue;
169304
}
170305

171306
functionresetQueue(controller){
172307
conststate=controller[kState];
173-
state.queue=[];
308+
state.queue=kEmptyQueue;
174309
state.queueTotalSize=0;
175310
}
176311

177312
functionpeekQueueValue(controller){
178313
conststate=controller[kState];
179314
assert(state.queue.length);
180-
returnstate.queue[0].value;
315+
returnstate.queue.peekPairValue();
181316
}
182317

183318
functionenqueueValueWithSize(controller,value,size){
@@ -188,7 +323,7 @@ function enqueueValueWithSize(controller, value, size) {
188323
coercedSize===Infinity){
189324
thrownewERR_INVALID_ARG_VALUE.RangeError('size',size);
190325
}
191-
ArrayPrototypePush(state.queue,{value,size: coercedSize});
326+
materializeQueue(state).pushPair(value,coercedSize);
192327
state.queueTotalSize+=coercedSize;
193328
}
194329

@@ -284,9 +419,11 @@ module.exports = {
284419
getNonWritablePropertyDescriptor,
285420
isBrandCheck,
286421
isPromisePending,
422+
kEmptyQueue,
287423
kState,
288424
kType,
289425
lazyTransfer,
426+
materializeQueue,
290427
nonOpCancel,
291428
nonOpFlush,
292429
nonOpPull,

‎lib/internal/webstreams/writablestream.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ const {
6767
getNonWritablePropertyDescriptor,
6868
isBrandCheck,
6969
isPromisePending,
70+
kEmptyQueue,
7071
kState,
7172
kType,
7273
lazyTransfer,
@@ -1177,6 +1178,11 @@ function writableStreamDefaultControllerGetChunkSize(controller, chunk) {
11771178
return1;
11781179
}
11791180

1181+
// The internal default size algorithm is never observable by user
1182+
// code, always returns 1, and cannot throw: skip the call.
1183+
if(sizeAlgorithm===defaultSizeAlgorithm)
1184+
return1;
1185+
11801186
try{
11811187
returnFunctionPrototypeCall(
11821188
sizeAlgorithm,
@@ -1293,7 +1299,7 @@ function setupWritableStreamDefaultController(
12931299
abortAlgorithm,
12941300
closeAlgorithm,
12951301
highWaterMark,
1296-
queue: [],
1302+
queue: kEmptyQueue,
12971303
queueTotalSize: 0,
12981304
abortController: newAbortController(),
12991305
sizeAlgorithm,

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit cbb2568

Browse files
anonrigaduh95
authored andcommitted
stream: use ring buffer for WHATWG stream queues
The [[queue]] backing every default readable/writable controller was a plain array of { value, size } wrappers consumed with ArrayPrototypeShift, so each buffered chunk allocated a wrapper object and each dequeue moved (or forced the engine to re-linearize) the remaining elements; the byte controller queue paid the same shift cost for its chunk descriptor records. Replace the array with a power-of-two ring buffer. Default controller queues store each entry as (value, size) in two consecutive slots, so the per-chunk wrapper allocation disappears; the byte controller keeps its descriptor records (they are mutated in place at the head) in single slots. Controllers start from (and are reset to) a shared immutable empty queue, so constructing a stream allocates no queue storage until a chunk is actually buffered. Enqueues measured by the internal default size algorithm (never observable by user code, always returns 1, cannot throw) skip the algorithm call and its try/catch entirely. The layout mirrors what Bun/WebKit use for the same spec structure: [[queue]] as a ring-buffer deque (WTF::Deque in Bun's src/jsc/bindings/webcore/streams/StreamQueue.h), the pure-JS ring buffer in Bun's src/js/internal/fifo.ts, and the trivial-size-algorithm bypass in src/jsc/bindings/webcore/streams/JSReadableStreamDefaultController.cpp. benchmark/compare.js against the unmodified baseline (30-run capture plus an independent 15-run repeat, Welch t-test, all p < 1e-5): webstreams/pipe-to.js +12-17% across all sixteen high-water-mark configurations, readable-read-buffered +20% (bufferSize=1) to +49% (bufferSize=1000), readable-async-iterator +21%. No stable significant regression across the rest of the webstreams suite: the creation.js and readable-read.js deltas seen in the full-suite capture disappear in isolated 60-run rechecks. Refs: https://github.com/oven-sh/bun/blob/main/src/js/internal/fifo.ts Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com> Assisted-by: Grok (Grok Build) PR-URL: #64312 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 67688cc commit cbb2568

4 files changed

Lines changed: 352 additions & 25 deletions

File tree

‎lib/internal/webstreams/readablestream.js‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,11 @@ const {
109109
extractSizeAlgorithm,
110110
getNonWritablePropertyDescriptor,
111111
isBrandCheck,
112+
kEmptyQueue,
112113
kState,
113114
kType,
114115
lazyTransfer,
116+
materializeQueue,
115117
nonOpCancel,
116118
nonOpPull,
117119
nonOpStart,
@@ -2519,6 +2521,11 @@ function readableStreamDefaultControllerEnqueue(controller, chunk) {
25192521
reader[kType]==='ReadableStreamDefaultReader'&&
25202522
reader[kState].readRequests.length){
25212523
readableStreamFulfillReadRequest(stream,chunk,false);
2524+
}elseif(controllerState.sizeAlgorithm===defaultSizeAlgorithm){
2525+
// The internal default size algorithm is never observable by user
2526+
// code, always returns 1, and cannot throw: enqueue with the
2527+
// constant instead of calling it.
2528+
enqueueValueWithSize(controller,chunk,1);
25222529
}else{
25232530
try{
25242531
constchunkSize=
@@ -2676,7 +2683,7 @@ function setupReadableStreamDefaultController(
26762683
pulling: false,
26772684
pullFulfilled: undefined,
26782685
pullRejected: undefined,
2679-
queue: [],
2686+
queue: kEmptyQueue,
26802687
queueTotalSize: 0,
26812688
started: false,
26822689
sizeAlgorithm,
@@ -3151,14 +3158,13 @@ function readableByteStreamControllerEnqueueChunkToQueue(
31513158
buffer,
31523159
byteOffset,
31533160
byteLength){
3154-
ArrayPrototypePush(
3155-
controller[kState].queue,
3156-
{
3157-
buffer,
3158-
byteOffset,
3159-
byteLength,
3160-
});
3161-
controller[kState].queueTotalSize+=byteLength;
3161+
conststate=controller[kState];
3162+
materializeQueue(state).push({
3163+
buffer,
3164+
byteOffset,
3165+
byteLength,
3166+
});
3167+
state.queueTotalSize+=byteLength;
31623168
}
31633169

31643170
functionreadableByteStreamControllerEnqueueDetachedPullIntoToQueue(
@@ -3213,7 +3219,7 @@ function readableByteStreamControllerFillPullIntoDescriptorFromQueue(
32133219
}=controller[kState];
32143220

32153221
while(totalBytesToCopyRemaining){
3216-
constheadOfQueue=queue[0];
3222+
constheadOfQueue=queue.peek();
32173223
constbytesToCopy=MathMin(
32183224
totalBytesToCopyRemaining,
32193225
headOfQueue.byteLength);
@@ -3231,7 +3237,7 @@ function readableByteStreamControllerFillPullIntoDescriptorFromQueue(
32313237
headOfQueue.byteOffset,
32323238
bytesToCopy);
32333239
if(headOfQueue.byteLength===bytesToCopy){
3234-
ArrayPrototypeShift(queue);
3240+
queue.shift();
32353241
}else{
32363242
headOfQueue.byteOffset+=bytesToCopy;
32373243
headOfQueue.byteLength-=bytesToCopy;
@@ -3447,7 +3453,7 @@ function readableByteStreamControllerDequeueChunk(controller) {
34473453
buffer,
34483454
byteOffset,
34493455
byteLength,
3450-
}=ArrayPrototypeShift(controller[kState].queue);
3456+
}=controller[kState].queue.shift();
34513457

34523458
controller[kState].queueTotalSize-=byteLength;
34533459
readableByteStreamControllerHandleQueueDrain(controller);
@@ -3543,7 +3549,7 @@ function setupReadableByteStreamController(
35433549
pullRejected: undefined,
35443550
started: false,
35453551
stream,
3546-
queue: [],
3552+
queue: kEmptyQueue,
35473553
queueTotalSize: 0,
35483554
highWaterMark,
35493555
pullAlgorithm,

‎lib/internal/webstreams/util.js‎

Lines changed: 148 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
'use strict';
22

33
const{
4+
Array,
45
ArrayBufferPrototypeGetByteLength,
56
ArrayBufferPrototypeGetDetached,
67
ArrayBufferPrototypeSlice,
7-
ArrayPrototypePush,
8-
ArrayPrototypeShift,
98
AsyncIteratorPrototype,
109
DataViewPrototypeGetBuffer,
1110
DataViewPrototypeGetByteLength,
1211
DataViewPrototypeGetByteOffset,
1312
FunctionPrototypeCall,
1413
MathMax,
1514
NumberIsNaN,
15+
ObjectFreeze,
1616
PromisePrototypeThen,
1717
PromiseReject,
1818
PromiseResolve,
@@ -152,32 +152,167 @@ function isBrandCheck(brand) {
152152
};
153153
}
154154

155+
// Backing store for the spec's [[queue]]: a power-of-two ring buffer
156+
// instead of a plain array. Entries are pushed at the tail and consumed
157+
// at the head, so a plain array either moves every element or forces the
158+
// engine to re-linearize on each shift, and the default controllers would
159+
// additionally have to allocate a { value, size } wrapper object per
160+
// chunk just to keep the pair together. Default readable/writable
161+
// controller queues store each entry as (value, size) in two consecutive
162+
// slots via the *Pair methods; the readable byte controller queue stores
163+
// its chunk descriptor records in single slots via push/shift/peek. A
164+
// given instance only ever uses one of the two access patterns, so
165+
// head/tail stay aligned to the entry stride.
166+
classQueue{
167+
constructor(listLength=8){
168+
this.head=0;
169+
this.tail=0;
170+
// Number of logical entries currently in the queue: (value, size)
171+
// pairs for default controller queues, descriptor records for byte
172+
// controller queues.
173+
this.length=0;
174+
this.capacityMask=listLength-1;
175+
this.list=newArray(listLength);
176+
this.dequeuedSize=0;
177+
}
178+
179+
// Single-slot entries (readable byte controller chunk records).
180+
181+
push(entry){
182+
consttail=this.tail;
183+
this.list[tail]=entry;
184+
this.tail=(tail+1)&this.capacityMask;
185+
this.length++;
186+
if(this.tail===this.head)
187+
this.grow();
188+
}
189+
190+
shift(){
191+
consthead=this.head;
192+
constlist=this.list;
193+
constentry=list[head];
194+
list[head]=undefined;
195+
this.head=(head+1)&this.capacityMask;
196+
if(--this.length===0)
197+
this.rewind();
198+
returnentry;
199+
}
200+
201+
peek(){
202+
returnthis.list[this.head];
203+
}
204+
205+
// Two-slot (value, size) entries (default controller queues). The
206+
// stride is always 2 and capacities are even, so `tail + 1`/`head + 1`
207+
// never need to wrap.
208+
209+
pushPair(value,size){
210+
consttail=this.tail;
211+
constlist=this.list;
212+
list[tail]=value;
213+
list[tail+1]=size;
214+
this.tail=(tail+2)&this.capacityMask;
215+
this.length++;
216+
if(this.tail===this.head)
217+
this.grow();
218+
}
219+
220+
// Returns the dequeued value; the size of the same entry is left in
221+
// `this.dequeuedSize` so that callers can update [[queueTotalSize]]
222+
// without a per-entry wrapper object having to exist.
223+
shiftPair(){
224+
consthead=this.head;
225+
constlist=this.list;
226+
constvalue=list[head];
227+
this.dequeuedSize=list[head+1];
228+
list[head]=undefined;
229+
list[head+1]=undefined;
230+
this.head=(head+2)&this.capacityMask;
231+
if(--this.length===0)
232+
this.rewind();
233+
returnvalue;
234+
}
235+
236+
peekPairValue(){
237+
returnthis.list[this.head];
238+
}
239+
240+
// The ring is completely full (the post-push tail caught up with the
241+
// head): double the capacity, re-linearizing from the head so index
242+
// arithmetic stays trivial.
243+
grow(){
244+
constlist=this.list;
245+
constcapacity=list.length;
246+
consthead=this.head;
247+
if(head!==0){
248+
constrelinearized=newArray(capacity*2);
249+
letn=0;
250+
for(leti=head;i<capacity;i++)
251+
relinearized[n++]=list[i];
252+
for(leti=0;i<head;i++)
253+
relinearized[n++]=list[i];
254+
this.list=relinearized;
255+
this.head=0;
256+
}else{
257+
list.length=capacity*2;
258+
}
259+
this.tail=capacity;
260+
this.capacityMask=(capacity*2)-1;
261+
}
262+
263+
// The queue just became empty: restart at slot 0 so shallow queues net
264+
// sequential slot access, and drop the enlarged backing store after a
265+
// large burst has fully drained.
266+
rewind(){
267+
this.head=0;
268+
this.tail=0;
269+
if(this.list.length>1024){
270+
this.list.length=8;
271+
this.capacityMask=0b111;
272+
}
273+
}
274+
}
275+
276+
// Controllers start out with (and are reset to) this shared immutable
277+
// empty queue, so constructing a stream never allocates queue storage;
278+
// a real Queue is materialized by the enqueue paths on first use. All
279+
// dequeue/peek paths are guarded by `.length` (or the equivalent
280+
// [[queueTotalSize]]) checks, so they can never observe the sentinel in
281+
// a mutating way; it never stores entries, so it gets a zero-length
282+
// backing list.
283+
constkEmptyQueue=ObjectFreeze(newQueue(0));
284+
285+
functionmaterializeQueue(state){
286+
constqueue=state.queue;
287+
if(queue===kEmptyQueue)
288+
returnstate.queue=newQueue();
289+
returnqueue;
290+
}
291+
155292
// The queue helpers below run once per chunk on the hot paths of every
156293
// default readable/writable stream, so they load the controller state a
157294
// single time and don't assert the existence of the queue fields (both
158295
// are unconditionally initialized during controller setup and only ever
159296
// replaced wholesale).
160297
functiondequeueValue(controller){
161298
conststate=controller[kState];
162-
assert(state.queue.length);
163-
const{
164-
value,
165-
size,
166-
}=ArrayPrototypeShift(state.queue);
167-
state.queueTotalSize=MathMax(0,state.queueTotalSize-size);
299+
constqueue=state.queue;
300+
assert(queue.length);
301+
constvalue=queue.shiftPair();
302+
state.queueTotalSize=MathMax(0,state.queueTotalSize-queue.dequeuedSize);
168303
returnvalue;
169304
}
170305

171306
functionresetQueue(controller){
172307
conststate=controller[kState];
173-
state.queue=[];
308+
state.queue=kEmptyQueue;
174309
state.queueTotalSize=0;
175310
}
176311

177312
functionpeekQueueValue(controller){
178313
conststate=controller[kState];
179314
assert(state.queue.length);
180-
returnstate.queue[0].value;
315+
returnstate.queue.peekPairValue();
181316
}
182317

183318
functionenqueueValueWithSize(controller,value,size){
@@ -188,7 +323,7 @@ function enqueueValueWithSize(controller, value, size) {
188323
coercedSize===Infinity){
189324
thrownewERR_INVALID_ARG_VALUE.RangeError('size',size);
190325
}
191-
ArrayPrototypePush(state.queue,{value,size: coercedSize});
326+
materializeQueue(state).pushPair(value,coercedSize);
192327
state.queueTotalSize+=coercedSize;
193328
}
194329

@@ -284,9 +419,11 @@ module.exports = {
284419
getNonWritablePropertyDescriptor,
285420
isBrandCheck,
286421
isPromisePending,
422+
kEmptyQueue,
287423
kState,
288424
kType,
289425
lazyTransfer,
426+
materializeQueue,
290427
nonOpCancel,
291428
nonOpFlush,
292429
nonOpPull,

‎lib/internal/webstreams/writablestream.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ const {
6767
getNonWritablePropertyDescriptor,
6868
isBrandCheck,
6969
isPromisePending,
70+
kEmptyQueue,
7071
kState,
7172
kType,
7273
lazyTransfer,
@@ -1177,6 +1178,11 @@ function writableStreamDefaultControllerGetChunkSize(controller, chunk) {
11771178
return1;
11781179
}
11791180

1181+
// The internal default size algorithm is never observable by user
1182+
// code, always returns 1, and cannot throw: skip the call.
1183+
if(sizeAlgorithm===defaultSizeAlgorithm)
1184+
return1;
1185+
11801186
try{
11811187
returnFunctionPrototypeCall(
11821188
sizeAlgorithm,
@@ -1293,7 +1299,7 @@ function setupWritableStreamDefaultController(
12931299
abortAlgorithm,
12941300
closeAlgorithm,
12951301
highWaterMark,
1296-
queue: [],
1302+
queue: kEmptyQueue,
12971303
queueTotalSize: 0,
12981304
abortController: newAbortController(),
12991305
sizeAlgorithm,

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Commit cbb2568

Browse files
anonrigaduh95
authored andcommitted
stream: use ring buffer for WHATWG stream queues
The [[queue]] backing every default readable/writable controller was a plain array of { value, size } wrappers consumed with ArrayPrototypeShift, so each buffered chunk allocated a wrapper object and each dequeue moved (or forced the engine to re-linearize) the remaining elements; the byte controller queue paid the same shift cost for its chunk descriptor records. Replace the array with a power-of-two ring buffer. Default controller queues store each entry as (value, size) in two consecutive slots, so the per-chunk wrapper allocation disappears; the byte controller keeps its descriptor records (they are mutated in place at the head) in single slots. Controllers start from (and are reset to) a shared immutable empty queue, so constructing a stream allocates no queue storage until a chunk is actually buffered. Enqueues measured by the internal default size algorithm (never observable by user code, always returns 1, cannot throw) skip the algorithm call and its try/catch entirely. The layout mirrors what Bun/WebKit use for the same spec structure: [[queue]] as a ring-buffer deque (WTF::Deque in Bun's src/jsc/bindings/webcore/streams/StreamQueue.h), the pure-JS ring buffer in Bun's src/js/internal/fifo.ts, and the trivial-size-algorithm bypass in src/jsc/bindings/webcore/streams/JSReadableStreamDefaultController.cpp. benchmark/compare.js against the unmodified baseline (30-run capture plus an independent 15-run repeat, Welch t-test, all p < 1e-5): webstreams/pipe-to.js +12-17% across all sixteen high-water-mark configurations, readable-read-buffered +20% (bufferSize=1) to +49% (bufferSize=1000), readable-async-iterator +21%. No stable significant regression across the rest of the webstreams suite: the creation.js and readable-read.js deltas seen in the full-suite capture disappear in isolated 60-run rechecks. Refs: https://github.com/oven-sh/bun/blob/main/src/js/internal/fifo.ts Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com> Assisted-by: Grok (Grok Build) PR-URL: #64312 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 67688cc commit cbb2568

4 files changed

Lines changed: 352 additions & 25 deletions

File tree

‎lib/internal/webstreams/readablestream.js‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,11 @@ const {
109109
extractSizeAlgorithm,
110110
getNonWritablePropertyDescriptor,
111111
isBrandCheck,
112+
kEmptyQueue,
112113
kState,
113114
kType,
114115
lazyTransfer,
116+
materializeQueue,
115117
nonOpCancel,
116118
nonOpPull,
117119
nonOpStart,
@@ -2519,6 +2521,11 @@ function readableStreamDefaultControllerEnqueue(controller, chunk) {
25192521
reader[kType]==='ReadableStreamDefaultReader'&&
25202522
reader[kState].readRequests.length){
25212523
readableStreamFulfillReadRequest(stream,chunk,false);
2524+
}elseif(controllerState.sizeAlgorithm===defaultSizeAlgorithm){
2525+
// The internal default size algorithm is never observable by user
2526+
// code, always returns 1, and cannot throw: enqueue with the
2527+
// constant instead of calling it.
2528+
enqueueValueWithSize(controller,chunk,1);
25222529
}else{
25232530
try{
25242531
constchunkSize=
@@ -2676,7 +2683,7 @@ function setupReadableStreamDefaultController(
26762683
pulling: false,
26772684
pullFulfilled: undefined,
26782685
pullRejected: undefined,
2679-
queue: [],
2686+
queue: kEmptyQueue,
26802687
queueTotalSize: 0,
26812688
started: false,
26822689
sizeAlgorithm,
@@ -3151,14 +3158,13 @@ function readableByteStreamControllerEnqueueChunkToQueue(
31513158
buffer,
31523159
byteOffset,
31533160
byteLength){
3154-
ArrayPrototypePush(
3155-
controller[kState].queue,
3156-
{
3157-
buffer,
3158-
byteOffset,
3159-
byteLength,
3160-
});
3161-
controller[kState].queueTotalSize+=byteLength;
3161+
conststate=controller[kState];
3162+
materializeQueue(state).push({
3163+
buffer,
3164+
byteOffset,
3165+
byteLength,
3166+
});
3167+
state.queueTotalSize+=byteLength;
31623168
}
31633169

31643170
functionreadableByteStreamControllerEnqueueDetachedPullIntoToQueue(
@@ -3213,7 +3219,7 @@ function readableByteStreamControllerFillPullIntoDescriptorFromQueue(
32133219
}=controller[kState];
32143220

32153221
while(totalBytesToCopyRemaining){
3216-
constheadOfQueue=queue[0];
3222+
constheadOfQueue=queue.peek();
32173223
constbytesToCopy=MathMin(
32183224
totalBytesToCopyRemaining,
32193225
headOfQueue.byteLength);
@@ -3231,7 +3237,7 @@ function readableByteStreamControllerFillPullIntoDescriptorFromQueue(
32313237
headOfQueue.byteOffset,
32323238
bytesToCopy);
32333239
if(headOfQueue.byteLength===bytesToCopy){
3234-
ArrayPrototypeShift(queue);
3240+
queue.shift();
32353241
}else{
32363242
headOfQueue.byteOffset+=bytesToCopy;
32373243
headOfQueue.byteLength-=bytesToCopy;
@@ -3447,7 +3453,7 @@ function readableByteStreamControllerDequeueChunk(controller) {
34473453
buffer,
34483454
byteOffset,
34493455
byteLength,
3450-
}=ArrayPrototypeShift(controller[kState].queue);
3456+
}=controller[kState].queue.shift();
34513457

34523458
controller[kState].queueTotalSize-=byteLength;
34533459
readableByteStreamControllerHandleQueueDrain(controller);
@@ -3543,7 +3549,7 @@ function setupReadableByteStreamController(
35433549
pullRejected: undefined,
35443550
started: false,
35453551
stream,
3546-
queue: [],
3552+
queue: kEmptyQueue,
35473553
queueTotalSize: 0,
35483554
highWaterMark,
35493555
pullAlgorithm,

‎lib/internal/webstreams/util.js‎

Lines changed: 148 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
'use strict';
22

33
const{
4+
Array,
45
ArrayBufferPrototypeGetByteLength,
56
ArrayBufferPrototypeGetDetached,
67
ArrayBufferPrototypeSlice,
7-
ArrayPrototypePush,
8-
ArrayPrototypeShift,
98
AsyncIteratorPrototype,
109
DataViewPrototypeGetBuffer,
1110
DataViewPrototypeGetByteLength,
1211
DataViewPrototypeGetByteOffset,
1312
FunctionPrototypeCall,
1413
MathMax,
1514
NumberIsNaN,
15+
ObjectFreeze,
1616
PromisePrototypeThen,
1717
PromiseReject,
1818
PromiseResolve,
@@ -152,32 +152,167 @@ function isBrandCheck(brand) {
152152
};
153153
}
154154

155+
// Backing store for the spec's [[queue]]: a power-of-two ring buffer
156+
// instead of a plain array. Entries are pushed at the tail and consumed
157+
// at the head, so a plain array either moves every element or forces the
158+
// engine to re-linearize on each shift, and the default controllers would
159+
// additionally have to allocate a { value, size } wrapper object per
160+
// chunk just to keep the pair together. Default readable/writable
161+
// controller queues store each entry as (value, size) in two consecutive
162+
// slots via the *Pair methods; the readable byte controller queue stores
163+
// its chunk descriptor records in single slots via push/shift/peek. A
164+
// given instance only ever uses one of the two access patterns, so
165+
// head/tail stay aligned to the entry stride.
166+
classQueue{
167+
constructor(listLength=8){
168+
this.head=0;
169+
this.tail=0;
170+
// Number of logical entries currently in the queue: (value, size)
171+
// pairs for default controller queues, descriptor records for byte
172+
// controller queues.
173+
this.length=0;
174+
this.capacityMask=listLength-1;
175+
this.list=newArray(listLength);
176+
this.dequeuedSize=0;
177+
}
178+
179+
// Single-slot entries (readable byte controller chunk records).
180+
181+
push(entry){
182+
consttail=this.tail;
183+
this.list[tail]=entry;
184+
this.tail=(tail+1)&this.capacityMask;
185+
this.length++;
186+
if(this.tail===this.head)
187+
this.grow();
188+
}
189+
190+
shift(){
191+
consthead=this.head;
192+
constlist=this.list;
193+
constentry=list[head];
194+
list[head]=undefined;
195+
this.head=(head+1)&this.capacityMask;
196+
if(--this.length===0)
197+
this.rewind();
198+
returnentry;
199+
}
200+
201+
peek(){
202+
returnthis.list[this.head];
203+
}
204+
205+
// Two-slot (value, size) entries (default controller queues). The
206+
// stride is always 2 and capacities are even, so `tail + 1`/`head + 1`
207+
// never need to wrap.
208+
209+
pushPair(value,size){
210+
consttail=this.tail;
211+
constlist=this.list;
212+
list[tail]=value;
213+
list[tail+1]=size;
214+
this.tail=(tail+2)&this.capacityMask;
215+
this.length++;
216+
if(this.tail===this.head)
217+
this.grow();
218+
}
219+
220+
// Returns the dequeued value; the size of the same entry is left in
221+
// `this.dequeuedSize` so that callers can update [[queueTotalSize]]
222+
// without a per-entry wrapper object having to exist.
223+
shiftPair(){
224+
consthead=this.head;
225+
constlist=this.list;
226+
constvalue=list[head];
227+
this.dequeuedSize=list[head+1];
228+
list[head]=undefined;
229+
list[head+1]=undefined;
230+
this.head=(head+2)&this.capacityMask;
231+
if(--this.length===0)
232+
this.rewind();
233+
returnvalue;
234+
}
235+
236+
peekPairValue(){
237+
returnthis.list[this.head];
238+
}
239+
240+
// The ring is completely full (the post-push tail caught up with the
241+
// head): double the capacity, re-linearizing from the head so index
242+
// arithmetic stays trivial.
243+
grow(){
244+
constlist=this.list;
245+
constcapacity=list.length;
246+
consthead=this.head;
247+
if(head!==0){
248+
constrelinearized=newArray(capacity*2);
249+
letn=0;
250+
for(leti=head;i<capacity;i++)
251+
relinearized[n++]=list[i];
252+
for(leti=0;i<head;i++)
253+
relinearized[n++]=list[i];
254+
this.list=relinearized;
255+
this.head=0;
256+
}else{
257+
list.length=capacity*2;
258+
}
259+
this.tail=capacity;
260+
this.capacityMask=(capacity*2)-1;
261+
}
262+
263+
// The queue just became empty: restart at slot 0 so shallow queues net
264+
// sequential slot access, and drop the enlarged backing store after a
265+
// large burst has fully drained.
266+
rewind(){
267+
this.head=0;
268+
this.tail=0;
269+
if(this.list.length>1024){
270+
this.list.length=8;
271+
this.capacityMask=0b111;
272+
}
273+
}
274+
}
275+
276+
// Controllers start out with (and are reset to) this shared immutable
277+
// empty queue, so constructing a stream never allocates queue storage;
278+
// a real Queue is materialized by the enqueue paths on first use. All
279+
// dequeue/peek paths are guarded by `.length` (or the equivalent
280+
// [[queueTotalSize]]) checks, so they can never observe the sentinel in
281+
// a mutating way; it never stores entries, so it gets a zero-length
282+
// backing list.
283+
constkEmptyQueue=ObjectFreeze(newQueue(0));
284+
285+
functionmaterializeQueue(state){
286+
constqueue=state.queue;
287+
if(queue===kEmptyQueue)
288+
returnstate.queue=newQueue();
289+
returnqueue;
290+
}
291+
155292
// The queue helpers below run once per chunk on the hot paths of every
156293
// default readable/writable stream, so they load the controller state a
157294
// single time and don't assert the existence of the queue fields (both
158295
// are unconditionally initialized during controller setup and only ever
159296
// replaced wholesale).
160297
functiondequeueValue(controller){
161298
conststate=controller[kState];
162-
assert(state.queue.length);
163-
const{
164-
value,
165-
size,
166-
}=ArrayPrototypeShift(state.queue);
167-
state.queueTotalSize=MathMax(0,state.queueTotalSize-size);
299+
constqueue=state.queue;
300+
assert(queue.length);
301+
constvalue=queue.shiftPair();
302+
state.queueTotalSize=MathMax(0,state.queueTotalSize-queue.dequeuedSize);
168303
returnvalue;
169304
}
170305

171306
functionresetQueue(controller){
172307
conststate=controller[kState];
173-
state.queue=[];
308+
state.queue=kEmptyQueue;
174309
state.queueTotalSize=0;
175310
}
176311

177312
functionpeekQueueValue(controller){
178313
conststate=controller[kState];
179314
assert(state.queue.length);
180-
returnstate.queue[0].value;
315+
returnstate.queue.peekPairValue();
181316
}
182317

183318
functionenqueueValueWithSize(controller,value,size){
@@ -188,7 +323,7 @@ function enqueueValueWithSize(controller, value, size) {
188323
coercedSize===Infinity){
189324
thrownewERR_INVALID_ARG_VALUE.RangeError('size',size);
190325
}
191-
ArrayPrototypePush(state.queue,{value,size: coercedSize});
326+
materializeQueue(state).pushPair(value,coercedSize);
192327
state.queueTotalSize+=coercedSize;
193328
}
194329

@@ -284,9 +419,11 @@ module.exports = {
284419
getNonWritablePropertyDescriptor,
285420
isBrandCheck,
286421
isPromisePending,
422+
kEmptyQueue,
287423
kState,
288424
kType,
289425
lazyTransfer,
426+
materializeQueue,
290427
nonOpCancel,
291428
nonOpFlush,
292429
nonOpPull,

‎lib/internal/webstreams/writablestream.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ const {
6767
getNonWritablePropertyDescriptor,
6868
isBrandCheck,
6969
isPromisePending,
70+
kEmptyQueue,
7071
kState,
7172
kType,
7273
lazyTransfer,
@@ -1177,6 +1178,11 @@ function writableStreamDefaultControllerGetChunkSize(controller, chunk) {
11771178
return1;
11781179
}
11791180

1181+
// The internal default size algorithm is never observable by user
1182+
// code, always returns 1, and cannot throw: skip the call.
1183+
if(sizeAlgorithm===defaultSizeAlgorithm)
1184+
return1;
1185+
11801186
try{
11811187
returnFunctionPrototypeCall(
11821188
sizeAlgorithm,
@@ -1293,7 +1299,7 @@ function setupWritableStreamDefaultController(
12931299
abortAlgorithm,
12941300
closeAlgorithm,
12951301
highWaterMark,
1296-
queue: [],
1302+
queue: kEmptyQueue,
12971303
queueTotalSize: 0,
12981304
abortController: newAbortController(),
12991305
sizeAlgorithm,

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit cbb2568

Browse files
anonrigaduh95
authored andcommitted
stream: use ring buffer for WHATWG stream queues
The [[queue]] backing every default readable/writable controller was a plain array of { value, size } wrappers consumed with ArrayPrototypeShift, so each buffered chunk allocated a wrapper object and each dequeue moved (or forced the engine to re-linearize) the remaining elements; the byte controller queue paid the same shift cost for its chunk descriptor records. Replace the array with a power-of-two ring buffer. Default controller queues store each entry as (value, size) in two consecutive slots, so the per-chunk wrapper allocation disappears; the byte controller keeps its descriptor records (they are mutated in place at the head) in single slots. Controllers start from (and are reset to) a shared immutable empty queue, so constructing a stream allocates no queue storage until a chunk is actually buffered. Enqueues measured by the internal default size algorithm (never observable by user code, always returns 1, cannot throw) skip the algorithm call and its try/catch entirely. The layout mirrors what Bun/WebKit use for the same spec structure: [[queue]] as a ring-buffer deque (WTF::Deque in Bun's src/jsc/bindings/webcore/streams/StreamQueue.h), the pure-JS ring buffer in Bun's src/js/internal/fifo.ts, and the trivial-size-algorithm bypass in src/jsc/bindings/webcore/streams/JSReadableStreamDefaultController.cpp. benchmark/compare.js against the unmodified baseline (30-run capture plus an independent 15-run repeat, Welch t-test, all p < 1e-5): webstreams/pipe-to.js +12-17% across all sixteen high-water-mark configurations, readable-read-buffered +20% (bufferSize=1) to +49% (bufferSize=1000), readable-async-iterator +21%. No stable significant regression across the rest of the webstreams suite: the creation.js and readable-read.js deltas seen in the full-suite capture disappear in isolated 60-run rechecks. Refs: https://github.com/oven-sh/bun/blob/main/src/js/internal/fifo.ts Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com> Assisted-by: Grok (Grok Build) PR-URL: #64312 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 67688cc commit cbb2568

4 files changed

Lines changed: 352 additions & 25 deletions

File tree

‎lib/internal/webstreams/readablestream.js‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,11 @@ const {
109109
extractSizeAlgorithm,
110110
getNonWritablePropertyDescriptor,
111111
isBrandCheck,
112+
kEmptyQueue,
112113
kState,
113114
kType,
114115
lazyTransfer,
116+
materializeQueue,
115117
nonOpCancel,
116118
nonOpPull,
117119
nonOpStart,
@@ -2519,6 +2521,11 @@ function readableStreamDefaultControllerEnqueue(controller, chunk) {
25192521
reader[kType]==='ReadableStreamDefaultReader'&&
25202522
reader[kState].readRequests.length){
25212523
readableStreamFulfillReadRequest(stream,chunk,false);
2524+
}elseif(controllerState.sizeAlgorithm===defaultSizeAlgorithm){
2525+
// The internal default size algorithm is never observable by user
2526+
// code, always returns 1, and cannot throw: enqueue with the
2527+
// constant instead of calling it.
2528+
enqueueValueWithSize(controller,chunk,1);
25222529
}else{
25232530
try{
25242531
constchunkSize=
@@ -2676,7 +2683,7 @@ function setupReadableStreamDefaultController(
26762683
pulling: false,
26772684
pullFulfilled: undefined,
26782685
pullRejected: undefined,
2679-
queue: [],
2686+
queue: kEmptyQueue,
26802687
queueTotalSize: 0,
26812688
started: false,
26822689
sizeAlgorithm,
@@ -3151,14 +3158,13 @@ function readableByteStreamControllerEnqueueChunkToQueue(
31513158
buffer,
31523159
byteOffset,
31533160
byteLength){
3154-
ArrayPrototypePush(
3155-
controller[kState].queue,
3156-
{
3157-
buffer,
3158-
byteOffset,
3159-
byteLength,
3160-
});
3161-
controller[kState].queueTotalSize+=byteLength;
3161+
conststate=controller[kState];
3162+
materializeQueue(state).push({
3163+
buffer,
3164+
byteOffset,
3165+
byteLength,
3166+
});
3167+
state.queueTotalSize+=byteLength;
31623168
}
31633169

31643170
functionreadableByteStreamControllerEnqueueDetachedPullIntoToQueue(
@@ -3213,7 +3219,7 @@ function readableByteStreamControllerFillPullIntoDescriptorFromQueue(
32133219
}=controller[kState];
32143220

32153221
while(totalBytesToCopyRemaining){
3216-
constheadOfQueue=queue[0];
3222+
constheadOfQueue=queue.peek();
32173223
constbytesToCopy=MathMin(
32183224
totalBytesToCopyRemaining,
32193225
headOfQueue.byteLength);
@@ -3231,7 +3237,7 @@ function readableByteStreamControllerFillPullIntoDescriptorFromQueue(
32313237
headOfQueue.byteOffset,
32323238
bytesToCopy);
32333239
if(headOfQueue.byteLength===bytesToCopy){
3234-
ArrayPrototypeShift(queue);
3240+
queue.shift();
32353241
}else{
32363242
headOfQueue.byteOffset+=bytesToCopy;
32373243
headOfQueue.byteLength-=bytesToCopy;
@@ -3447,7 +3453,7 @@ function readableByteStreamControllerDequeueChunk(controller) {
34473453
buffer,
34483454
byteOffset,
34493455
byteLength,
3450-
}=ArrayPrototypeShift(controller[kState].queue);
3456+
}=controller[kState].queue.shift();
34513457

34523458
controller[kState].queueTotalSize-=byteLength;
34533459
readableByteStreamControllerHandleQueueDrain(controller);
@@ -3543,7 +3549,7 @@ function setupReadableByteStreamController(
35433549
pullRejected: undefined,
35443550
started: false,
35453551
stream,
3546-
queue: [],
3552+
queue: kEmptyQueue,
35473553
queueTotalSize: 0,
35483554
highWaterMark,
35493555
pullAlgorithm,

‎lib/internal/webstreams/util.js‎

Lines changed: 148 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
'use strict';
22

33
const{
4+
Array,
45
ArrayBufferPrototypeGetByteLength,
56
ArrayBufferPrototypeGetDetached,
67
ArrayBufferPrototypeSlice,
7-
ArrayPrototypePush,
8-
ArrayPrototypeShift,
98
AsyncIteratorPrototype,
109
DataViewPrototypeGetBuffer,
1110
DataViewPrototypeGetByteLength,
1211
DataViewPrototypeGetByteOffset,
1312
FunctionPrototypeCall,
1413
MathMax,
1514
NumberIsNaN,
15+
ObjectFreeze,
1616
PromisePrototypeThen,
1717
PromiseReject,
1818
PromiseResolve,
@@ -152,32 +152,167 @@ function isBrandCheck(brand) {
152152
};
153153
}
154154

155+
// Backing store for the spec's [[queue]]: a power-of-two ring buffer
156+
// instead of a plain array. Entries are pushed at the tail and consumed
157+
// at the head, so a plain array either moves every element or forces the
158+
// engine to re-linearize on each shift, and the default controllers would
159+
// additionally have to allocate a { value, size } wrapper object per
160+
// chunk just to keep the pair together. Default readable/writable
161+
// controller queues store each entry as (value, size) in two consecutive
162+
// slots via the *Pair methods; the readable byte controller queue stores
163+
// its chunk descriptor records in single slots via push/shift/peek. A
164+
// given instance only ever uses one of the two access patterns, so
165+
// head/tail stay aligned to the entry stride.
166+
classQueue{
167+
constructor(listLength=8){
168+
this.head=0;
169+
this.tail=0;
170+
// Number of logical entries currently in the queue: (value, size)
171+
// pairs for default controller queues, descriptor records for byte
172+
// controller queues.
173+
this.length=0;
174+
this.capacityMask=listLength-1;
175+
this.list=newArray(listLength);
176+
this.dequeuedSize=0;
177+
}
178+
179+
// Single-slot entries (readable byte controller chunk records).
180+
181+
push(entry){
182+
consttail=this.tail;
183+
this.list[tail]=entry;
184+
this.tail=(tail+1)&this.capacityMask;
185+
this.length++;
186+
if(this.tail===this.head)
187+
this.grow();
188+
}
189+
190+
shift(){
191+
consthead=this.head;
192+
constlist=this.list;
193+
constentry=list[head];
194+
list[head]=undefined;
195+
this.head=(head+1)&this.capacityMask;
196+
if(--this.length===0)
197+
this.rewind();
198+
returnentry;
199+
}
200+
201+
peek(){
202+
returnthis.list[this.head];
203+
}
204+
205+
// Two-slot (value, size) entries (default controller queues). The
206+
// stride is always 2 and capacities are even, so `tail + 1`/`head + 1`
207+
// never need to wrap.
208+
209+
pushPair(value,size){
210+
consttail=this.tail;
211+
constlist=this.list;
212+
list[tail]=value;
213+
list[tail+1]=size;
214+
this.tail=(tail+2)&this.capacityMask;
215+
this.length++;
216+
if(this.tail===this.head)
217+
this.grow();
218+
}
219+
220+
// Returns the dequeued value; the size of the same entry is left in
221+
// `this.dequeuedSize` so that callers can update [[queueTotalSize]]
222+
// without a per-entry wrapper object having to exist.
223+
shiftPair(){
224+
consthead=this.head;
225+
constlist=this.list;
226+
constvalue=list[head];
227+
this.dequeuedSize=list[head+1];
228+
list[head]=undefined;
229+
list[head+1]=undefined;
230+
this.head=(head+2)&this.capacityMask;
231+
if(--this.length===0)
232+
this.rewind();
233+
returnvalue;
234+
}
235+
236+
peekPairValue(){
237+
returnthis.list[this.head];
238+
}
239+
240+
// The ring is completely full (the post-push tail caught up with the
241+
// head): double the capacity, re-linearizing from the head so index
242+
// arithmetic stays trivial.
243+
grow(){
244+
constlist=this.list;
245+
constcapacity=list.length;
246+
consthead=this.head;
247+
if(head!==0){
248+
constrelinearized=newArray(capacity*2);
249+
letn=0;
250+
for(leti=head;i<capacity;i++)
251+
relinearized[n++]=list[i];
252+
for(leti=0;i<head;i++)
253+
relinearized[n++]=list[i];
254+
this.list=relinearized;
255+
this.head=0;
256+
}else{
257+
list.length=capacity*2;
258+
}
259+
this.tail=capacity;
260+
this.capacityMask=(capacity*2)-1;
261+
}
262+
263+
// The queue just became empty: restart at slot 0 so shallow queues net
264+
// sequential slot access, and drop the enlarged backing store after a
265+
// large burst has fully drained.
266+
rewind(){
267+
this.head=0;
268+
this.tail=0;
269+
if(this.list.length>1024){
270+
this.list.length=8;
271+
this.capacityMask=0b111;
272+
}
273+
}
274+
}
275+
276+
// Controllers start out with (and are reset to) this shared immutable
277+
// empty queue, so constructing a stream never allocates queue storage;
278+
// a real Queue is materialized by the enqueue paths on first use. All
279+
// dequeue/peek paths are guarded by `.length` (or the equivalent
280+
// [[queueTotalSize]]) checks, so they can never observe the sentinel in
281+
// a mutating way; it never stores entries, so it gets a zero-length
282+
// backing list.
283+
constkEmptyQueue=ObjectFreeze(newQueue(0));
284+
285+
functionmaterializeQueue(state){
286+
constqueue=state.queue;
287+
if(queue===kEmptyQueue)
288+
returnstate.queue=newQueue();
289+
returnqueue;
290+
}
291+
155292
// The queue helpers below run once per chunk on the hot paths of every
156293
// default readable/writable stream, so they load the controller state a
157294
// single time and don't assert the existence of the queue fields (both
158295
// are unconditionally initialized during controller setup and only ever
159296
// replaced wholesale).
160297
functiondequeueValue(controller){
161298
conststate=controller[kState];
162-
assert(state.queue.length);
163-
const{
164-
value,
165-
size,
166-
}=ArrayPrototypeShift(state.queue);
167-
state.queueTotalSize=MathMax(0,state.queueTotalSize-size);
299+
constqueue=state.queue;
300+
assert(queue.length);
301+
constvalue=queue.shiftPair();
302+
state.queueTotalSize=MathMax(0,state.queueTotalSize-queue.dequeuedSize);
168303
returnvalue;
169304
}
170305

171306
functionresetQueue(controller){
172307
conststate=controller[kState];
173-
state.queue=[];
308+
state.queue=kEmptyQueue;
174309
state.queueTotalSize=0;
175310
}
176311

177312
functionpeekQueueValue(controller){
178313
conststate=controller[kState];
179314
assert(state.queue.length);
180-
returnstate.queue[0].value;
315+
returnstate.queue.peekPairValue();
181316
}
182317

183318
functionenqueueValueWithSize(controller,value,size){
@@ -188,7 +323,7 @@ function enqueueValueWithSize(controller, value, size) {
188323
coercedSize===Infinity){
189324
thrownewERR_INVALID_ARG_VALUE.RangeError('size',size);
190325
}
191-
ArrayPrototypePush(state.queue,{value,size: coercedSize});
326+
materializeQueue(state).pushPair(value,coercedSize);
192327
state.queueTotalSize+=coercedSize;
193328
}
194329

@@ -284,9 +419,11 @@ module.exports = {
284419
getNonWritablePropertyDescriptor,
285420
isBrandCheck,
286421
isPromisePending,
422+
kEmptyQueue,
287423
kState,
288424
kType,
289425
lazyTransfer,
426+
materializeQueue,
290427
nonOpCancel,
291428
nonOpFlush,
292429
nonOpPull,

‎lib/internal/webstreams/writablestream.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ const {
6767
getNonWritablePropertyDescriptor,
6868
isBrandCheck,
6969
isPromisePending,
70+
kEmptyQueue,
7071
kState,
7172
kType,
7273
lazyTransfer,
@@ -1177,6 +1178,11 @@ function writableStreamDefaultControllerGetChunkSize(controller, chunk) {
11771178
return1;
11781179
}
11791180

1181+
// The internal default size algorithm is never observable by user
1182+
// code, always returns 1, and cannot throw: skip the call.
1183+
if(sizeAlgorithm===defaultSizeAlgorithm)
1184+
return1;
1185+
11801186
try{
11811187
returnFunctionPrototypeCall(
11821188
sizeAlgorithm,
@@ -1293,7 +1299,7 @@ function setupWritableStreamDefaultController(
12931299
abortAlgorithm,
12941300
closeAlgorithm,
12951301
highWaterMark,
1296-
queue: [],
1302+
queue: kEmptyQueue,
12971303
queueTotalSize: 0,
12981304
abortController: newAbortController(),
12991305
sizeAlgorithm,

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit cbb2568

Browse files
anonrigaduh95
authored andcommitted
stream: use ring buffer for WHATWG stream queues
The [[queue]] backing every default readable/writable controller was a plain array of { value, size } wrappers consumed with ArrayPrototypeShift, so each buffered chunk allocated a wrapper object and each dequeue moved (or forced the engine to re-linearize) the remaining elements; the byte controller queue paid the same shift cost for its chunk descriptor records. Replace the array with a power-of-two ring buffer. Default controller queues store each entry as (value, size) in two consecutive slots, so the per-chunk wrapper allocation disappears; the byte controller keeps its descriptor records (they are mutated in place at the head) in single slots. Controllers start from (and are reset to) a shared immutable empty queue, so constructing a stream allocates no queue storage until a chunk is actually buffered. Enqueues measured by the internal default size algorithm (never observable by user code, always returns 1, cannot throw) skip the algorithm call and its try/catch entirely. The layout mirrors what Bun/WebKit use for the same spec structure: [[queue]] as a ring-buffer deque (WTF::Deque in Bun's src/jsc/bindings/webcore/streams/StreamQueue.h), the pure-JS ring buffer in Bun's src/js/internal/fifo.ts, and the trivial-size-algorithm bypass in src/jsc/bindings/webcore/streams/JSReadableStreamDefaultController.cpp. benchmark/compare.js against the unmodified baseline (30-run capture plus an independent 15-run repeat, Welch t-test, all p < 1e-5): webstreams/pipe-to.js +12-17% across all sixteen high-water-mark configurations, readable-read-buffered +20% (bufferSize=1) to +49% (bufferSize=1000), readable-async-iterator +21%. No stable significant regression across the rest of the webstreams suite: the creation.js and readable-read.js deltas seen in the full-suite capture disappear in isolated 60-run rechecks. Refs: https://github.com/oven-sh/bun/blob/main/src/js/internal/fifo.ts Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com> Assisted-by: Grok (Grok Build) PR-URL: #64312 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 67688cc commit cbb2568

4 files changed

Lines changed: 352 additions & 25 deletions

File tree

‎lib/internal/webstreams/readablestream.js‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,11 @@ const {
109109
extractSizeAlgorithm,
110110
getNonWritablePropertyDescriptor,
111111
isBrandCheck,
112+
kEmptyQueue,
112113
kState,
113114
kType,
114115
lazyTransfer,
116+
materializeQueue,
115117
nonOpCancel,
116118
nonOpPull,
117119
nonOpStart,
@@ -2519,6 +2521,11 @@ function readableStreamDefaultControllerEnqueue(controller, chunk) {
25192521
reader[kType]==='ReadableStreamDefaultReader'&&
25202522
reader[kState].readRequests.length){
25212523
readableStreamFulfillReadRequest(stream,chunk,false);
2524+
}elseif(controllerState.sizeAlgorithm===defaultSizeAlgorithm){
2525+
// The internal default size algorithm is never observable by user
2526+
// code, always returns 1, and cannot throw: enqueue with the
2527+
// constant instead of calling it.
2528+
enqueueValueWithSize(controller,chunk,1);
25222529
}else{
25232530
try{
25242531
constchunkSize=
@@ -2676,7 +2683,7 @@ function setupReadableStreamDefaultController(
26762683
pulling: false,
26772684
pullFulfilled: undefined,
26782685
pullRejected: undefined,
2679-
queue: [],
2686+
queue: kEmptyQueue,
26802687
queueTotalSize: 0,
26812688
started: false,
26822689
sizeAlgorithm,
@@ -3151,14 +3158,13 @@ function readableByteStreamControllerEnqueueChunkToQueue(
31513158
buffer,
31523159
byteOffset,
31533160
byteLength){
3154-
ArrayPrototypePush(
3155-
controller[kState].queue,
3156-
{
3157-
buffer,
3158-
byteOffset,
3159-
byteLength,
3160-
});
3161-
controller[kState].queueTotalSize+=byteLength;
3161+
conststate=controller[kState];
3162+
materializeQueue(state).push({
3163+
buffer,
3164+
byteOffset,
3165+
byteLength,
3166+
});
3167+
state.queueTotalSize+=byteLength;
31623168
}
31633169

31643170
functionreadableByteStreamControllerEnqueueDetachedPullIntoToQueue(
@@ -3213,7 +3219,7 @@ function readableByteStreamControllerFillPullIntoDescriptorFromQueue(
32133219
}=controller[kState];
32143220

32153221
while(totalBytesToCopyRemaining){
3216-
constheadOfQueue=queue[0];
3222+
constheadOfQueue=queue.peek();
32173223
constbytesToCopy=MathMin(
32183224
totalBytesToCopyRemaining,
32193225
headOfQueue.byteLength);
@@ -3231,7 +3237,7 @@ function readableByteStreamControllerFillPullIntoDescriptorFromQueue(
32313237
headOfQueue.byteOffset,
32323238
bytesToCopy);
32333239
if(headOfQueue.byteLength===bytesToCopy){
3234-
ArrayPrototypeShift(queue);
3240+
queue.shift();
32353241
}else{
32363242
headOfQueue.byteOffset+=bytesToCopy;
32373243
headOfQueue.byteLength-=bytesToCopy;
@@ -3447,7 +3453,7 @@ function readableByteStreamControllerDequeueChunk(controller) {
34473453
buffer,
34483454
byteOffset,
34493455
byteLength,
3450-
}=ArrayPrototypeShift(controller[kState].queue);
3456+
}=controller[kState].queue.shift();
34513457

34523458
controller[kState].queueTotalSize-=byteLength;
34533459
readableByteStreamControllerHandleQueueDrain(controller);
@@ -3543,7 +3549,7 @@ function setupReadableByteStreamController(
35433549
pullRejected: undefined,
35443550
started: false,
35453551
stream,
3546-
queue: [],
3552+
queue: kEmptyQueue,
35473553
queueTotalSize: 0,
35483554
highWaterMark,
35493555
pullAlgorithm,

‎lib/internal/webstreams/util.js‎

Lines changed: 148 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
'use strict';
22

33
const{
4+
Array,
45
ArrayBufferPrototypeGetByteLength,
56
ArrayBufferPrototypeGetDetached,
67
ArrayBufferPrototypeSlice,
7-
ArrayPrototypePush,
8-
ArrayPrototypeShift,
98
AsyncIteratorPrototype,
109
DataViewPrototypeGetBuffer,
1110
DataViewPrototypeGetByteLength,
1211
DataViewPrototypeGetByteOffset,
1312
FunctionPrototypeCall,
1413
MathMax,
1514
NumberIsNaN,
15+
ObjectFreeze,
1616
PromisePrototypeThen,
1717
PromiseReject,
1818
PromiseResolve,
@@ -152,32 +152,167 @@ function isBrandCheck(brand) {
152152
};
153153
}
154154

155+
// Backing store for the spec's [[queue]]: a power-of-two ring buffer
156+
// instead of a plain array. Entries are pushed at the tail and consumed
157+
// at the head, so a plain array either moves every element or forces the
158+
// engine to re-linearize on each shift, and the default controllers would
159+
// additionally have to allocate a { value, size } wrapper object per
160+
// chunk just to keep the pair together. Default readable/writable
161+
// controller queues store each entry as (value, size) in two consecutive
162+
// slots via the *Pair methods; the readable byte controller queue stores
163+
// its chunk descriptor records in single slots via push/shift/peek. A
164+
// given instance only ever uses one of the two access patterns, so
165+
// head/tail stay aligned to the entry stride.
166+
classQueue{
167+
constructor(listLength=8){
168+
this.head=0;
169+
this.tail=0;
170+
// Number of logical entries currently in the queue: (value, size)
171+
// pairs for default controller queues, descriptor records for byte
172+
// controller queues.
173+
this.length=0;
174+
this.capacityMask=listLength-1;
175+
this.list=newArray(listLength);
176+
this.dequeuedSize=0;
177+
}
178+
179+
// Single-slot entries (readable byte controller chunk records).
180+
181+
push(entry){
182+
consttail=this.tail;
183+
this.list[tail]=entry;
184+
this.tail=(tail+1)&this.capacityMask;
185+
this.length++;
186+
if(this.tail===this.head)
187+
this.grow();
188+
}
189+
190+
shift(){
191+
consthead=this.head;
192+
constlist=this.list;
193+
constentry=list[head];
194+
list[head]=undefined;
195+
this.head=(head+1)&this.capacityMask;
196+
if(--this.length===0)
197+
this.rewind();
198+
returnentry;
199+
}
200+
201+
peek(){
202+
returnthis.list[this.head];
203+
}
204+
205+
// Two-slot (value, size) entries (default controller queues). The
206+
// stride is always 2 and capacities are even, so `tail + 1`/`head + 1`
207+
// never need to wrap.
208+
209+
pushPair(value,size){
210+
consttail=this.tail;
211+
constlist=this.list;
212+
list[tail]=value;
213+
list[tail+1]=size;
214+
this.tail=(tail+2)&this.capacityMask;
215+
this.length++;
216+
if(this.tail===this.head)
217+
this.grow();
218+
}
219+
220+
// Returns the dequeued value; the size of the same entry is left in
221+
// `this.dequeuedSize` so that callers can update [[queueTotalSize]]
222+
// without a per-entry wrapper object having to exist.
223+
shiftPair(){
224+
consthead=this.head;
225+
constlist=this.list;
226+
constvalue=list[head];
227+
this.dequeuedSize=list[head+1];
228+
list[head]=undefined;
229+
list[head+1]=undefined;
230+
this.head=(head+2)&this.capacityMask;
231+
if(--this.length===0)
232+
this.rewind();
233+
returnvalue;
234+
}
235+
236+
peekPairValue(){
237+
returnthis.list[this.head];
238+
}
239+
240+
// The ring is completely full (the post-push tail caught up with the
241+
// head): double the capacity, re-linearizing from the head so index
242+
// arithmetic stays trivial.
243+
grow(){
244+
constlist=this.list;
245+
constcapacity=list.length;
246+
consthead=this.head;
247+
if(head!==0){
248+
constrelinearized=newArray(capacity*2);
249+
letn=0;
250+
for(leti=head;i<capacity;i++)
251+
relinearized[n++]=list[i];
252+
for(leti=0;i<head;i++)
253+
relinearized[n++]=list[i];
254+
this.list=relinearized;
255+
this.head=0;
256+
}else{
257+
list.length=capacity*2;
258+
}
259+
this.tail=capacity;
260+
this.capacityMask=(capacity*2)-1;
261+
}
262+
263+
// The queue just became empty: restart at slot 0 so shallow queues net
264+
// sequential slot access, and drop the enlarged backing store after a
265+
// large burst has fully drained.
266+
rewind(){
267+
this.head=0;
268+
this.tail=0;
269+
if(this.list.length>1024){
270+
this.list.length=8;
271+
this.capacityMask=0b111;
272+
}
273+
}
274+
}
275+
276+
// Controllers start out with (and are reset to) this shared immutable
277+
// empty queue, so constructing a stream never allocates queue storage;
278+
// a real Queue is materialized by the enqueue paths on first use. All
279+
// dequeue/peek paths are guarded by `.length` (or the equivalent
280+
// [[queueTotalSize]]) checks, so they can never observe the sentinel in
281+
// a mutating way; it never stores entries, so it gets a zero-length
282+
// backing list.
283+
constkEmptyQueue=ObjectFreeze(newQueue(0));
284+
285+
functionmaterializeQueue(state){
286+
constqueue=state.queue;
287+
if(queue===kEmptyQueue)
288+
returnstate.queue=newQueue();
289+
returnqueue;
290+
}
291+
155292
// The queue helpers below run once per chunk on the hot paths of every
156293
// default readable/writable stream, so they load the controller state a
157294
// single time and don't assert the existence of the queue fields (both
158295
// are unconditionally initialized during controller setup and only ever
159296
// replaced wholesale).
160297
functiondequeueValue(controller){
161298
conststate=controller[kState];
162-
assert(state.queue.length);
163-
const{
164-
value,
165-
size,
166-
}=ArrayPrototypeShift(state.queue);
167-
state.queueTotalSize=MathMax(0,state.queueTotalSize-size);
299+
constqueue=state.queue;
300+
assert(queue.length);
301+
constvalue=queue.shiftPair();
302+
state.queueTotalSize=MathMax(0,state.queueTotalSize-queue.dequeuedSize);
168303
returnvalue;
169304
}
170305

171306
functionresetQueue(controller){
172307
conststate=controller[kState];
173-
state.queue=[];
308+
state.queue=kEmptyQueue;
174309
state.queueTotalSize=0;
175310
}
176311

177312
functionpeekQueueValue(controller){
178313
conststate=controller[kState];
179314
assert(state.queue.length);
180-
returnstate.queue[0].value;
315+
returnstate.queue.peekPairValue();
181316
}
182317

183318
functionenqueueValueWithSize(controller,value,size){
@@ -188,7 +323,7 @@ function enqueueValueWithSize(controller, value, size) {
188323
coercedSize===Infinity){
189324
thrownewERR_INVALID_ARG_VALUE.RangeError('size',size);
190325
}
191-
ArrayPrototypePush(state.queue,{value,size: coercedSize});
326+
materializeQueue(state).pushPair(value,coercedSize);
192327
state.queueTotalSize+=coercedSize;
193328
}
194329

@@ -284,9 +419,11 @@ module.exports = {
284419
getNonWritablePropertyDescriptor,
285420
isBrandCheck,
286421
isPromisePending,
422+
kEmptyQueue,
287423
kState,
288424
kType,
289425
lazyTransfer,
426+
materializeQueue,
290427
nonOpCancel,
291428
nonOpFlush,
292429
nonOpPull,

‎lib/internal/webstreams/writablestream.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ const {
6767
getNonWritablePropertyDescriptor,
6868
isBrandCheck,
6969
isPromisePending,
70+
kEmptyQueue,
7071
kState,
7172
kType,
7273
lazyTransfer,
@@ -1177,6 +1178,11 @@ function writableStreamDefaultControllerGetChunkSize(controller, chunk) {
11771178
return1;
11781179
}
11791180

1181+
// The internal default size algorithm is never observable by user
1182+
// code, always returns 1, and cannot throw: skip the call.
1183+
if(sizeAlgorithm===defaultSizeAlgorithm)
1184+
return1;
1185+
11801186
try{
11811187
returnFunctionPrototypeCall(
11821188
sizeAlgorithm,
@@ -1293,7 +1299,7 @@ function setupWritableStreamDefaultController(
12931299
abortAlgorithm,
12941300
closeAlgorithm,
12951301
highWaterMark,
1296-
queue: [],
1302+
queue: kEmptyQueue,
12971303
queueTotalSize: 0,
12981304
abortController: newAbortController(),
12991305
sizeAlgorithm,

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Commit cbb2568

Browse files
anonrigaduh95
authored andcommitted
stream: use ring buffer for WHATWG stream queues
The [[queue]] backing every default readable/writable controller was a plain array of { value, size } wrappers consumed with ArrayPrototypeShift, so each buffered chunk allocated a wrapper object and each dequeue moved (or forced the engine to re-linearize) the remaining elements; the byte controller queue paid the same shift cost for its chunk descriptor records. Replace the array with a power-of-two ring buffer. Default controller queues store each entry as (value, size) in two consecutive slots, so the per-chunk wrapper allocation disappears; the byte controller keeps its descriptor records (they are mutated in place at the head) in single slots. Controllers start from (and are reset to) a shared immutable empty queue, so constructing a stream allocates no queue storage until a chunk is actually buffered. Enqueues measured by the internal default size algorithm (never observable by user code, always returns 1, cannot throw) skip the algorithm call and its try/catch entirely. The layout mirrors what Bun/WebKit use for the same spec structure: [[queue]] as a ring-buffer deque (WTF::Deque in Bun's src/jsc/bindings/webcore/streams/StreamQueue.h), the pure-JS ring buffer in Bun's src/js/internal/fifo.ts, and the trivial-size-algorithm bypass in src/jsc/bindings/webcore/streams/JSReadableStreamDefaultController.cpp. benchmark/compare.js against the unmodified baseline (30-run capture plus an independent 15-run repeat, Welch t-test, all p < 1e-5): webstreams/pipe-to.js +12-17% across all sixteen high-water-mark configurations, readable-read-buffered +20% (bufferSize=1) to +49% (bufferSize=1000), readable-async-iterator +21%. No stable significant regression across the rest of the webstreams suite: the creation.js and readable-read.js deltas seen in the full-suite capture disappear in isolated 60-run rechecks. Refs: https://github.com/oven-sh/bun/blob/main/src/js/internal/fifo.ts Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com> Assisted-by: Grok (Grok Build) PR-URL: #64312 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 67688cc commit cbb2568

4 files changed

Lines changed: 352 additions & 25 deletions

File tree

‎lib/internal/webstreams/readablestream.js‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,11 @@ const {
109109
extractSizeAlgorithm,
110110
getNonWritablePropertyDescriptor,
111111
isBrandCheck,
112+
kEmptyQueue,
112113
kState,
113114
kType,
114115
lazyTransfer,
116+
materializeQueue,
115117
nonOpCancel,
116118
nonOpPull,
117119
nonOpStart,
@@ -2519,6 +2521,11 @@ function readableStreamDefaultControllerEnqueue(controller, chunk) {
25192521
reader[kType]==='ReadableStreamDefaultReader'&&
25202522
reader[kState].readRequests.length){
25212523
readableStreamFulfillReadRequest(stream,chunk,false);
2524+
}elseif(controllerState.sizeAlgorithm===defaultSizeAlgorithm){
2525+
// The internal default size algorithm is never observable by user
2526+
// code, always returns 1, and cannot throw: enqueue with the
2527+
// constant instead of calling it.
2528+
enqueueValueWithSize(controller,chunk,1);
25222529
}else{
25232530
try{
25242531
constchunkSize=
@@ -2676,7 +2683,7 @@ function setupReadableStreamDefaultController(
26762683
pulling: false,
26772684
pullFulfilled: undefined,
26782685
pullRejected: undefined,
2679-
queue: [],
2686+
queue: kEmptyQueue,
26802687
queueTotalSize: 0,
26812688
started: false,
26822689
sizeAlgorithm,
@@ -3151,14 +3158,13 @@ function readableByteStreamControllerEnqueueChunkToQueue(
31513158
buffer,
31523159
byteOffset,
31533160
byteLength){
3154-
ArrayPrototypePush(
3155-
controller[kState].queue,
3156-
{
3157-
buffer,
3158-
byteOffset,
3159-
byteLength,
3160-
});
3161-
controller[kState].queueTotalSize+=byteLength;
3161+
conststate=controller[kState];
3162+
materializeQueue(state).push({
3163+
buffer,
3164+
byteOffset,
3165+
byteLength,
3166+
});
3167+
state.queueTotalSize+=byteLength;
31623168
}
31633169

31643170
functionreadableByteStreamControllerEnqueueDetachedPullIntoToQueue(
@@ -3213,7 +3219,7 @@ function readableByteStreamControllerFillPullIntoDescriptorFromQueue(
32133219
}=controller[kState];
32143220

32153221
while(totalBytesToCopyRemaining){
3216-
constheadOfQueue=queue[0];
3222+
constheadOfQueue=queue.peek();
32173223
constbytesToCopy=MathMin(
32183224
totalBytesToCopyRemaining,
32193225
headOfQueue.byteLength);
@@ -3231,7 +3237,7 @@ function readableByteStreamControllerFillPullIntoDescriptorFromQueue(
32313237
headOfQueue.byteOffset,
32323238
bytesToCopy);
32333239
if(headOfQueue.byteLength===bytesToCopy){
3234-
ArrayPrototypeShift(queue);
3240+
queue.shift();
32353241
}else{
32363242
headOfQueue.byteOffset+=bytesToCopy;
32373243
headOfQueue.byteLength-=bytesToCopy;
@@ -3447,7 +3453,7 @@ function readableByteStreamControllerDequeueChunk(controller) {
34473453
buffer,
34483454
byteOffset,
34493455
byteLength,
3450-
}=ArrayPrototypeShift(controller[kState].queue);
3456+
}=controller[kState].queue.shift();
34513457

34523458
controller[kState].queueTotalSize-=byteLength;
34533459
readableByteStreamControllerHandleQueueDrain(controller);
@@ -3543,7 +3549,7 @@ function setupReadableByteStreamController(
35433549
pullRejected: undefined,
35443550
started: false,
35453551
stream,
3546-
queue: [],
3552+
queue: kEmptyQueue,
35473553
queueTotalSize: 0,
35483554
highWaterMark,
35493555
pullAlgorithm,

‎lib/internal/webstreams/util.js‎

Lines changed: 148 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
'use strict';
22

33
const{
4+
Array,
45
ArrayBufferPrototypeGetByteLength,
56
ArrayBufferPrototypeGetDetached,
67
ArrayBufferPrototypeSlice,
7-
ArrayPrototypePush,
8-
ArrayPrototypeShift,
98
AsyncIteratorPrototype,
109
DataViewPrototypeGetBuffer,
1110
DataViewPrototypeGetByteLength,
1211
DataViewPrototypeGetByteOffset,
1312
FunctionPrototypeCall,
1413
MathMax,
1514
NumberIsNaN,
15+
ObjectFreeze,
1616
PromisePrototypeThen,
1717
PromiseReject,
1818
PromiseResolve,
@@ -152,32 +152,167 @@ function isBrandCheck(brand) {
152152
};
153153
}
154154

155+
// Backing store for the spec's [[queue]]: a power-of-two ring buffer
156+
// instead of a plain array. Entries are pushed at the tail and consumed
157+
// at the head, so a plain array either moves every element or forces the
158+
// engine to re-linearize on each shift, and the default controllers would
159+
// additionally have to allocate a { value, size } wrapper object per
160+
// chunk just to keep the pair together. Default readable/writable
161+
// controller queues store each entry as (value, size) in two consecutive
162+
// slots via the *Pair methods; the readable byte controller queue stores
163+
// its chunk descriptor records in single slots via push/shift/peek. A
164+
// given instance only ever uses one of the two access patterns, so
165+
// head/tail stay aligned to the entry stride.
166+
classQueue{
167+
constructor(listLength=8){
168+
this.head=0;
169+
this.tail=0;
170+
// Number of logical entries currently in the queue: (value, size)
171+
// pairs for default controller queues, descriptor records for byte
172+
// controller queues.
173+
this.length=0;
174+
this.capacityMask=listLength-1;
175+
this.list=newArray(listLength);
176+
this.dequeuedSize=0;
177+
}
178+
179+
// Single-slot entries (readable byte controller chunk records).
180+
181+
push(entry){
182+
consttail=this.tail;
183+
this.list[tail]=entry;
184+
this.tail=(tail+1)&this.capacityMask;
185+
this.length++;
186+
if(this.tail===this.head)
187+
this.grow();
188+
}
189+
190+
shift(){
191+
consthead=this.head;
192+
constlist=this.list;
193+
constentry=list[head];
194+
list[head]=undefined;
195+
this.head=(head+1)&this.capacityMask;
196+
if(--this.length===0)
197+
this.rewind();
198+
returnentry;
199+
}
200+
201+
peek(){
202+
returnthis.list[this.head];
203+
}
204+
205+
// Two-slot (value, size) entries (default controller queues). The
206+
// stride is always 2 and capacities are even, so `tail + 1`/`head + 1`
207+
// never need to wrap.
208+
209+
pushPair(value,size){
210+
consttail=this.tail;
211+
constlist=this.list;
212+
list[tail]=value;
213+
list[tail+1]=size;
214+
this.tail=(tail+2)&this.capacityMask;
215+
this.length++;
216+
if(this.tail===this.head)
217+
this.grow();
218+
}
219+
220+
// Returns the dequeued value; the size of the same entry is left in
221+
// `this.dequeuedSize` so that callers can update [[queueTotalSize]]
222+
// without a per-entry wrapper object having to exist.
223+
shiftPair(){
224+
consthead=this.head;
225+
constlist=this.list;
226+
constvalue=list[head];
227+
this.dequeuedSize=list[head+1];
228+
list[head]=undefined;
229+
list[head+1]=undefined;
230+
this.head=(head+2)&this.capacityMask;
231+
if(--this.length===0)
232+
this.rewind();
233+
returnvalue;
234+
}
235+
236+
peekPairValue(){
237+
returnthis.list[this.head];
238+
}
239+
240+
// The ring is completely full (the post-push tail caught up with the
241+
// head): double the capacity, re-linearizing from the head so index
242+
// arithmetic stays trivial.
243+
grow(){
244+
constlist=this.list;
245+
constcapacity=list.length;
246+
consthead=this.head;
247+
if(head!==0){
248+
constrelinearized=newArray(capacity*2);
249+
letn=0;
250+
for(leti=head;i<capacity;i++)
251+
relinearized[n++]=list[i];
252+
for(leti=0;i<head;i++)
253+
relinearized[n++]=list[i];
254+
this.list=relinearized;
255+
this.head=0;
256+
}else{
257+
list.length=capacity*2;
258+
}
259+
this.tail=capacity;
260+
this.capacityMask=(capacity*2)-1;
261+
}
262+
263+
// The queue just became empty: restart at slot 0 so shallow queues net
264+
// sequential slot access, and drop the enlarged backing store after a
265+
// large burst has fully drained.
266+
rewind(){
267+
this.head=0;
268+
this.tail=0;
269+
if(this.list.length>1024){
270+
this.list.length=8;
271+
this.capacityMask=0b111;
272+
}
273+
}
274+
}
275+
276+
// Controllers start out with (and are reset to) this shared immutable
277+
// empty queue, so constructing a stream never allocates queue storage;
278+
// a real Queue is materialized by the enqueue paths on first use. All
279+
// dequeue/peek paths are guarded by `.length` (or the equivalent
280+
// [[queueTotalSize]]) checks, so they can never observe the sentinel in
281+
// a mutating way; it never stores entries, so it gets a zero-length
282+
// backing list.
283+
constkEmptyQueue=ObjectFreeze(newQueue(0));
284+
285+
functionmaterializeQueue(state){
286+
constqueue=state.queue;
287+
if(queue===kEmptyQueue)
288+
returnstate.queue=newQueue();
289+
returnqueue;
290+
}
291+
155292
// The queue helpers below run once per chunk on the hot paths of every
156293
// default readable/writable stream, so they load the controller state a
157294
// single time and don't assert the existence of the queue fields (both
158295
// are unconditionally initialized during controller setup and only ever
159296
// replaced wholesale).
160297
functiondequeueValue(controller){
161298
conststate=controller[kState];
162-
assert(state.queue.length);
163-
const{
164-
value,
165-
size,
166-
}=ArrayPrototypeShift(state.queue);
167-
state.queueTotalSize=MathMax(0,state.queueTotalSize-size);
299+
constqueue=state.queue;
300+
assert(queue.length);
301+
constvalue=queue.shiftPair();
302+
state.queueTotalSize=MathMax(0,state.queueTotalSize-queue.dequeuedSize);
168303
returnvalue;
169304
}
170305

171306
functionresetQueue(controller){
172307
conststate=controller[kState];
173-
state.queue=[];
308+
state.queue=kEmptyQueue;
174309
state.queueTotalSize=0;
175310
}
176311

177312
functionpeekQueueValue(controller){
178313
conststate=controller[kState];
179314
assert(state.queue.length);
180-
returnstate.queue[0].value;
315+
returnstate.queue.peekPairValue();
181316
}
182317

183318
functionenqueueValueWithSize(controller,value,size){
@@ -188,7 +323,7 @@ function enqueueValueWithSize(controller, value, size) {
188323
coercedSize===Infinity){
189324
thrownewERR_INVALID_ARG_VALUE.RangeError('size',size);
190325
}
191-
ArrayPrototypePush(state.queue,{value,size: coercedSize});
326+
materializeQueue(state).pushPair(value,coercedSize);
192327
state.queueTotalSize+=coercedSize;
193328
}
194329

@@ -284,9 +419,11 @@ module.exports = {
284419
getNonWritablePropertyDescriptor,
285420
isBrandCheck,
286421
isPromisePending,
422+
kEmptyQueue,
287423
kState,
288424
kType,
289425
lazyTransfer,
426+
materializeQueue,
290427
nonOpCancel,
291428
nonOpFlush,
292429
nonOpPull,

‎lib/internal/webstreams/writablestream.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ const {
6767
getNonWritablePropertyDescriptor,
6868
isBrandCheck,
6969
isPromisePending,
70+
kEmptyQueue,
7071
kState,
7172
kType,
7273
lazyTransfer,
@@ -1177,6 +1178,11 @@ function writableStreamDefaultControllerGetChunkSize(controller, chunk) {
11771178
return1;
11781179
}
11791180

1181+
// The internal default size algorithm is never observable by user
1182+
// code, always returns 1, and cannot throw: skip the call.
1183+
if(sizeAlgorithm===defaultSizeAlgorithm)
1184+
return1;
1185+
11801186
try{
11811187
returnFunctionPrototypeCall(
11821188
sizeAlgorithm,
@@ -1293,7 +1299,7 @@ function setupWritableStreamDefaultController(
12931299
abortAlgorithm,
12941300
closeAlgorithm,
12951301
highWaterMark,
1296-
queue: [],
1302+
queue: kEmptyQueue,
12971303
queueTotalSize: 0,
12981304
abortController: newAbortController(),
12991305
sizeAlgorithm,

0 commit comments

Comments
 (0)