Commit 3043b2a

Browse files
committed
stream: speed up async iteration over WHATWG byte streams
for await / reader.read() loops over byte streams were ~4x slower than over default streams. Three per-chunk costs, none required by the spec: - ArrayBufferViewGetBuffer/ByteLength/ByteOffset went through ReflectGet(view.constructor.prototype, ...), a reflective get that is ~3.5x slower than the original prototype getters from primordials and spoofable through a user-defined .constructor to boot. - The buffered fast paths in ReadableStreamDefaultReader.read() and the async iterator only covered default controllers, so byte streams with queued data still allocated a read request and PromiseWithResolvers per chunk. Byte-queue dequeue is fully synchronous (it is the queue-filled arm of the byte controller's pull steps), so both fast paths now resolve directly from the byte queue. - readableByteStreamControllerEnqueue re-ran the reader brand check and re-loaded the read request list four times per chunk across HasDefaultReader / ProcessReadRequestsUsingQueue / GetNumReadRequests / FulfillReadRequest; it now does a single pass. The async iterator also reuses its read request object across reads (at most one is ever in flight). benchmark/webstreams interleaved same-day A/B, --runs 10: readable-async-iterator bytes +16.3% (***), readable-read byob +9.1% (***), all other rows neutral. Profiler harness: parked byte iteration +14%, buffered byte iteration +37%, buffered byte read loop +18%, default-stream rows at parity. WPT streams/compression/encoding subtests identical to baseline. Signed-off-by: Matteo Collina <hello@matteocollina.com> PR-URL: #64291 Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 9445e27 commit 3043b2a

2 files changed

Lines changed: 118 additions & 51 deletions

File tree

β€Žlib/internal/webstreams/readablestream.jsβ€Ž

Lines changed: 93 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,12 @@ class ReadableStream {
497497
current: undefined,
498498
};
499499
letstarted=false;
500+
// A single reusable read request: at most one read is ever in flight
501+
// (next() chains through state.current), and the request is consumed
502+
// before the next read starts, so only its promise record changes
503+
// per read.
504+
// eslint-disable-next-line no-use-before-define
505+
constreadRequest=newReadableStreamAsyncIteratorReadRequest(reader,state,undefined);
500506

501507
// The nextSteps function is not an async function in order
502508
// to make it more efficient. Because nextSteps explicitly
@@ -515,8 +521,8 @@ class ReadableStream {
515521
}
516522
constpromise=PromiseWithResolvers();
517523

518-
// eslint-disable-next-line no-use-before-define
519-
readableStreamDefaultReaderRead(reader,newReadableStreamAsyncIteratorReadRequest(reader,state,promise));
524+
readRequest.promise=promise;
525+
readableStreamDefaultReaderRead(reader,readRequest);
520526
returnpromise.promise;
521527
}
522528

@@ -570,28 +576,38 @@ class ReadableStream {
570576
}
571577
// No read is in flight. Mirror the buffered fast path of
572578
// ReadableStreamDefaultReader.read(): when data is already queued
573-
// in a default controller, resolve immediately without allocating
574-
// a read request. The result settles synchronously, so leaving
579+
// in the controller, resolve immediately without allocating a
580+
// read request. The result settles synchronously, so leaving
575581
// state.current undefined matches the state the slow path reaches
576582
// once its read request callbacks have settled.
577583
conststream=reader[kState].stream;
578-
if(!state.done&&stream!==undefined){
584+
if(!state.done&&stream!==undefined&&
585+
stream[kState].state==='readable'){
579586
constcontroller=stream[kState].controller;
580-
if(stream[kState].state==='readable'&&
581-
isReadableStreamDefaultController(controller)&&
582-
controller[kState].queue.length>0){
583-
stream[kState].disturbed=true;
584-
constchunk=dequeueValue(controller);
585-
586-
if(controller[kState].closeRequested&&
587-
!controller[kState].queue.length){
588-
readableStreamDefaultControllerClearAlgorithms(controller);
589-
readableStreamClose(stream);
590-
}else{
591-
readableStreamDefaultControllerCallPullIfNeeded(controller);
587+
if(isReadableStreamDefaultController(controller)){
588+
if(controller[kState].queue.length>0){
589+
stream[kState].disturbed=true;
590+
constchunk=dequeueValue(controller);
591+
592+
if(controller[kState].closeRequested&&
593+
!controller[kState].queue.length){
594+
readableStreamDefaultControllerClearAlgorithms(controller);
595+
readableStreamClose(stream);
596+
}else{
597+
readableStreamDefaultControllerCallPullIfNeeded(controller);
598+
}
599+
600+
returnPromiseResolve({done: false,value: chunk});
592601
}
602+
}elseif(controller[kState].queueTotalSize>0){
603+
// Byte controller with buffered data: same shape as above via
604+
// the queue-filled arm of the byte controller's pull steps.
605+
stream[kState].disturbed=true;
606+
returnPromiseResolve({
607+
done: false,
593608

594-
returnPromiseResolve({done: false,value: chunk});
609+
value: readableByteStreamControllerDequeueChunk(controller),
610+
});
595611
}
596612
}
597613
state.current=nextSteps();
@@ -914,24 +930,36 @@ class ReadableStreamDefaultReader {
914930
conststream=this[kState].stream;
915931
constcontroller=stream[kState].controller;
916932

917-
// Fast path: if data is already buffered in a default controller,
933+
// Fast path: if data is already buffered in the controller's queue,
918934
// return a resolved promise immediately without creating a read request.
919935
// This is spec-compliant because read() returns a Promise, and
920936
// Promise.resolve() callbacks still run in the microtask queue.
921-
if(stream[kState].state==='readable'&&
922-
isReadableStreamDefaultController(controller)&&
923-
controller[kState].queue.length>0){
924-
stream[kState].disturbed=true;
925-
constchunk=dequeueValue(controller);
937+
if(stream[kState].state==='readable'){
938+
if(isReadableStreamDefaultController(controller)){
939+
if(controller[kState].queue.length>0){
940+
stream[kState].disturbed=true;
941+
constchunk=dequeueValue(controller);
942+
943+
if(controller[kState].closeRequested&&!controller[kState].queue.length){
944+
readableStreamDefaultControllerClearAlgorithms(controller);
945+
readableStreamClose(stream);
946+
}else{
947+
readableStreamDefaultControllerCallPullIfNeeded(controller);
948+
}
926949

927-
if(controller[kState].closeRequested&&!controller[kState].queue.length){
928-
readableStreamDefaultControllerClearAlgorithms(controller);
929-
readableStreamClose(stream);
930-
}else{
931-
readableStreamDefaultControllerCallPullIfNeeded(controller);
950+
returnPromiseResolve({done: false,value: chunk});
951+
}
952+
}elseif(controller[kState].queueTotalSize>0){
953+
// Byte controller with buffered data: mirror the queue-filled arm
954+
// of its pull steps (which never consults pendingPullIntos) minus
955+
// the read request.
956+
stream[kState].disturbed=true;
957+
returnPromiseResolve({
958+
done: false,
959+
960+
value: readableByteStreamControllerDequeueChunk(controller),
961+
});
932962
}
933-
934-
returnPromiseResolve({value: chunk,done: false});
935963
}
936964

937965
// Slow path: create request and go through normal flow
@@ -3040,9 +3068,23 @@ function readableByteStreamControllerEnqueue(controller, chunk) {
30403068
}
30413069
}
30423070

3043-
if(readableStreamHasDefaultReader(stream)){
3044-
readableByteStreamControllerProcessReadRequestsUsingQueue(controller);
3045-
if(!readableStreamGetNumReadRequests(stream)){
3071+
// Single consolidated pass over the reader state. The spec routes this
3072+
// through HasDefaultReader / ProcessReadRequestsUsingQueue /
3073+
// GetNumReadRequests / FulfillReadRequest, which would re-run the same
3074+
// reader brand check and re-load the read request list four times on
3075+
// this per-chunk path.
3076+
const{ reader }=stream[kState];
3077+
if(reader!==undefined&&
3078+
reader[kState]!==undefined&&
3079+
reader[kType]==='ReadableStreamDefaultReader'){
3080+
const{ readRequests }=reader[kState];
3081+
if(readRequests.length&&controller[kState].queueTotalSize>0){
3082+
// Only possible when data was enqueued while the stream was not
3083+
// being read; read requests otherwise never coexist with a
3084+
// non-empty queue.
3085+
readableByteStreamControllerProcessReadRequestsUsingQueue(controller);
3086+
}
3087+
if(!readRequests.length){
30463088
readableByteStreamControllerEnqueueChunkToQueue(
30473089
controller,
30483090
transferredBuffer,
@@ -3056,7 +3098,8 @@ function readableByteStreamControllerEnqueue(controller, chunk) {
30563098
}
30573099
consttransferredView=
30583100
newUint8Array(transferredBuffer,byteOffset,byteLength);
3059-
readableStreamFulfillReadRequest(stream,transferredView,false);
3101+
constreadRequest=ArrayPrototypeShift(readRequests);
3102+
readRequest[kChunk](transferredView);
30603103
}
30613104
}elseif(readableStreamHasBYOBReader(stream)){
30623105
readableByteStreamControllerEnqueueChunkToQueue(
@@ -3391,22 +3434,28 @@ function readableByteStreamControllerCancelSteps(controller, reason) {
33913434
returnresult;
33923435
}
33933436

3394-
functionreadableByteStreamControllerFillReadRequestFromQueue(controller,readRequest){
3395-
const{
3396-
queue,
3397-
queueTotalSize,
3398-
}=controller[kState];
3399-
assert(queueTotalSize>0);
3437+
// Dequeues the first chunk of the byte queue as a Uint8Array view,
3438+
// handling queue drain (close-on-empty or pull) before the view is
3439+
// created. This is the [[queueTotalSize]] > 0 arm of the byte
3440+
// controller's pull steps; it is also called directly from the
3441+
// buffered fast paths in ReadableStreamDefaultReader.read() and the
3442+
// async iterator, which resolve with the view without allocating a
3443+
// read request.
3444+
functionreadableByteStreamControllerDequeueChunk(controller){
3445+
assert(controller[kState].queueTotalSize>0);
34003446
const{
34013447
buffer,
34023448
byteOffset,
34033449
byteLength,
3404-
}=ArrayPrototypeShift(queue);
3450+
}=ArrayPrototypeShift(controller[kState].queue);
34053451

34063452
controller[kState].queueTotalSize-=byteLength;
34073453
readableByteStreamControllerHandleQueueDrain(controller);
3408-
constview=newUint8Array(buffer,byteOffset,byteLength);
3409-
readRequest[kChunk](view);
3454+
returnnewUint8Array(buffer,byteOffset,byteLength);
3455+
}
3456+
3457+
functionreadableByteStreamControllerFillReadRequestFromQueue(controller,readRequest){
3458+
readRequest[kChunk](readableByteStreamControllerDequeueChunk(controller));
34103459
}
34113460

34123461
functionreadableByteStreamControllerProcessReadRequestsUsingQueue(controller){

β€Žlib/internal/webstreams/util.jsβ€Ž

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ const {
77
ArrayPrototypePush,
88
ArrayPrototypeShift,
99
AsyncIteratorPrototype,
10+
DataViewPrototypeGetBuffer,
11+
DataViewPrototypeGetByteLength,
12+
DataViewPrototypeGetByteOffset,
1013
FunctionPrototypeCall,
1114
MathMax,
1215
NumberIsNaN,
1316
PromisePrototypeThen,
1417
PromiseReject,
1518
PromiseResolve,
16-
ReflectGet,
1719
Symbol,
20+
TypedArrayPrototypeGetBuffer,
21+
TypedArrayPrototypeGetByteLength,
22+
TypedArrayPrototypeGetByteOffset,
1823
Uint8Array,
1924
}=primordials;
2025

@@ -41,6 +46,10 @@ const {
4146

4247
constassert=require('internal/assert');
4348

49+
const{
50+
isDataView,
51+
}=require('internal/util/types');
52+
4453
const{
4554
validateFunction,
4655
}=require('internal/validators');
@@ -93,20 +102,29 @@ function customInspect(depth, options, name, data) {
93102
return`${name}${inspect(data,opts)}`;
94103
}
95104

96-
// These are defensive to work around the possibility that
97-
// the buffer, byteLength, and byteOffset properties on
98-
// ArrayBuffer and ArrayBufferView's may have been tampered with.
105+
// These use the original prototype getters so that user tampering with
106+
// the buffer, byteLength, and byteOffset properties on ArrayBuffer and
107+
// ArrayBufferView's is not observed. They run once or more per chunk on
108+
// every byte-stream path, so they must not go through a reflective get
109+
// (the previous view.constructor.prototype lookup was both slower and
110+
// spoofable via a user-defined .constructor).
99111

100112
functionArrayBufferViewGetBuffer(view){
101-
returnReflectGet(view.constructor.prototype,'buffer',view);
113+
returnisDataView(view) ?
114+
DataViewPrototypeGetBuffer(view) :
115+
TypedArrayPrototypeGetBuffer(view);
102116
}
103117

104118
functionArrayBufferViewGetByteLength(view){
105-
returnReflectGet(view.constructor.prototype,'byteLength',view);
119+
returnisDataView(view) ?
120+
DataViewPrototypeGetByteLength(view) :
121+
TypedArrayPrototypeGetByteLength(view);
106122
}
107123

108124
functionArrayBufferViewGetByteOffset(view){
109-
returnReflectGet(view.constructor.prototype,'byteOffset',view);
125+
returnisDataView(view) ?
126+
DataViewPrototypeGetByteOffset(view) :
127+
TypedArrayPrototypeGetByteOffset(view);
110128
}
111129

112130
functioncloneAsUint8Array(view){

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 3043b2a

Browse files
committed
stream: speed up async iteration over WHATWG byte streams
for await / reader.read() loops over byte streams were ~4x slower than over default streams. Three per-chunk costs, none required by the spec: - ArrayBufferViewGetBuffer/ByteLength/ByteOffset went through ReflectGet(view.constructor.prototype, ...), a reflective get that is ~3.5x slower than the original prototype getters from primordials and spoofable through a user-defined .constructor to boot. - The buffered fast paths in ReadableStreamDefaultReader.read() and the async iterator only covered default controllers, so byte streams with queued data still allocated a read request and PromiseWithResolvers per chunk. Byte-queue dequeue is fully synchronous (it is the queue-filled arm of the byte controller's pull steps), so both fast paths now resolve directly from the byte queue. - readableByteStreamControllerEnqueue re-ran the reader brand check and re-loaded the read request list four times per chunk across HasDefaultReader / ProcessReadRequestsUsingQueue / GetNumReadRequests / FulfillReadRequest; it now does a single pass. The async iterator also reuses its read request object across reads (at most one is ever in flight). benchmark/webstreams interleaved same-day A/B, --runs 10: readable-async-iterator bytes +16.3% (***), readable-read byob +9.1% (***), all other rows neutral. Profiler harness: parked byte iteration +14%, buffered byte iteration +37%, buffered byte read loop +18%, default-stream rows at parity. WPT streams/compression/encoding subtests identical to baseline. Signed-off-by: Matteo Collina <hello@matteocollina.com> PR-URL: #64291 Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 9445e27 commit 3043b2a

2 files changed

Lines changed: 118 additions & 51 deletions

File tree

β€Žlib/internal/webstreams/readablestream.jsβ€Ž

Lines changed: 93 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,12 @@ class ReadableStream {
497497
current: undefined,
498498
};
499499
letstarted=false;
500+
// A single reusable read request: at most one read is ever in flight
501+
// (next() chains through state.current), and the request is consumed
502+
// before the next read starts, so only its promise record changes
503+
// per read.
504+
// eslint-disable-next-line no-use-before-define
505+
constreadRequest=newReadableStreamAsyncIteratorReadRequest(reader,state,undefined);
500506

501507
// The nextSteps function is not an async function in order
502508
// to make it more efficient. Because nextSteps explicitly
@@ -515,8 +521,8 @@ class ReadableStream {
515521
}
516522
constpromise=PromiseWithResolvers();
517523

518-
// eslint-disable-next-line no-use-before-define
519-
readableStreamDefaultReaderRead(reader,newReadableStreamAsyncIteratorReadRequest(reader,state,promise));
524+
readRequest.promise=promise;
525+
readableStreamDefaultReaderRead(reader,readRequest);
520526
returnpromise.promise;
521527
}
522528

@@ -570,28 +576,38 @@ class ReadableStream {
570576
}
571577
// No read is in flight. Mirror the buffered fast path of
572578
// ReadableStreamDefaultReader.read(): when data is already queued
573-
// in a default controller, resolve immediately without allocating
574-
// a read request. The result settles synchronously, so leaving
579+
// in the controller, resolve immediately without allocating a
580+
// read request. The result settles synchronously, so leaving
575581
// state.current undefined matches the state the slow path reaches
576582
// once its read request callbacks have settled.
577583
conststream=reader[kState].stream;
578-
if(!state.done&&stream!==undefined){
584+
if(!state.done&&stream!==undefined&&
585+
stream[kState].state==='readable'){
579586
constcontroller=stream[kState].controller;
580-
if(stream[kState].state==='readable'&&
581-
isReadableStreamDefaultController(controller)&&
582-
controller[kState].queue.length>0){
583-
stream[kState].disturbed=true;
584-
constchunk=dequeueValue(controller);
585-
586-
if(controller[kState].closeRequested&&
587-
!controller[kState].queue.length){
588-
readableStreamDefaultControllerClearAlgorithms(controller);
589-
readableStreamClose(stream);
590-
}else{
591-
readableStreamDefaultControllerCallPullIfNeeded(controller);
587+
if(isReadableStreamDefaultController(controller)){
588+
if(controller[kState].queue.length>0){
589+
stream[kState].disturbed=true;
590+
constchunk=dequeueValue(controller);
591+
592+
if(controller[kState].closeRequested&&
593+
!controller[kState].queue.length){
594+
readableStreamDefaultControllerClearAlgorithms(controller);
595+
readableStreamClose(stream);
596+
}else{
597+
readableStreamDefaultControllerCallPullIfNeeded(controller);
598+
}
599+
600+
returnPromiseResolve({done: false,value: chunk});
592601
}
602+
}elseif(controller[kState].queueTotalSize>0){
603+
// Byte controller with buffered data: same shape as above via
604+
// the queue-filled arm of the byte controller's pull steps.
605+
stream[kState].disturbed=true;
606+
returnPromiseResolve({
607+
done: false,
593608

594-
returnPromiseResolve({done: false,value: chunk});
609+
value: readableByteStreamControllerDequeueChunk(controller),
610+
});
595611
}
596612
}
597613
state.current=nextSteps();
@@ -914,24 +930,36 @@ class ReadableStreamDefaultReader {
914930
conststream=this[kState].stream;
915931
constcontroller=stream[kState].controller;
916932

917-
// Fast path: if data is already buffered in a default controller,
933+
// Fast path: if data is already buffered in the controller's queue,
918934
// return a resolved promise immediately without creating a read request.
919935
// This is spec-compliant because read() returns a Promise, and
920936
// Promise.resolve() callbacks still run in the microtask queue.
921-
if(stream[kState].state==='readable'&&
922-
isReadableStreamDefaultController(controller)&&
923-
controller[kState].queue.length>0){
924-
stream[kState].disturbed=true;
925-
constchunk=dequeueValue(controller);
937+
if(stream[kState].state==='readable'){
938+
if(isReadableStreamDefaultController(controller)){
939+
if(controller[kState].queue.length>0){
940+
stream[kState].disturbed=true;
941+
constchunk=dequeueValue(controller);
942+
943+
if(controller[kState].closeRequested&&!controller[kState].queue.length){
944+
readableStreamDefaultControllerClearAlgorithms(controller);
945+
readableStreamClose(stream);
946+
}else{
947+
readableStreamDefaultControllerCallPullIfNeeded(controller);
948+
}
926949

927-
if(controller[kState].closeRequested&&!controller[kState].queue.length){
928-
readableStreamDefaultControllerClearAlgorithms(controller);
929-
readableStreamClose(stream);
930-
}else{
931-
readableStreamDefaultControllerCallPullIfNeeded(controller);
950+
returnPromiseResolve({done: false,value: chunk});
951+
}
952+
}elseif(controller[kState].queueTotalSize>0){
953+
// Byte controller with buffered data: mirror the queue-filled arm
954+
// of its pull steps (which never consults pendingPullIntos) minus
955+
// the read request.
956+
stream[kState].disturbed=true;
957+
returnPromiseResolve({
958+
done: false,
959+
960+
value: readableByteStreamControllerDequeueChunk(controller),
961+
});
932962
}
933-
934-
returnPromiseResolve({value: chunk,done: false});
935963
}
936964

937965
// Slow path: create request and go through normal flow
@@ -3040,9 +3068,23 @@ function readableByteStreamControllerEnqueue(controller, chunk) {
30403068
}
30413069
}
30423070

3043-
if(readableStreamHasDefaultReader(stream)){
3044-
readableByteStreamControllerProcessReadRequestsUsingQueue(controller);
3045-
if(!readableStreamGetNumReadRequests(stream)){
3071+
// Single consolidated pass over the reader state. The spec routes this
3072+
// through HasDefaultReader / ProcessReadRequestsUsingQueue /
3073+
// GetNumReadRequests / FulfillReadRequest, which would re-run the same
3074+
// reader brand check and re-load the read request list four times on
3075+
// this per-chunk path.
3076+
const{ reader }=stream[kState];
3077+
if(reader!==undefined&&
3078+
reader[kState]!==undefined&&
3079+
reader[kType]==='ReadableStreamDefaultReader'){
3080+
const{ readRequests }=reader[kState];
3081+
if(readRequests.length&&controller[kState].queueTotalSize>0){
3082+
// Only possible when data was enqueued while the stream was not
3083+
// being read; read requests otherwise never coexist with a
3084+
// non-empty queue.
3085+
readableByteStreamControllerProcessReadRequestsUsingQueue(controller);
3086+
}
3087+
if(!readRequests.length){
30463088
readableByteStreamControllerEnqueueChunkToQueue(
30473089
controller,
30483090
transferredBuffer,
@@ -3056,7 +3098,8 @@ function readableByteStreamControllerEnqueue(controller, chunk) {
30563098
}
30573099
consttransferredView=
30583100
newUint8Array(transferredBuffer,byteOffset,byteLength);
3059-
readableStreamFulfillReadRequest(stream,transferredView,false);
3101+
constreadRequest=ArrayPrototypeShift(readRequests);
3102+
readRequest[kChunk](transferredView);
30603103
}
30613104
}elseif(readableStreamHasBYOBReader(stream)){
30623105
readableByteStreamControllerEnqueueChunkToQueue(
@@ -3391,22 +3434,28 @@ function readableByteStreamControllerCancelSteps(controller, reason) {
33913434
returnresult;
33923435
}
33933436

3394-
functionreadableByteStreamControllerFillReadRequestFromQueue(controller,readRequest){
3395-
const{
3396-
queue,
3397-
queueTotalSize,
3398-
}=controller[kState];
3399-
assert(queueTotalSize>0);
3437+
// Dequeues the first chunk of the byte queue as a Uint8Array view,
3438+
// handling queue drain (close-on-empty or pull) before the view is
3439+
// created. This is the [[queueTotalSize]] > 0 arm of the byte
3440+
// controller's pull steps; it is also called directly from the
3441+
// buffered fast paths in ReadableStreamDefaultReader.read() and the
3442+
// async iterator, which resolve with the view without allocating a
3443+
// read request.
3444+
functionreadableByteStreamControllerDequeueChunk(controller){
3445+
assert(controller[kState].queueTotalSize>0);
34003446
const{
34013447
buffer,
34023448
byteOffset,
34033449
byteLength,
3404-
}=ArrayPrototypeShift(queue);
3450+
}=ArrayPrototypeShift(controller[kState].queue);
34053451

34063452
controller[kState].queueTotalSize-=byteLength;
34073453
readableByteStreamControllerHandleQueueDrain(controller);
3408-
constview=newUint8Array(buffer,byteOffset,byteLength);
3409-
readRequest[kChunk](view);
3454+
returnnewUint8Array(buffer,byteOffset,byteLength);
3455+
}
3456+
3457+
functionreadableByteStreamControllerFillReadRequestFromQueue(controller,readRequest){
3458+
readRequest[kChunk](readableByteStreamControllerDequeueChunk(controller));
34103459
}
34113460

34123461
functionreadableByteStreamControllerProcessReadRequestsUsingQueue(controller){

β€Žlib/internal/webstreams/util.jsβ€Ž

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ const {
77
ArrayPrototypePush,
88
ArrayPrototypeShift,
99
AsyncIteratorPrototype,
10+
DataViewPrototypeGetBuffer,
11+
DataViewPrototypeGetByteLength,
12+
DataViewPrototypeGetByteOffset,
1013
FunctionPrototypeCall,
1114
MathMax,
1215
NumberIsNaN,
1316
PromisePrototypeThen,
1417
PromiseReject,
1518
PromiseResolve,
16-
ReflectGet,
1719
Symbol,
20+
TypedArrayPrototypeGetBuffer,
21+
TypedArrayPrototypeGetByteLength,
22+
TypedArrayPrototypeGetByteOffset,
1823
Uint8Array,
1924
}=primordials;
2025

@@ -41,6 +46,10 @@ const {
4146

4247
constassert=require('internal/assert');
4348

49+
const{
50+
isDataView,
51+
}=require('internal/util/types');
52+
4453
const{
4554
validateFunction,
4655
}=require('internal/validators');
@@ -93,20 +102,29 @@ function customInspect(depth, options, name, data) {
93102
return`${name}${inspect(data,opts)}`;
94103
}
95104

96-
// These are defensive to work around the possibility that
97-
// the buffer, byteLength, and byteOffset properties on
98-
// ArrayBuffer and ArrayBufferView's may have been tampered with.
105+
// These use the original prototype getters so that user tampering with
106+
// the buffer, byteLength, and byteOffset properties on ArrayBuffer and
107+
// ArrayBufferView's is not observed. They run once or more per chunk on
108+
// every byte-stream path, so they must not go through a reflective get
109+
// (the previous view.constructor.prototype lookup was both slower and
110+
// spoofable via a user-defined .constructor).
99111

100112
functionArrayBufferViewGetBuffer(view){
101-
returnReflectGet(view.constructor.prototype,'buffer',view);
113+
returnisDataView(view) ?
114+
DataViewPrototypeGetBuffer(view) :
115+
TypedArrayPrototypeGetBuffer(view);
102116
}
103117

104118
functionArrayBufferViewGetByteLength(view){
105-
returnReflectGet(view.constructor.prototype,'byteLength',view);
119+
returnisDataView(view) ?
120+
DataViewPrototypeGetByteLength(view) :
121+
TypedArrayPrototypeGetByteLength(view);
106122
}
107123

108124
functionArrayBufferViewGetByteOffset(view){
109-
returnReflectGet(view.constructor.prototype,'byteOffset',view);
125+
returnisDataView(view) ?
126+
DataViewPrototypeGetByteOffset(view) :
127+
TypedArrayPrototypeGetByteOffset(view);
110128
}
111129

112130
functioncloneAsUint8Array(view){

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 3043b2a

Browse files
committed
stream: speed up async iteration over WHATWG byte streams
for await / reader.read() loops over byte streams were ~4x slower than over default streams. Three per-chunk costs, none required by the spec: - ArrayBufferViewGetBuffer/ByteLength/ByteOffset went through ReflectGet(view.constructor.prototype, ...), a reflective get that is ~3.5x slower than the original prototype getters from primordials and spoofable through a user-defined .constructor to boot. - The buffered fast paths in ReadableStreamDefaultReader.read() and the async iterator only covered default controllers, so byte streams with queued data still allocated a read request and PromiseWithResolvers per chunk. Byte-queue dequeue is fully synchronous (it is the queue-filled arm of the byte controller's pull steps), so both fast paths now resolve directly from the byte queue. - readableByteStreamControllerEnqueue re-ran the reader brand check and re-loaded the read request list four times per chunk across HasDefaultReader / ProcessReadRequestsUsingQueue / GetNumReadRequests / FulfillReadRequest; it now does a single pass. The async iterator also reuses its read request object across reads (at most one is ever in flight). benchmark/webstreams interleaved same-day A/B, --runs 10: readable-async-iterator bytes +16.3% (***), readable-read byob +9.1% (***), all other rows neutral. Profiler harness: parked byte iteration +14%, buffered byte iteration +37%, buffered byte read loop +18%, default-stream rows at parity. WPT streams/compression/encoding subtests identical to baseline. Signed-off-by: Matteo Collina <hello@matteocollina.com> PR-URL: #64291 Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 9445e27 commit 3043b2a

2 files changed

Lines changed: 118 additions & 51 deletions

File tree

β€Žlib/internal/webstreams/readablestream.jsβ€Ž

Lines changed: 93 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,12 @@ class ReadableStream {
497497
current: undefined,
498498
};
499499
letstarted=false;
500+
// A single reusable read request: at most one read is ever in flight
501+
// (next() chains through state.current), and the request is consumed
502+
// before the next read starts, so only its promise record changes
503+
// per read.
504+
// eslint-disable-next-line no-use-before-define
505+
constreadRequest=newReadableStreamAsyncIteratorReadRequest(reader,state,undefined);
500506

501507
// The nextSteps function is not an async function in order
502508
// to make it more efficient. Because nextSteps explicitly
@@ -515,8 +521,8 @@ class ReadableStream {
515521
}
516522
constpromise=PromiseWithResolvers();
517523

518-
// eslint-disable-next-line no-use-before-define
519-
readableStreamDefaultReaderRead(reader,newReadableStreamAsyncIteratorReadRequest(reader,state,promise));
524+
readRequest.promise=promise;
525+
readableStreamDefaultReaderRead(reader,readRequest);
520526
returnpromise.promise;
521527
}
522528

@@ -570,28 +576,38 @@ class ReadableStream {
570576
}
571577
// No read is in flight. Mirror the buffered fast path of
572578
// ReadableStreamDefaultReader.read(): when data is already queued
573-
// in a default controller, resolve immediately without allocating
574-
// a read request. The result settles synchronously, so leaving
579+
// in the controller, resolve immediately without allocating a
580+
// read request. The result settles synchronously, so leaving
575581
// state.current undefined matches the state the slow path reaches
576582
// once its read request callbacks have settled.
577583
conststream=reader[kState].stream;
578-
if(!state.done&&stream!==undefined){
584+
if(!state.done&&stream!==undefined&&
585+
stream[kState].state==='readable'){
579586
constcontroller=stream[kState].controller;
580-
if(stream[kState].state==='readable'&&
581-
isReadableStreamDefaultController(controller)&&
582-
controller[kState].queue.length>0){
583-
stream[kState].disturbed=true;
584-
constchunk=dequeueValue(controller);
585-
586-
if(controller[kState].closeRequested&&
587-
!controller[kState].queue.length){
588-
readableStreamDefaultControllerClearAlgorithms(controller);
589-
readableStreamClose(stream);
590-
}else{
591-
readableStreamDefaultControllerCallPullIfNeeded(controller);
587+
if(isReadableStreamDefaultController(controller)){
588+
if(controller[kState].queue.length>0){
589+
stream[kState].disturbed=true;
590+
constchunk=dequeueValue(controller);
591+
592+
if(controller[kState].closeRequested&&
593+
!controller[kState].queue.length){
594+
readableStreamDefaultControllerClearAlgorithms(controller);
595+
readableStreamClose(stream);
596+
}else{
597+
readableStreamDefaultControllerCallPullIfNeeded(controller);
598+
}
599+
600+
returnPromiseResolve({done: false,value: chunk});
592601
}
602+
}elseif(controller[kState].queueTotalSize>0){
603+
// Byte controller with buffered data: same shape as above via
604+
// the queue-filled arm of the byte controller's pull steps.
605+
stream[kState].disturbed=true;
606+
returnPromiseResolve({
607+
done: false,
593608

594-
returnPromiseResolve({done: false,value: chunk});
609+
value: readableByteStreamControllerDequeueChunk(controller),
610+
});
595611
}
596612
}
597613
state.current=nextSteps();
@@ -914,24 +930,36 @@ class ReadableStreamDefaultReader {
914930
conststream=this[kState].stream;
915931
constcontroller=stream[kState].controller;
916932

917-
// Fast path: if data is already buffered in a default controller,
933+
// Fast path: if data is already buffered in the controller's queue,
918934
// return a resolved promise immediately without creating a read request.
919935
// This is spec-compliant because read() returns a Promise, and
920936
// Promise.resolve() callbacks still run in the microtask queue.
921-
if(stream[kState].state==='readable'&&
922-
isReadableStreamDefaultController(controller)&&
923-
controller[kState].queue.length>0){
924-
stream[kState].disturbed=true;
925-
constchunk=dequeueValue(controller);
937+
if(stream[kState].state==='readable'){
938+
if(isReadableStreamDefaultController(controller)){
939+
if(controller[kState].queue.length>0){
940+
stream[kState].disturbed=true;
941+
constchunk=dequeueValue(controller);
942+
943+
if(controller[kState].closeRequested&&!controller[kState].queue.length){
944+
readableStreamDefaultControllerClearAlgorithms(controller);
945+
readableStreamClose(stream);
946+
}else{
947+
readableStreamDefaultControllerCallPullIfNeeded(controller);
948+
}
926949

927-
if(controller[kState].closeRequested&&!controller[kState].queue.length){
928-
readableStreamDefaultControllerClearAlgorithms(controller);
929-
readableStreamClose(stream);
930-
}else{
931-
readableStreamDefaultControllerCallPullIfNeeded(controller);
950+
returnPromiseResolve({done: false,value: chunk});
951+
}
952+
}elseif(controller[kState].queueTotalSize>0){
953+
// Byte controller with buffered data: mirror the queue-filled arm
954+
// of its pull steps (which never consults pendingPullIntos) minus
955+
// the read request.
956+
stream[kState].disturbed=true;
957+
returnPromiseResolve({
958+
done: false,
959+
960+
value: readableByteStreamControllerDequeueChunk(controller),
961+
});
932962
}
933-
934-
returnPromiseResolve({value: chunk,done: false});
935963
}
936964

937965
// Slow path: create request and go through normal flow
@@ -3040,9 +3068,23 @@ function readableByteStreamControllerEnqueue(controller, chunk) {
30403068
}
30413069
}
30423070

3043-
if(readableStreamHasDefaultReader(stream)){
3044-
readableByteStreamControllerProcessReadRequestsUsingQueue(controller);
3045-
if(!readableStreamGetNumReadRequests(stream)){
3071+
// Single consolidated pass over the reader state. The spec routes this
3072+
// through HasDefaultReader / ProcessReadRequestsUsingQueue /
3073+
// GetNumReadRequests / FulfillReadRequest, which would re-run the same
3074+
// reader brand check and re-load the read request list four times on
3075+
// this per-chunk path.
3076+
const{ reader }=stream[kState];
3077+
if(reader!==undefined&&
3078+
reader[kState]!==undefined&&
3079+
reader[kType]==='ReadableStreamDefaultReader'){
3080+
const{ readRequests }=reader[kState];
3081+
if(readRequests.length&&controller[kState].queueTotalSize>0){
3082+
// Only possible when data was enqueued while the stream was not
3083+
// being read; read requests otherwise never coexist with a
3084+
// non-empty queue.
3085+
readableByteStreamControllerProcessReadRequestsUsingQueue(controller);
3086+
}
3087+
if(!readRequests.length){
30463088
readableByteStreamControllerEnqueueChunkToQueue(
30473089
controller,
30483090
transferredBuffer,
@@ -3056,7 +3098,8 @@ function readableByteStreamControllerEnqueue(controller, chunk) {
30563098
}
30573099
consttransferredView=
30583100
newUint8Array(transferredBuffer,byteOffset,byteLength);
3059-
readableStreamFulfillReadRequest(stream,transferredView,false);
3101+
constreadRequest=ArrayPrototypeShift(readRequests);
3102+
readRequest[kChunk](transferredView);
30603103
}
30613104
}elseif(readableStreamHasBYOBReader(stream)){
30623105
readableByteStreamControllerEnqueueChunkToQueue(
@@ -3391,22 +3434,28 @@ function readableByteStreamControllerCancelSteps(controller, reason) {
33913434
returnresult;
33923435
}
33933436

3394-
functionreadableByteStreamControllerFillReadRequestFromQueue(controller,readRequest){
3395-
const{
3396-
queue,
3397-
queueTotalSize,
3398-
}=controller[kState];
3399-
assert(queueTotalSize>0);
3437+
// Dequeues the first chunk of the byte queue as a Uint8Array view,
3438+
// handling queue drain (close-on-empty or pull) before the view is
3439+
// created. This is the [[queueTotalSize]] > 0 arm of the byte
3440+
// controller's pull steps; it is also called directly from the
3441+
// buffered fast paths in ReadableStreamDefaultReader.read() and the
3442+
// async iterator, which resolve with the view without allocating a
3443+
// read request.
3444+
functionreadableByteStreamControllerDequeueChunk(controller){
3445+
assert(controller[kState].queueTotalSize>0);
34003446
const{
34013447
buffer,
34023448
byteOffset,
34033449
byteLength,
3404-
}=ArrayPrototypeShift(queue);
3450+
}=ArrayPrototypeShift(controller[kState].queue);
34053451

34063452
controller[kState].queueTotalSize-=byteLength;
34073453
readableByteStreamControllerHandleQueueDrain(controller);
3408-
constview=newUint8Array(buffer,byteOffset,byteLength);
3409-
readRequest[kChunk](view);
3454+
returnnewUint8Array(buffer,byteOffset,byteLength);
3455+
}
3456+
3457+
functionreadableByteStreamControllerFillReadRequestFromQueue(controller,readRequest){
3458+
readRequest[kChunk](readableByteStreamControllerDequeueChunk(controller));
34103459
}
34113460

34123461
functionreadableByteStreamControllerProcessReadRequestsUsingQueue(controller){

β€Žlib/internal/webstreams/util.jsβ€Ž

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ const {
77
ArrayPrototypePush,
88
ArrayPrototypeShift,
99
AsyncIteratorPrototype,
10+
DataViewPrototypeGetBuffer,
11+
DataViewPrototypeGetByteLength,
12+
DataViewPrototypeGetByteOffset,
1013
FunctionPrototypeCall,
1114
MathMax,
1215
NumberIsNaN,
1316
PromisePrototypeThen,
1417
PromiseReject,
1518
PromiseResolve,
16-
ReflectGet,
1719
Symbol,
20+
TypedArrayPrototypeGetBuffer,
21+
TypedArrayPrototypeGetByteLength,
22+
TypedArrayPrototypeGetByteOffset,
1823
Uint8Array,
1924
}=primordials;
2025

@@ -41,6 +46,10 @@ const {
4146

4247
constassert=require('internal/assert');
4348

49+
const{
50+
isDataView,
51+
}=require('internal/util/types');
52+
4453
const{
4554
validateFunction,
4655
}=require('internal/validators');
@@ -93,20 +102,29 @@ function customInspect(depth, options, name, data) {
93102
return`${name}${inspect(data,opts)}`;
94103
}
95104

96-
// These are defensive to work around the possibility that
97-
// the buffer, byteLength, and byteOffset properties on
98-
// ArrayBuffer and ArrayBufferView's may have been tampered with.
105+
// These use the original prototype getters so that user tampering with
106+
// the buffer, byteLength, and byteOffset properties on ArrayBuffer and
107+
// ArrayBufferView's is not observed. They run once or more per chunk on
108+
// every byte-stream path, so they must not go through a reflective get
109+
// (the previous view.constructor.prototype lookup was both slower and
110+
// spoofable via a user-defined .constructor).
99111

100112
functionArrayBufferViewGetBuffer(view){
101-
returnReflectGet(view.constructor.prototype,'buffer',view);
113+
returnisDataView(view) ?
114+
DataViewPrototypeGetBuffer(view) :
115+
TypedArrayPrototypeGetBuffer(view);
102116
}
103117

104118
functionArrayBufferViewGetByteLength(view){
105-
returnReflectGet(view.constructor.prototype,'byteLength',view);
119+
returnisDataView(view) ?
120+
DataViewPrototypeGetByteLength(view) :
121+
TypedArrayPrototypeGetByteLength(view);
106122
}
107123

108124
functionArrayBufferViewGetByteOffset(view){
109-
returnReflectGet(view.constructor.prototype,'byteOffset',view);
125+
returnisDataView(view) ?
126+
DataViewPrototypeGetByteOffset(view) :
127+
TypedArrayPrototypeGetByteOffset(view);
110128
}
111129

112130
functioncloneAsUint8Array(view){

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 3043b2a

Browse files
committed
stream: speed up async iteration over WHATWG byte streams
for await / reader.read() loops over byte streams were ~4x slower than over default streams. Three per-chunk costs, none required by the spec: - ArrayBufferViewGetBuffer/ByteLength/ByteOffset went through ReflectGet(view.constructor.prototype, ...), a reflective get that is ~3.5x slower than the original prototype getters from primordials and spoofable through a user-defined .constructor to boot. - The buffered fast paths in ReadableStreamDefaultReader.read() and the async iterator only covered default controllers, so byte streams with queued data still allocated a read request and PromiseWithResolvers per chunk. Byte-queue dequeue is fully synchronous (it is the queue-filled arm of the byte controller's pull steps), so both fast paths now resolve directly from the byte queue. - readableByteStreamControllerEnqueue re-ran the reader brand check and re-loaded the read request list four times per chunk across HasDefaultReader / ProcessReadRequestsUsingQueue / GetNumReadRequests / FulfillReadRequest; it now does a single pass. The async iterator also reuses its read request object across reads (at most one is ever in flight). benchmark/webstreams interleaved same-day A/B, --runs 10: readable-async-iterator bytes +16.3% (***), readable-read byob +9.1% (***), all other rows neutral. Profiler harness: parked byte iteration +14%, buffered byte iteration +37%, buffered byte read loop +18%, default-stream rows at parity. WPT streams/compression/encoding subtests identical to baseline. Signed-off-by: Matteo Collina <hello@matteocollina.com> PR-URL: #64291 Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 9445e27 commit 3043b2a

2 files changed

Lines changed: 118 additions & 51 deletions

File tree

β€Žlib/internal/webstreams/readablestream.jsβ€Ž

Lines changed: 93 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,12 @@ class ReadableStream {
497497
current: undefined,
498498
};
499499
letstarted=false;
500+
// A single reusable read request: at most one read is ever in flight
501+
// (next() chains through state.current), and the request is consumed
502+
// before the next read starts, so only its promise record changes
503+
// per read.
504+
// eslint-disable-next-line no-use-before-define
505+
constreadRequest=newReadableStreamAsyncIteratorReadRequest(reader,state,undefined);
500506

501507
// The nextSteps function is not an async function in order
502508
// to make it more efficient. Because nextSteps explicitly
@@ -515,8 +521,8 @@ class ReadableStream {
515521
}
516522
constpromise=PromiseWithResolvers();
517523

518-
// eslint-disable-next-line no-use-before-define
519-
readableStreamDefaultReaderRead(reader,newReadableStreamAsyncIteratorReadRequest(reader,state,promise));
524+
readRequest.promise=promise;
525+
readableStreamDefaultReaderRead(reader,readRequest);
520526
returnpromise.promise;
521527
}
522528

@@ -570,28 +576,38 @@ class ReadableStream {
570576
}
571577
// No read is in flight. Mirror the buffered fast path of
572578
// ReadableStreamDefaultReader.read(): when data is already queued
573-
// in a default controller, resolve immediately without allocating
574-
// a read request. The result settles synchronously, so leaving
579+
// in the controller, resolve immediately without allocating a
580+
// read request. The result settles synchronously, so leaving
575581
// state.current undefined matches the state the slow path reaches
576582
// once its read request callbacks have settled.
577583
conststream=reader[kState].stream;
578-
if(!state.done&&stream!==undefined){
584+
if(!state.done&&stream!==undefined&&
585+
stream[kState].state==='readable'){
579586
constcontroller=stream[kState].controller;
580-
if(stream[kState].state==='readable'&&
581-
isReadableStreamDefaultController(controller)&&
582-
controller[kState].queue.length>0){
583-
stream[kState].disturbed=true;
584-
constchunk=dequeueValue(controller);
585-
586-
if(controller[kState].closeRequested&&
587-
!controller[kState].queue.length){
588-
readableStreamDefaultControllerClearAlgorithms(controller);
589-
readableStreamClose(stream);
590-
}else{
591-
readableStreamDefaultControllerCallPullIfNeeded(controller);
587+
if(isReadableStreamDefaultController(controller)){
588+
if(controller[kState].queue.length>0){
589+
stream[kState].disturbed=true;
590+
constchunk=dequeueValue(controller);
591+
592+
if(controller[kState].closeRequested&&
593+
!controller[kState].queue.length){
594+
readableStreamDefaultControllerClearAlgorithms(controller);
595+
readableStreamClose(stream);
596+
}else{
597+
readableStreamDefaultControllerCallPullIfNeeded(controller);
598+
}
599+
600+
returnPromiseResolve({done: false,value: chunk});
592601
}
602+
}elseif(controller[kState].queueTotalSize>0){
603+
// Byte controller with buffered data: same shape as above via
604+
// the queue-filled arm of the byte controller's pull steps.
605+
stream[kState].disturbed=true;
606+
returnPromiseResolve({
607+
done: false,
593608

594-
returnPromiseResolve({done: false,value: chunk});
609+
value: readableByteStreamControllerDequeueChunk(controller),
610+
});
595611
}
596612
}
597613
state.current=nextSteps();
@@ -914,24 +930,36 @@ class ReadableStreamDefaultReader {
914930
conststream=this[kState].stream;
915931
constcontroller=stream[kState].controller;
916932

917-
// Fast path: if data is already buffered in a default controller,
933+
// Fast path: if data is already buffered in the controller's queue,
918934
// return a resolved promise immediately without creating a read request.
919935
// This is spec-compliant because read() returns a Promise, and
920936
// Promise.resolve() callbacks still run in the microtask queue.
921-
if(stream[kState].state==='readable'&&
922-
isReadableStreamDefaultController(controller)&&
923-
controller[kState].queue.length>0){
924-
stream[kState].disturbed=true;
925-
constchunk=dequeueValue(controller);
937+
if(stream[kState].state==='readable'){
938+
if(isReadableStreamDefaultController(controller)){
939+
if(controller[kState].queue.length>0){
940+
stream[kState].disturbed=true;
941+
constchunk=dequeueValue(controller);
942+
943+
if(controller[kState].closeRequested&&!controller[kState].queue.length){
944+
readableStreamDefaultControllerClearAlgorithms(controller);
945+
readableStreamClose(stream);
946+
}else{
947+
readableStreamDefaultControllerCallPullIfNeeded(controller);
948+
}
926949

927-
if(controller[kState].closeRequested&&!controller[kState].queue.length){
928-
readableStreamDefaultControllerClearAlgorithms(controller);
929-
readableStreamClose(stream);
930-
}else{
931-
readableStreamDefaultControllerCallPullIfNeeded(controller);
950+
returnPromiseResolve({done: false,value: chunk});
951+
}
952+
}elseif(controller[kState].queueTotalSize>0){
953+
// Byte controller with buffered data: mirror the queue-filled arm
954+
// of its pull steps (which never consults pendingPullIntos) minus
955+
// the read request.
956+
stream[kState].disturbed=true;
957+
returnPromiseResolve({
958+
done: false,
959+
960+
value: readableByteStreamControllerDequeueChunk(controller),
961+
});
932962
}
933-
934-
returnPromiseResolve({value: chunk,done: false});
935963
}
936964

937965
// Slow path: create request and go through normal flow
@@ -3040,9 +3068,23 @@ function readableByteStreamControllerEnqueue(controller, chunk) {
30403068
}
30413069
}
30423070

3043-
if(readableStreamHasDefaultReader(stream)){
3044-
readableByteStreamControllerProcessReadRequestsUsingQueue(controller);
3045-
if(!readableStreamGetNumReadRequests(stream)){
3071+
// Single consolidated pass over the reader state. The spec routes this
3072+
// through HasDefaultReader / ProcessReadRequestsUsingQueue /
3073+
// GetNumReadRequests / FulfillReadRequest, which would re-run the same
3074+
// reader brand check and re-load the read request list four times on
3075+
// this per-chunk path.
3076+
const{ reader }=stream[kState];
3077+
if(reader!==undefined&&
3078+
reader[kState]!==undefined&&
3079+
reader[kType]==='ReadableStreamDefaultReader'){
3080+
const{ readRequests }=reader[kState];
3081+
if(readRequests.length&&controller[kState].queueTotalSize>0){
3082+
// Only possible when data was enqueued while the stream was not
3083+
// being read; read requests otherwise never coexist with a
3084+
// non-empty queue.
3085+
readableByteStreamControllerProcessReadRequestsUsingQueue(controller);
3086+
}
3087+
if(!readRequests.length){
30463088
readableByteStreamControllerEnqueueChunkToQueue(
30473089
controller,
30483090
transferredBuffer,
@@ -3056,7 +3098,8 @@ function readableByteStreamControllerEnqueue(controller, chunk) {
30563098
}
30573099
consttransferredView=
30583100
newUint8Array(transferredBuffer,byteOffset,byteLength);
3059-
readableStreamFulfillReadRequest(stream,transferredView,false);
3101+
constreadRequest=ArrayPrototypeShift(readRequests);
3102+
readRequest[kChunk](transferredView);
30603103
}
30613104
}elseif(readableStreamHasBYOBReader(stream)){
30623105
readableByteStreamControllerEnqueueChunkToQueue(
@@ -3391,22 +3434,28 @@ function readableByteStreamControllerCancelSteps(controller, reason) {
33913434
returnresult;
33923435
}
33933436

3394-
functionreadableByteStreamControllerFillReadRequestFromQueue(controller,readRequest){
3395-
const{
3396-
queue,
3397-
queueTotalSize,
3398-
}=controller[kState];
3399-
assert(queueTotalSize>0);
3437+
// Dequeues the first chunk of the byte queue as a Uint8Array view,
3438+
// handling queue drain (close-on-empty or pull) before the view is
3439+
// created. This is the [[queueTotalSize]] > 0 arm of the byte
3440+
// controller's pull steps; it is also called directly from the
3441+
// buffered fast paths in ReadableStreamDefaultReader.read() and the
3442+
// async iterator, which resolve with the view without allocating a
3443+
// read request.
3444+
functionreadableByteStreamControllerDequeueChunk(controller){
3445+
assert(controller[kState].queueTotalSize>0);
34003446
const{
34013447
buffer,
34023448
byteOffset,
34033449
byteLength,
3404-
}=ArrayPrototypeShift(queue);
3450+
}=ArrayPrototypeShift(controller[kState].queue);
34053451

34063452
controller[kState].queueTotalSize-=byteLength;
34073453
readableByteStreamControllerHandleQueueDrain(controller);
3408-
constview=newUint8Array(buffer,byteOffset,byteLength);
3409-
readRequest[kChunk](view);
3454+
returnnewUint8Array(buffer,byteOffset,byteLength);
3455+
}
3456+
3457+
functionreadableByteStreamControllerFillReadRequestFromQueue(controller,readRequest){
3458+
readRequest[kChunk](readableByteStreamControllerDequeueChunk(controller));
34103459
}
34113460

34123461
functionreadableByteStreamControllerProcessReadRequestsUsingQueue(controller){

β€Žlib/internal/webstreams/util.jsβ€Ž

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ const {
77
ArrayPrototypePush,
88
ArrayPrototypeShift,
99
AsyncIteratorPrototype,
10+
DataViewPrototypeGetBuffer,
11+
DataViewPrototypeGetByteLength,
12+
DataViewPrototypeGetByteOffset,
1013
FunctionPrototypeCall,
1114
MathMax,
1215
NumberIsNaN,
1316
PromisePrototypeThen,
1417
PromiseReject,
1518
PromiseResolve,
16-
ReflectGet,
1719
Symbol,
20+
TypedArrayPrototypeGetBuffer,
21+
TypedArrayPrototypeGetByteLength,
22+
TypedArrayPrototypeGetByteOffset,
1823
Uint8Array,
1924
}=primordials;
2025

@@ -41,6 +46,10 @@ const {
4146

4247
constassert=require('internal/assert');
4348

49+
const{
50+
isDataView,
51+
}=require('internal/util/types');
52+
4453
const{
4554
validateFunction,
4655
}=require('internal/validators');
@@ -93,20 +102,29 @@ function customInspect(depth, options, name, data) {
93102
return`${name}${inspect(data,opts)}`;
94103
}
95104

96-
// These are defensive to work around the possibility that
97-
// the buffer, byteLength, and byteOffset properties on
98-
// ArrayBuffer and ArrayBufferView's may have been tampered with.
105+
// These use the original prototype getters so that user tampering with
106+
// the buffer, byteLength, and byteOffset properties on ArrayBuffer and
107+
// ArrayBufferView's is not observed. They run once or more per chunk on
108+
// every byte-stream path, so they must not go through a reflective get
109+
// (the previous view.constructor.prototype lookup was both slower and
110+
// spoofable via a user-defined .constructor).
99111

100112
functionArrayBufferViewGetBuffer(view){
101-
returnReflectGet(view.constructor.prototype,'buffer',view);
113+
returnisDataView(view) ?
114+
DataViewPrototypeGetBuffer(view) :
115+
TypedArrayPrototypeGetBuffer(view);
102116
}
103117

104118
functionArrayBufferViewGetByteLength(view){
105-
returnReflectGet(view.constructor.prototype,'byteLength',view);
119+
returnisDataView(view) ?
120+
DataViewPrototypeGetByteLength(view) :
121+
TypedArrayPrototypeGetByteLength(view);
106122
}
107123

108124
functionArrayBufferViewGetByteOffset(view){
109-
returnReflectGet(view.constructor.prototype,'byteOffset',view);
125+
returnisDataView(view) ?
126+
DataViewPrototypeGetByteOffset(view) :
127+
TypedArrayPrototypeGetByteOffset(view);
110128
}
111129

112130
functioncloneAsUint8Array(view){

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 3043b2a

Browse files
committed
stream: speed up async iteration over WHATWG byte streams
for await / reader.read() loops over byte streams were ~4x slower than over default streams. Three per-chunk costs, none required by the spec: - ArrayBufferViewGetBuffer/ByteLength/ByteOffset went through ReflectGet(view.constructor.prototype, ...), a reflective get that is ~3.5x slower than the original prototype getters from primordials and spoofable through a user-defined .constructor to boot. - The buffered fast paths in ReadableStreamDefaultReader.read() and the async iterator only covered default controllers, so byte streams with queued data still allocated a read request and PromiseWithResolvers per chunk. Byte-queue dequeue is fully synchronous (it is the queue-filled arm of the byte controller's pull steps), so both fast paths now resolve directly from the byte queue. - readableByteStreamControllerEnqueue re-ran the reader brand check and re-loaded the read request list four times per chunk across HasDefaultReader / ProcessReadRequestsUsingQueue / GetNumReadRequests / FulfillReadRequest; it now does a single pass. The async iterator also reuses its read request object across reads (at most one is ever in flight). benchmark/webstreams interleaved same-day A/B, --runs 10: readable-async-iterator bytes +16.3% (***), readable-read byob +9.1% (***), all other rows neutral. Profiler harness: parked byte iteration +14%, buffered byte iteration +37%, buffered byte read loop +18%, default-stream rows at parity. WPT streams/compression/encoding subtests identical to baseline. Signed-off-by: Matteo Collina <hello@matteocollina.com> PR-URL: #64291 Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 9445e27 commit 3043b2a

2 files changed

Lines changed: 118 additions & 51 deletions

File tree

β€Žlib/internal/webstreams/readablestream.jsβ€Ž

Lines changed: 93 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,12 @@ class ReadableStream {
497497
current: undefined,
498498
};
499499
letstarted=false;
500+
// A single reusable read request: at most one read is ever in flight
501+
// (next() chains through state.current), and the request is consumed
502+
// before the next read starts, so only its promise record changes
503+
// per read.
504+
// eslint-disable-next-line no-use-before-define
505+
constreadRequest=newReadableStreamAsyncIteratorReadRequest(reader,state,undefined);
500506

501507
// The nextSteps function is not an async function in order
502508
// to make it more efficient. Because nextSteps explicitly
@@ -515,8 +521,8 @@ class ReadableStream {
515521
}
516522
constpromise=PromiseWithResolvers();
517523

518-
// eslint-disable-next-line no-use-before-define
519-
readableStreamDefaultReaderRead(reader,newReadableStreamAsyncIteratorReadRequest(reader,state,promise));
524+
readRequest.promise=promise;
525+
readableStreamDefaultReaderRead(reader,readRequest);
520526
returnpromise.promise;
521527
}
522528

@@ -570,28 +576,38 @@ class ReadableStream {
570576
}
571577
// No read is in flight. Mirror the buffered fast path of
572578
// ReadableStreamDefaultReader.read(): when data is already queued
573-
// in a default controller, resolve immediately without allocating
574-
// a read request. The result settles synchronously, so leaving
579+
// in the controller, resolve immediately without allocating a
580+
// read request. The result settles synchronously, so leaving
575581
// state.current undefined matches the state the slow path reaches
576582
// once its read request callbacks have settled.
577583
conststream=reader[kState].stream;
578-
if(!state.done&&stream!==undefined){
584+
if(!state.done&&stream!==undefined&&
585+
stream[kState].state==='readable'){
579586
constcontroller=stream[kState].controller;
580-
if(stream[kState].state==='readable'&&
581-
isReadableStreamDefaultController(controller)&&
582-
controller[kState].queue.length>0){
583-
stream[kState].disturbed=true;
584-
constchunk=dequeueValue(controller);
585-
586-
if(controller[kState].closeRequested&&
587-
!controller[kState].queue.length){
588-
readableStreamDefaultControllerClearAlgorithms(controller);
589-
readableStreamClose(stream);
590-
}else{
591-
readableStreamDefaultControllerCallPullIfNeeded(controller);
587+
if(isReadableStreamDefaultController(controller)){
588+
if(controller[kState].queue.length>0){
589+
stream[kState].disturbed=true;
590+
constchunk=dequeueValue(controller);
591+
592+
if(controller[kState].closeRequested&&
593+
!controller[kState].queue.length){
594+
readableStreamDefaultControllerClearAlgorithms(controller);
595+
readableStreamClose(stream);
596+
}else{
597+
readableStreamDefaultControllerCallPullIfNeeded(controller);
598+
}
599+
600+
returnPromiseResolve({done: false,value: chunk});
592601
}
602+
}elseif(controller[kState].queueTotalSize>0){
603+
// Byte controller with buffered data: same shape as above via
604+
// the queue-filled arm of the byte controller's pull steps.
605+
stream[kState].disturbed=true;
606+
returnPromiseResolve({
607+
done: false,
593608

594-
returnPromiseResolve({done: false,value: chunk});
609+
value: readableByteStreamControllerDequeueChunk(controller),
610+
});
595611
}
596612
}
597613
state.current=nextSteps();
@@ -914,24 +930,36 @@ class ReadableStreamDefaultReader {
914930
conststream=this[kState].stream;
915931
constcontroller=stream[kState].controller;
916932

917-
// Fast path: if data is already buffered in a default controller,
933+
// Fast path: if data is already buffered in the controller's queue,
918934
// return a resolved promise immediately without creating a read request.
919935
// This is spec-compliant because read() returns a Promise, and
920936
// Promise.resolve() callbacks still run in the microtask queue.
921-
if(stream[kState].state==='readable'&&
922-
isReadableStreamDefaultController(controller)&&
923-
controller[kState].queue.length>0){
924-
stream[kState].disturbed=true;
925-
constchunk=dequeueValue(controller);
937+
if(stream[kState].state==='readable'){
938+
if(isReadableStreamDefaultController(controller)){
939+
if(controller[kState].queue.length>0){
940+
stream[kState].disturbed=true;
941+
constchunk=dequeueValue(controller);
942+
943+
if(controller[kState].closeRequested&&!controller[kState].queue.length){
944+
readableStreamDefaultControllerClearAlgorithms(controller);
945+
readableStreamClose(stream);
946+
}else{
947+
readableStreamDefaultControllerCallPullIfNeeded(controller);
948+
}
926949

927-
if(controller[kState].closeRequested&&!controller[kState].queue.length){
928-
readableStreamDefaultControllerClearAlgorithms(controller);
929-
readableStreamClose(stream);
930-
}else{
931-
readableStreamDefaultControllerCallPullIfNeeded(controller);
950+
returnPromiseResolve({done: false,value: chunk});
951+
}
952+
}elseif(controller[kState].queueTotalSize>0){
953+
// Byte controller with buffered data: mirror the queue-filled arm
954+
// of its pull steps (which never consults pendingPullIntos) minus
955+
// the read request.
956+
stream[kState].disturbed=true;
957+
returnPromiseResolve({
958+
done: false,
959+
960+
value: readableByteStreamControllerDequeueChunk(controller),
961+
});
932962
}
933-
934-
returnPromiseResolve({value: chunk,done: false});
935963
}
936964

937965
// Slow path: create request and go through normal flow
@@ -3040,9 +3068,23 @@ function readableByteStreamControllerEnqueue(controller, chunk) {
30403068
}
30413069
}
30423070

3043-
if(readableStreamHasDefaultReader(stream)){
3044-
readableByteStreamControllerProcessReadRequestsUsingQueue(controller);
3045-
if(!readableStreamGetNumReadRequests(stream)){
3071+
// Single consolidated pass over the reader state. The spec routes this
3072+
// through HasDefaultReader / ProcessReadRequestsUsingQueue /
3073+
// GetNumReadRequests / FulfillReadRequest, which would re-run the same
3074+
// reader brand check and re-load the read request list four times on
3075+
// this per-chunk path.
3076+
const{ reader }=stream[kState];
3077+
if(reader!==undefined&&
3078+
reader[kState]!==undefined&&
3079+
reader[kType]==='ReadableStreamDefaultReader'){
3080+
const{ readRequests }=reader[kState];
3081+
if(readRequests.length&&controller[kState].queueTotalSize>0){
3082+
// Only possible when data was enqueued while the stream was not
3083+
// being read; read requests otherwise never coexist with a
3084+
// non-empty queue.
3085+
readableByteStreamControllerProcessReadRequestsUsingQueue(controller);
3086+
}
3087+
if(!readRequests.length){
30463088
readableByteStreamControllerEnqueueChunkToQueue(
30473089
controller,
30483090
transferredBuffer,
@@ -3056,7 +3098,8 @@ function readableByteStreamControllerEnqueue(controller, chunk) {
30563098
}
30573099
consttransferredView=
30583100
newUint8Array(transferredBuffer,byteOffset,byteLength);
3059-
readableStreamFulfillReadRequest(stream,transferredView,false);
3101+
constreadRequest=ArrayPrototypeShift(readRequests);
3102+
readRequest[kChunk](transferredView);
30603103
}
30613104
}elseif(readableStreamHasBYOBReader(stream)){
30623105
readableByteStreamControllerEnqueueChunkToQueue(
@@ -3391,22 +3434,28 @@ function readableByteStreamControllerCancelSteps(controller, reason) {
33913434
returnresult;
33923435
}
33933436

3394-
functionreadableByteStreamControllerFillReadRequestFromQueue(controller,readRequest){
3395-
const{
3396-
queue,
3397-
queueTotalSize,
3398-
}=controller[kState];
3399-
assert(queueTotalSize>0);
3437+
// Dequeues the first chunk of the byte queue as a Uint8Array view,
3438+
// handling queue drain (close-on-empty or pull) before the view is
3439+
// created. This is the [[queueTotalSize]] > 0 arm of the byte
3440+
// controller's pull steps; it is also called directly from the
3441+
// buffered fast paths in ReadableStreamDefaultReader.read() and the
3442+
// async iterator, which resolve with the view without allocating a
3443+
// read request.
3444+
functionreadableByteStreamControllerDequeueChunk(controller){
3445+
assert(controller[kState].queueTotalSize>0);
34003446
const{
34013447
buffer,
34023448
byteOffset,
34033449
byteLength,
3404-
}=ArrayPrototypeShift(queue);
3450+
}=ArrayPrototypeShift(controller[kState].queue);
34053451

34063452
controller[kState].queueTotalSize-=byteLength;
34073453
readableByteStreamControllerHandleQueueDrain(controller);
3408-
constview=newUint8Array(buffer,byteOffset,byteLength);
3409-
readRequest[kChunk](view);
3454+
returnnewUint8Array(buffer,byteOffset,byteLength);
3455+
}
3456+
3457+
functionreadableByteStreamControllerFillReadRequestFromQueue(controller,readRequest){
3458+
readRequest[kChunk](readableByteStreamControllerDequeueChunk(controller));
34103459
}
34113460

34123461
functionreadableByteStreamControllerProcessReadRequestsUsingQueue(controller){

β€Žlib/internal/webstreams/util.jsβ€Ž

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ const {
77
ArrayPrototypePush,
88
ArrayPrototypeShift,
99
AsyncIteratorPrototype,
10+
DataViewPrototypeGetBuffer,
11+
DataViewPrototypeGetByteLength,
12+
DataViewPrototypeGetByteOffset,
1013
FunctionPrototypeCall,
1114
MathMax,
1215
NumberIsNaN,
1316
PromisePrototypeThen,
1417
PromiseReject,
1518
PromiseResolve,
16-
ReflectGet,
1719
Symbol,
20+
TypedArrayPrototypeGetBuffer,
21+
TypedArrayPrototypeGetByteLength,
22+
TypedArrayPrototypeGetByteOffset,
1823
Uint8Array,
1924
}=primordials;
2025

@@ -41,6 +46,10 @@ const {
4146

4247
constassert=require('internal/assert');
4348

49+
const{
50+
isDataView,
51+
}=require('internal/util/types');
52+
4453
const{
4554
validateFunction,
4655
}=require('internal/validators');
@@ -93,20 +102,29 @@ function customInspect(depth, options, name, data) {
93102
return`${name}${inspect(data,opts)}`;
94103
}
95104

96-
// These are defensive to work around the possibility that
97-
// the buffer, byteLength, and byteOffset properties on
98-
// ArrayBuffer and ArrayBufferView's may have been tampered with.
105+
// These use the original prototype getters so that user tampering with
106+
// the buffer, byteLength, and byteOffset properties on ArrayBuffer and
107+
// ArrayBufferView's is not observed. They run once or more per chunk on
108+
// every byte-stream path, so they must not go through a reflective get
109+
// (the previous view.constructor.prototype lookup was both slower and
110+
// spoofable via a user-defined .constructor).
99111

100112
functionArrayBufferViewGetBuffer(view){
101-
returnReflectGet(view.constructor.prototype,'buffer',view);
113+
returnisDataView(view) ?
114+
DataViewPrototypeGetBuffer(view) :
115+
TypedArrayPrototypeGetBuffer(view);
102116
}
103117

104118
functionArrayBufferViewGetByteLength(view){
105-
returnReflectGet(view.constructor.prototype,'byteLength',view);
119+
returnisDataView(view) ?
120+
DataViewPrototypeGetByteLength(view) :
121+
TypedArrayPrototypeGetByteLength(view);
106122
}
107123

108124
functionArrayBufferViewGetByteOffset(view){
109-
returnReflectGet(view.constructor.prototype,'byteOffset',view);
125+
returnisDataView(view) ?
126+
DataViewPrototypeGetByteOffset(view) :
127+
TypedArrayPrototypeGetByteOffset(view);
110128
}
111129

112130
functioncloneAsUint8Array(view){

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 3043b2a

Browse files
committed
stream: speed up async iteration over WHATWG byte streams
for await / reader.read() loops over byte streams were ~4x slower than over default streams. Three per-chunk costs, none required by the spec: - ArrayBufferViewGetBuffer/ByteLength/ByteOffset went through ReflectGet(view.constructor.prototype, ...), a reflective get that is ~3.5x slower than the original prototype getters from primordials and spoofable through a user-defined .constructor to boot. - The buffered fast paths in ReadableStreamDefaultReader.read() and the async iterator only covered default controllers, so byte streams with queued data still allocated a read request and PromiseWithResolvers per chunk. Byte-queue dequeue is fully synchronous (it is the queue-filled arm of the byte controller's pull steps), so both fast paths now resolve directly from the byte queue. - readableByteStreamControllerEnqueue re-ran the reader brand check and re-loaded the read request list four times per chunk across HasDefaultReader / ProcessReadRequestsUsingQueue / GetNumReadRequests / FulfillReadRequest; it now does a single pass. The async iterator also reuses its read request object across reads (at most one is ever in flight). benchmark/webstreams interleaved same-day A/B, --runs 10: readable-async-iterator bytes +16.3% (***), readable-read byob +9.1% (***), all other rows neutral. Profiler harness: parked byte iteration +14%, buffered byte iteration +37%, buffered byte read loop +18%, default-stream rows at parity. WPT streams/compression/encoding subtests identical to baseline. Signed-off-by: Matteo Collina <hello@matteocollina.com> PR-URL: #64291 Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 9445e27 commit 3043b2a

2 files changed

Lines changed: 118 additions & 51 deletions

File tree

β€Žlib/internal/webstreams/readablestream.jsβ€Ž

Lines changed: 93 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,12 @@ class ReadableStream {
497497
current: undefined,
498498
};
499499
letstarted=false;
500+
// A single reusable read request: at most one read is ever in flight
501+
// (next() chains through state.current), and the request is consumed
502+
// before the next read starts, so only its promise record changes
503+
// per read.
504+
// eslint-disable-next-line no-use-before-define
505+
constreadRequest=newReadableStreamAsyncIteratorReadRequest(reader,state,undefined);
500506

501507
// The nextSteps function is not an async function in order
502508
// to make it more efficient. Because nextSteps explicitly
@@ -515,8 +521,8 @@ class ReadableStream {
515521
}
516522
constpromise=PromiseWithResolvers();
517523

518-
// eslint-disable-next-line no-use-before-define
519-
readableStreamDefaultReaderRead(reader,newReadableStreamAsyncIteratorReadRequest(reader,state,promise));
524+
readRequest.promise=promise;
525+
readableStreamDefaultReaderRead(reader,readRequest);
520526
returnpromise.promise;
521527
}
522528

@@ -570,28 +576,38 @@ class ReadableStream {
570576
}
571577
// No read is in flight. Mirror the buffered fast path of
572578
// ReadableStreamDefaultReader.read(): when data is already queued
573-
// in a default controller, resolve immediately without allocating
574-
// a read request. The result settles synchronously, so leaving
579+
// in the controller, resolve immediately without allocating a
580+
// read request. The result settles synchronously, so leaving
575581
// state.current undefined matches the state the slow path reaches
576582
// once its read request callbacks have settled.
577583
conststream=reader[kState].stream;
578-
if(!state.done&&stream!==undefined){
584+
if(!state.done&&stream!==undefined&&
585+
stream[kState].state==='readable'){
579586
constcontroller=stream[kState].controller;
580-
if(stream[kState].state==='readable'&&
581-
isReadableStreamDefaultController(controller)&&
582-
controller[kState].queue.length>0){
583-
stream[kState].disturbed=true;
584-
constchunk=dequeueValue(controller);
585-
586-
if(controller[kState].closeRequested&&
587-
!controller[kState].queue.length){
588-
readableStreamDefaultControllerClearAlgorithms(controller);
589-
readableStreamClose(stream);
590-
}else{
591-
readableStreamDefaultControllerCallPullIfNeeded(controller);
587+
if(isReadableStreamDefaultController(controller)){
588+
if(controller[kState].queue.length>0){
589+
stream[kState].disturbed=true;
590+
constchunk=dequeueValue(controller);
591+
592+
if(controller[kState].closeRequested&&
593+
!controller[kState].queue.length){
594+
readableStreamDefaultControllerClearAlgorithms(controller);
595+
readableStreamClose(stream);
596+
}else{
597+
readableStreamDefaultControllerCallPullIfNeeded(controller);
598+
}
599+
600+
returnPromiseResolve({done: false,value: chunk});
592601
}
602+
}elseif(controller[kState].queueTotalSize>0){
603+
// Byte controller with buffered data: same shape as above via
604+
// the queue-filled arm of the byte controller's pull steps.
605+
stream[kState].disturbed=true;
606+
returnPromiseResolve({
607+
done: false,
593608

594-
returnPromiseResolve({done: false,value: chunk});
609+
value: readableByteStreamControllerDequeueChunk(controller),
610+
});
595611
}
596612
}
597613
state.current=nextSteps();
@@ -914,24 +930,36 @@ class ReadableStreamDefaultReader {
914930
conststream=this[kState].stream;
915931
constcontroller=stream[kState].controller;
916932

917-
// Fast path: if data is already buffered in a default controller,
933+
// Fast path: if data is already buffered in the controller's queue,
918934
// return a resolved promise immediately without creating a read request.
919935
// This is spec-compliant because read() returns a Promise, and
920936
// Promise.resolve() callbacks still run in the microtask queue.
921-
if(stream[kState].state==='readable'&&
922-
isReadableStreamDefaultController(controller)&&
923-
controller[kState].queue.length>0){
924-
stream[kState].disturbed=true;
925-
constchunk=dequeueValue(controller);
937+
if(stream[kState].state==='readable'){
938+
if(isReadableStreamDefaultController(controller)){
939+
if(controller[kState].queue.length>0){
940+
stream[kState].disturbed=true;
941+
constchunk=dequeueValue(controller);
942+
943+
if(controller[kState].closeRequested&&!controller[kState].queue.length){
944+
readableStreamDefaultControllerClearAlgorithms(controller);
945+
readableStreamClose(stream);
946+
}else{
947+
readableStreamDefaultControllerCallPullIfNeeded(controller);
948+
}
926949

927-
if(controller[kState].closeRequested&&!controller[kState].queue.length){
928-
readableStreamDefaultControllerClearAlgorithms(controller);
929-
readableStreamClose(stream);
930-
}else{
931-
readableStreamDefaultControllerCallPullIfNeeded(controller);
950+
returnPromiseResolve({done: false,value: chunk});
951+
}
952+
}elseif(controller[kState].queueTotalSize>0){
953+
// Byte controller with buffered data: mirror the queue-filled arm
954+
// of its pull steps (which never consults pendingPullIntos) minus
955+
// the read request.
956+
stream[kState].disturbed=true;
957+
returnPromiseResolve({
958+
done: false,
959+
960+
value: readableByteStreamControllerDequeueChunk(controller),
961+
});
932962
}
933-
934-
returnPromiseResolve({value: chunk,done: false});
935963
}
936964

937965
// Slow path: create request and go through normal flow
@@ -3040,9 +3068,23 @@ function readableByteStreamControllerEnqueue(controller, chunk) {
30403068
}
30413069
}
30423070

3043-
if(readableStreamHasDefaultReader(stream)){
3044-
readableByteStreamControllerProcessReadRequestsUsingQueue(controller);
3045-
if(!readableStreamGetNumReadRequests(stream)){
3071+
// Single consolidated pass over the reader state. The spec routes this
3072+
// through HasDefaultReader / ProcessReadRequestsUsingQueue /
3073+
// GetNumReadRequests / FulfillReadRequest, which would re-run the same
3074+
// reader brand check and re-load the read request list four times on
3075+
// this per-chunk path.
3076+
const{ reader }=stream[kState];
3077+
if(reader!==undefined&&
3078+
reader[kState]!==undefined&&
3079+
reader[kType]==='ReadableStreamDefaultReader'){
3080+
const{ readRequests }=reader[kState];
3081+
if(readRequests.length&&controller[kState].queueTotalSize>0){
3082+
// Only possible when data was enqueued while the stream was not
3083+
// being read; read requests otherwise never coexist with a
3084+
// non-empty queue.
3085+
readableByteStreamControllerProcessReadRequestsUsingQueue(controller);
3086+
}
3087+
if(!readRequests.length){
30463088
readableByteStreamControllerEnqueueChunkToQueue(
30473089
controller,
30483090
transferredBuffer,
@@ -3056,7 +3098,8 @@ function readableByteStreamControllerEnqueue(controller, chunk) {
30563098
}
30573099
consttransferredView=
30583100
newUint8Array(transferredBuffer,byteOffset,byteLength);
3059-
readableStreamFulfillReadRequest(stream,transferredView,false);
3101+
constreadRequest=ArrayPrototypeShift(readRequests);
3102+
readRequest[kChunk](transferredView);
30603103
}
30613104
}elseif(readableStreamHasBYOBReader(stream)){
30623105
readableByteStreamControllerEnqueueChunkToQueue(
@@ -3391,22 +3434,28 @@ function readableByteStreamControllerCancelSteps(controller, reason) {
33913434
returnresult;
33923435
}
33933436

3394-
functionreadableByteStreamControllerFillReadRequestFromQueue(controller,readRequest){
3395-
const{
3396-
queue,
3397-
queueTotalSize,
3398-
}=controller[kState];
3399-
assert(queueTotalSize>0);
3437+
// Dequeues the first chunk of the byte queue as a Uint8Array view,
3438+
// handling queue drain (close-on-empty or pull) before the view is
3439+
// created. This is the [[queueTotalSize]] > 0 arm of the byte
3440+
// controller's pull steps; it is also called directly from the
3441+
// buffered fast paths in ReadableStreamDefaultReader.read() and the
3442+
// async iterator, which resolve with the view without allocating a
3443+
// read request.
3444+
functionreadableByteStreamControllerDequeueChunk(controller){
3445+
assert(controller[kState].queueTotalSize>0);
34003446
const{
34013447
buffer,
34023448
byteOffset,
34033449
byteLength,
3404-
}=ArrayPrototypeShift(queue);
3450+
}=ArrayPrototypeShift(controller[kState].queue);
34053451

34063452
controller[kState].queueTotalSize-=byteLength;
34073453
readableByteStreamControllerHandleQueueDrain(controller);
3408-
constview=newUint8Array(buffer,byteOffset,byteLength);
3409-
readRequest[kChunk](view);
3454+
returnnewUint8Array(buffer,byteOffset,byteLength);
3455+
}
3456+
3457+
functionreadableByteStreamControllerFillReadRequestFromQueue(controller,readRequest){
3458+
readRequest[kChunk](readableByteStreamControllerDequeueChunk(controller));
34103459
}
34113460

34123461
functionreadableByteStreamControllerProcessReadRequestsUsingQueue(controller){

β€Žlib/internal/webstreams/util.jsβ€Ž

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ const {
77
ArrayPrototypePush,
88
ArrayPrototypeShift,
99
AsyncIteratorPrototype,
10+
DataViewPrototypeGetBuffer,
11+
DataViewPrototypeGetByteLength,
12+
DataViewPrototypeGetByteOffset,
1013
FunctionPrototypeCall,
1114
MathMax,
1215
NumberIsNaN,
1316
PromisePrototypeThen,
1417
PromiseReject,
1518
PromiseResolve,
16-
ReflectGet,
1719
Symbol,
20+
TypedArrayPrototypeGetBuffer,
21+
TypedArrayPrototypeGetByteLength,
22+
TypedArrayPrototypeGetByteOffset,
1823
Uint8Array,
1924
}=primordials;
2025

@@ -41,6 +46,10 @@ const {
4146

4247
constassert=require('internal/assert');
4348

49+
const{
50+
isDataView,
51+
}=require('internal/util/types');
52+
4453
const{
4554
validateFunction,
4655
}=require('internal/validators');
@@ -93,20 +102,29 @@ function customInspect(depth, options, name, data) {
93102
return`${name}${inspect(data,opts)}`;
94103
}
95104

96-
// These are defensive to work around the possibility that
97-
// the buffer, byteLength, and byteOffset properties on
98-
// ArrayBuffer and ArrayBufferView's may have been tampered with.
105+
// These use the original prototype getters so that user tampering with
106+
// the buffer, byteLength, and byteOffset properties on ArrayBuffer and
107+
// ArrayBufferView's is not observed. They run once or more per chunk on
108+
// every byte-stream path, so they must not go through a reflective get
109+
// (the previous view.constructor.prototype lookup was both slower and
110+
// spoofable via a user-defined .constructor).
99111

100112
functionArrayBufferViewGetBuffer(view){
101-
returnReflectGet(view.constructor.prototype,'buffer',view);
113+
returnisDataView(view) ?
114+
DataViewPrototypeGetBuffer(view) :
115+
TypedArrayPrototypeGetBuffer(view);
102116
}
103117

104118
functionArrayBufferViewGetByteLength(view){
105-
returnReflectGet(view.constructor.prototype,'byteLength',view);
119+
returnisDataView(view) ?
120+
DataViewPrototypeGetByteLength(view) :
121+
TypedArrayPrototypeGetByteLength(view);
106122
}
107123

108124
functionArrayBufferViewGetByteOffset(view){
109-
returnReflectGet(view.constructor.prototype,'byteOffset',view);
125+
returnisDataView(view) ?
126+
DataViewPrototypeGetByteOffset(view) :
127+
TypedArrayPrototypeGetByteOffset(view);
110128
}
111129

112130
functioncloneAsUint8Array(view){

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 3043b2a

Browse files
committed
stream: speed up async iteration over WHATWG byte streams
for await / reader.read() loops over byte streams were ~4x slower than over default streams. Three per-chunk costs, none required by the spec: - ArrayBufferViewGetBuffer/ByteLength/ByteOffset went through ReflectGet(view.constructor.prototype, ...), a reflective get that is ~3.5x slower than the original prototype getters from primordials and spoofable through a user-defined .constructor to boot. - The buffered fast paths in ReadableStreamDefaultReader.read() and the async iterator only covered default controllers, so byte streams with queued data still allocated a read request and PromiseWithResolvers per chunk. Byte-queue dequeue is fully synchronous (it is the queue-filled arm of the byte controller's pull steps), so both fast paths now resolve directly from the byte queue. - readableByteStreamControllerEnqueue re-ran the reader brand check and re-loaded the read request list four times per chunk across HasDefaultReader / ProcessReadRequestsUsingQueue / GetNumReadRequests / FulfillReadRequest; it now does a single pass. The async iterator also reuses its read request object across reads (at most one is ever in flight). benchmark/webstreams interleaved same-day A/B, --runs 10: readable-async-iterator bytes +16.3% (***), readable-read byob +9.1% (***), all other rows neutral. Profiler harness: parked byte iteration +14%, buffered byte iteration +37%, buffered byte read loop +18%, default-stream rows at parity. WPT streams/compression/encoding subtests identical to baseline. Signed-off-by: Matteo Collina <hello@matteocollina.com> PR-URL: #64291 Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 9445e27 commit 3043b2a

2 files changed

Lines changed: 118 additions & 51 deletions

File tree

β€Žlib/internal/webstreams/readablestream.jsβ€Ž

Lines changed: 93 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,12 @@ class ReadableStream {
497497
current: undefined,
498498
};
499499
letstarted=false;
500+
// A single reusable read request: at most one read is ever in flight
501+
// (next() chains through state.current), and the request is consumed
502+
// before the next read starts, so only its promise record changes
503+
// per read.
504+
// eslint-disable-next-line no-use-before-define
505+
constreadRequest=newReadableStreamAsyncIteratorReadRequest(reader,state,undefined);
500506

501507
// The nextSteps function is not an async function in order
502508
// to make it more efficient. Because nextSteps explicitly
@@ -515,8 +521,8 @@ class ReadableStream {
515521
}
516522
constpromise=PromiseWithResolvers();
517523

518-
// eslint-disable-next-line no-use-before-define
519-
readableStreamDefaultReaderRead(reader,newReadableStreamAsyncIteratorReadRequest(reader,state,promise));
524+
readRequest.promise=promise;
525+
readableStreamDefaultReaderRead(reader,readRequest);
520526
returnpromise.promise;
521527
}
522528

@@ -570,28 +576,38 @@ class ReadableStream {
570576
}
571577
// No read is in flight. Mirror the buffered fast path of
572578
// ReadableStreamDefaultReader.read(): when data is already queued
573-
// in a default controller, resolve immediately without allocating
574-
// a read request. The result settles synchronously, so leaving
579+
// in the controller, resolve immediately without allocating a
580+
// read request. The result settles synchronously, so leaving
575581
// state.current undefined matches the state the slow path reaches
576582
// once its read request callbacks have settled.
577583
conststream=reader[kState].stream;
578-
if(!state.done&&stream!==undefined){
584+
if(!state.done&&stream!==undefined&&
585+
stream[kState].state==='readable'){
579586
constcontroller=stream[kState].controller;
580-
if(stream[kState].state==='readable'&&
581-
isReadableStreamDefaultController(controller)&&
582-
controller[kState].queue.length>0){
583-
stream[kState].disturbed=true;
584-
constchunk=dequeueValue(controller);
585-
586-
if(controller[kState].closeRequested&&
587-
!controller[kState].queue.length){
588-
readableStreamDefaultControllerClearAlgorithms(controller);
589-
readableStreamClose(stream);
590-
}else{
591-
readableStreamDefaultControllerCallPullIfNeeded(controller);
587+
if(isReadableStreamDefaultController(controller)){
588+
if(controller[kState].queue.length>0){
589+
stream[kState].disturbed=true;
590+
constchunk=dequeueValue(controller);
591+
592+
if(controller[kState].closeRequested&&
593+
!controller[kState].queue.length){
594+
readableStreamDefaultControllerClearAlgorithms(controller);
595+
readableStreamClose(stream);
596+
}else{
597+
readableStreamDefaultControllerCallPullIfNeeded(controller);
598+
}
599+
600+
returnPromiseResolve({done: false,value: chunk});
592601
}
602+
}elseif(controller[kState].queueTotalSize>0){
603+
// Byte controller with buffered data: same shape as above via
604+
// the queue-filled arm of the byte controller's pull steps.
605+
stream[kState].disturbed=true;
606+
returnPromiseResolve({
607+
done: false,
593608

594-
returnPromiseResolve({done: false,value: chunk});
609+
value: readableByteStreamControllerDequeueChunk(controller),
610+
});
595611
}
596612
}
597613
state.current=nextSteps();
@@ -914,24 +930,36 @@ class ReadableStreamDefaultReader {
914930
conststream=this[kState].stream;
915931
constcontroller=stream[kState].controller;
916932

917-
// Fast path: if data is already buffered in a default controller,
933+
// Fast path: if data is already buffered in the controller's queue,
918934
// return a resolved promise immediately without creating a read request.
919935
// This is spec-compliant because read() returns a Promise, and
920936
// Promise.resolve() callbacks still run in the microtask queue.
921-
if(stream[kState].state==='readable'&&
922-
isReadableStreamDefaultController(controller)&&
923-
controller[kState].queue.length>0){
924-
stream[kState].disturbed=true;
925-
constchunk=dequeueValue(controller);
937+
if(stream[kState].state==='readable'){
938+
if(isReadableStreamDefaultController(controller)){
939+
if(controller[kState].queue.length>0){
940+
stream[kState].disturbed=true;
941+
constchunk=dequeueValue(controller);
942+
943+
if(controller[kState].closeRequested&&!controller[kState].queue.length){
944+
readableStreamDefaultControllerClearAlgorithms(controller);
945+
readableStreamClose(stream);
946+
}else{
947+
readableStreamDefaultControllerCallPullIfNeeded(controller);
948+
}
926949

927-
if(controller[kState].closeRequested&&!controller[kState].queue.length){
928-
readableStreamDefaultControllerClearAlgorithms(controller);
929-
readableStreamClose(stream);
930-
}else{
931-
readableStreamDefaultControllerCallPullIfNeeded(controller);
950+
returnPromiseResolve({done: false,value: chunk});
951+
}
952+
}elseif(controller[kState].queueTotalSize>0){
953+
// Byte controller with buffered data: mirror the queue-filled arm
954+
// of its pull steps (which never consults pendingPullIntos) minus
955+
// the read request.
956+
stream[kState].disturbed=true;
957+
returnPromiseResolve({
958+
done: false,
959+
960+
value: readableByteStreamControllerDequeueChunk(controller),
961+
});
932962
}
933-
934-
returnPromiseResolve({value: chunk,done: false});
935963
}
936964

937965
// Slow path: create request and go through normal flow
@@ -3040,9 +3068,23 @@ function readableByteStreamControllerEnqueue(controller, chunk) {
30403068
}
30413069
}
30423070

3043-
if(readableStreamHasDefaultReader(stream)){
3044-
readableByteStreamControllerProcessReadRequestsUsingQueue(controller);
3045-
if(!readableStreamGetNumReadRequests(stream)){
3071+
// Single consolidated pass over the reader state. The spec routes this
3072+
// through HasDefaultReader / ProcessReadRequestsUsingQueue /
3073+
// GetNumReadRequests / FulfillReadRequest, which would re-run the same
3074+
// reader brand check and re-load the read request list four times on
3075+
// this per-chunk path.
3076+
const{ reader }=stream[kState];
3077+
if(reader!==undefined&&
3078+
reader[kState]!==undefined&&
3079+
reader[kType]==='ReadableStreamDefaultReader'){
3080+
const{ readRequests }=reader[kState];
3081+
if(readRequests.length&&controller[kState].queueTotalSize>0){
3082+
// Only possible when data was enqueued while the stream was not
3083+
// being read; read requests otherwise never coexist with a
3084+
// non-empty queue.
3085+
readableByteStreamControllerProcessReadRequestsUsingQueue(controller);
3086+
}
3087+
if(!readRequests.length){
30463088
readableByteStreamControllerEnqueueChunkToQueue(
30473089
controller,
30483090
transferredBuffer,
@@ -3056,7 +3098,8 @@ function readableByteStreamControllerEnqueue(controller, chunk) {
30563098
}
30573099
consttransferredView=
30583100
newUint8Array(transferredBuffer,byteOffset,byteLength);
3059-
readableStreamFulfillReadRequest(stream,transferredView,false);
3101+
constreadRequest=ArrayPrototypeShift(readRequests);
3102+
readRequest[kChunk](transferredView);
30603103
}
30613104
}elseif(readableStreamHasBYOBReader(stream)){
30623105
readableByteStreamControllerEnqueueChunkToQueue(
@@ -3391,22 +3434,28 @@ function readableByteStreamControllerCancelSteps(controller, reason) {
33913434
returnresult;
33923435
}
33933436

3394-
functionreadableByteStreamControllerFillReadRequestFromQueue(controller,readRequest){
3395-
const{
3396-
queue,
3397-
queueTotalSize,
3398-
}=controller[kState];
3399-
assert(queueTotalSize>0);
3437+
// Dequeues the first chunk of the byte queue as a Uint8Array view,
3438+
// handling queue drain (close-on-empty or pull) before the view is
3439+
// created. This is the [[queueTotalSize]] > 0 arm of the byte
3440+
// controller's pull steps; it is also called directly from the
3441+
// buffered fast paths in ReadableStreamDefaultReader.read() and the
3442+
// async iterator, which resolve with the view without allocating a
3443+
// read request.
3444+
functionreadableByteStreamControllerDequeueChunk(controller){
3445+
assert(controller[kState].queueTotalSize>0);
34003446
const{
34013447
buffer,
34023448
byteOffset,
34033449
byteLength,
3404-
}=ArrayPrototypeShift(queue);
3450+
}=ArrayPrototypeShift(controller[kState].queue);
34053451

34063452
controller[kState].queueTotalSize-=byteLength;
34073453
readableByteStreamControllerHandleQueueDrain(controller);
3408-
constview=newUint8Array(buffer,byteOffset,byteLength);
3409-
readRequest[kChunk](view);
3454+
returnnewUint8Array(buffer,byteOffset,byteLength);
3455+
}
3456+
3457+
functionreadableByteStreamControllerFillReadRequestFromQueue(controller,readRequest){
3458+
readRequest[kChunk](readableByteStreamControllerDequeueChunk(controller));
34103459
}
34113460

34123461
functionreadableByteStreamControllerProcessReadRequestsUsingQueue(controller){

β€Žlib/internal/webstreams/util.jsβ€Ž

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ const {
77
ArrayPrototypePush,
88
ArrayPrototypeShift,
99
AsyncIteratorPrototype,
10+
DataViewPrototypeGetBuffer,
11+
DataViewPrototypeGetByteLength,
12+
DataViewPrototypeGetByteOffset,
1013
FunctionPrototypeCall,
1114
MathMax,
1215
NumberIsNaN,
1316
PromisePrototypeThen,
1417
PromiseReject,
1518
PromiseResolve,
16-
ReflectGet,
1719
Symbol,
20+
TypedArrayPrototypeGetBuffer,
21+
TypedArrayPrototypeGetByteLength,
22+
TypedArrayPrototypeGetByteOffset,
1823
Uint8Array,
1924
}=primordials;
2025

@@ -41,6 +46,10 @@ const {
4146

4247
constassert=require('internal/assert');
4348

49+
const{
50+
isDataView,
51+
}=require('internal/util/types');
52+
4453
const{
4554
validateFunction,
4655
}=require('internal/validators');
@@ -93,20 +102,29 @@ function customInspect(depth, options, name, data) {
93102
return`${name}${inspect(data,opts)}`;
94103
}
95104

96-
// These are defensive to work around the possibility that
97-
// the buffer, byteLength, and byteOffset properties on
98-
// ArrayBuffer and ArrayBufferView's may have been tampered with.
105+
// These use the original prototype getters so that user tampering with
106+
// the buffer, byteLength, and byteOffset properties on ArrayBuffer and
107+
// ArrayBufferView's is not observed. They run once or more per chunk on
108+
// every byte-stream path, so they must not go through a reflective get
109+
// (the previous view.constructor.prototype lookup was both slower and
110+
// spoofable via a user-defined .constructor).
99111

100112
functionArrayBufferViewGetBuffer(view){
101-
returnReflectGet(view.constructor.prototype,'buffer',view);
113+
returnisDataView(view) ?
114+
DataViewPrototypeGetBuffer(view) :
115+
TypedArrayPrototypeGetBuffer(view);
102116
}
103117

104118
functionArrayBufferViewGetByteLength(view){
105-
returnReflectGet(view.constructor.prototype,'byteLength',view);
119+
returnisDataView(view) ?
120+
DataViewPrototypeGetByteLength(view) :
121+
TypedArrayPrototypeGetByteLength(view);
106122
}
107123

108124
functionArrayBufferViewGetByteOffset(view){
109-
returnReflectGet(view.constructor.prototype,'byteOffset',view);
125+
returnisDataView(view) ?
126+
DataViewPrototypeGetByteOffset(view) :
127+
TypedArrayPrototypeGetByteOffset(view);
110128
}
111129

112130
functioncloneAsUint8Array(view){

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 3043b2a

Browse files
committed
stream: speed up async iteration over WHATWG byte streams
for await / reader.read() loops over byte streams were ~4x slower than over default streams. Three per-chunk costs, none required by the spec: - ArrayBufferViewGetBuffer/ByteLength/ByteOffset went through ReflectGet(view.constructor.prototype, ...), a reflective get that is ~3.5x slower than the original prototype getters from primordials and spoofable through a user-defined .constructor to boot. - The buffered fast paths in ReadableStreamDefaultReader.read() and the async iterator only covered default controllers, so byte streams with queued data still allocated a read request and PromiseWithResolvers per chunk. Byte-queue dequeue is fully synchronous (it is the queue-filled arm of the byte controller's pull steps), so both fast paths now resolve directly from the byte queue. - readableByteStreamControllerEnqueue re-ran the reader brand check and re-loaded the read request list four times per chunk across HasDefaultReader / ProcessReadRequestsUsingQueue / GetNumReadRequests / FulfillReadRequest; it now does a single pass. The async iterator also reuses its read request object across reads (at most one is ever in flight). benchmark/webstreams interleaved same-day A/B, --runs 10: readable-async-iterator bytes +16.3% (***), readable-read byob +9.1% (***), all other rows neutral. Profiler harness: parked byte iteration +14%, buffered byte iteration +37%, buffered byte read loop +18%, default-stream rows at parity. WPT streams/compression/encoding subtests identical to baseline. Signed-off-by: Matteo Collina <hello@matteocollina.com> PR-URL: #64291 Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 9445e27 commit 3043b2a

2 files changed

Lines changed: 118 additions & 51 deletions

File tree

β€Žlib/internal/webstreams/readablestream.jsβ€Ž

Lines changed: 93 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,12 @@ class ReadableStream {
497497
current: undefined,
498498
};
499499
letstarted=false;
500+
// A single reusable read request: at most one read is ever in flight
501+
// (next() chains through state.current), and the request is consumed
502+
// before the next read starts, so only its promise record changes
503+
// per read.
504+
// eslint-disable-next-line no-use-before-define
505+
constreadRequest=newReadableStreamAsyncIteratorReadRequest(reader,state,undefined);
500506

501507
// The nextSteps function is not an async function in order
502508
// to make it more efficient. Because nextSteps explicitly
@@ -515,8 +521,8 @@ class ReadableStream {
515521
}
516522
constpromise=PromiseWithResolvers();
517523

518-
// eslint-disable-next-line no-use-before-define
519-
readableStreamDefaultReaderRead(reader,newReadableStreamAsyncIteratorReadRequest(reader,state,promise));
524+
readRequest.promise=promise;
525+
readableStreamDefaultReaderRead(reader,readRequest);
520526
returnpromise.promise;
521527
}
522528

@@ -570,28 +576,38 @@ class ReadableStream {
570576
}
571577
// No read is in flight. Mirror the buffered fast path of
572578
// ReadableStreamDefaultReader.read(): when data is already queued
573-
// in a default controller, resolve immediately without allocating
574-
// a read request. The result settles synchronously, so leaving
579+
// in the controller, resolve immediately without allocating a
580+
// read request. The result settles synchronously, so leaving
575581
// state.current undefined matches the state the slow path reaches
576582
// once its read request callbacks have settled.
577583
conststream=reader[kState].stream;
578-
if(!state.done&&stream!==undefined){
584+
if(!state.done&&stream!==undefined&&
585+
stream[kState].state==='readable'){
579586
constcontroller=stream[kState].controller;
580-
if(stream[kState].state==='readable'&&
581-
isReadableStreamDefaultController(controller)&&
582-
controller[kState].queue.length>0){
583-
stream[kState].disturbed=true;
584-
constchunk=dequeueValue(controller);
585-
586-
if(controller[kState].closeRequested&&
587-
!controller[kState].queue.length){
588-
readableStreamDefaultControllerClearAlgorithms(controller);
589-
readableStreamClose(stream);
590-
}else{
591-
readableStreamDefaultControllerCallPullIfNeeded(controller);
587+
if(isReadableStreamDefaultController(controller)){
588+
if(controller[kState].queue.length>0){
589+
stream[kState].disturbed=true;
590+
constchunk=dequeueValue(controller);
591+
592+
if(controller[kState].closeRequested&&
593+
!controller[kState].queue.length){
594+
readableStreamDefaultControllerClearAlgorithms(controller);
595+
readableStreamClose(stream);
596+
}else{
597+
readableStreamDefaultControllerCallPullIfNeeded(controller);
598+
}
599+
600+
returnPromiseResolve({done: false,value: chunk});
592601
}
602+
}elseif(controller[kState].queueTotalSize>0){
603+
// Byte controller with buffered data: same shape as above via
604+
// the queue-filled arm of the byte controller's pull steps.
605+
stream[kState].disturbed=true;
606+
returnPromiseResolve({
607+
done: false,
593608

594-
returnPromiseResolve({done: false,value: chunk});
609+
value: readableByteStreamControllerDequeueChunk(controller),
610+
});
595611
}
596612
}
597613
state.current=nextSteps();
@@ -914,24 +930,36 @@ class ReadableStreamDefaultReader {
914930
conststream=this[kState].stream;
915931
constcontroller=stream[kState].controller;
916932

917-
// Fast path: if data is already buffered in a default controller,
933+
// Fast path: if data is already buffered in the controller's queue,
918934
// return a resolved promise immediately without creating a read request.
919935
// This is spec-compliant because read() returns a Promise, and
920936
// Promise.resolve() callbacks still run in the microtask queue.
921-
if(stream[kState].state==='readable'&&
922-
isReadableStreamDefaultController(controller)&&
923-
controller[kState].queue.length>0){
924-
stream[kState].disturbed=true;
925-
constchunk=dequeueValue(controller);
937+
if(stream[kState].state==='readable'){
938+
if(isReadableStreamDefaultController(controller)){
939+
if(controller[kState].queue.length>0){
940+
stream[kState].disturbed=true;
941+
constchunk=dequeueValue(controller);
942+
943+
if(controller[kState].closeRequested&&!controller[kState].queue.length){
944+
readableStreamDefaultControllerClearAlgorithms(controller);
945+
readableStreamClose(stream);
946+
}else{
947+
readableStreamDefaultControllerCallPullIfNeeded(controller);
948+
}
926949

927-
if(controller[kState].closeRequested&&!controller[kState].queue.length){
928-
readableStreamDefaultControllerClearAlgorithms(controller);
929-
readableStreamClose(stream);
930-
}else{
931-
readableStreamDefaultControllerCallPullIfNeeded(controller);
950+
returnPromiseResolve({done: false,value: chunk});
951+
}
952+
}elseif(controller[kState].queueTotalSize>0){
953+
// Byte controller with buffered data: mirror the queue-filled arm
954+
// of its pull steps (which never consults pendingPullIntos) minus
955+
// the read request.
956+
stream[kState].disturbed=true;
957+
returnPromiseResolve({
958+
done: false,
959+
960+
value: readableByteStreamControllerDequeueChunk(controller),
961+
});
932962
}
933-
934-
returnPromiseResolve({value: chunk,done: false});
935963
}
936964

937965
// Slow path: create request and go through normal flow
@@ -3040,9 +3068,23 @@ function readableByteStreamControllerEnqueue(controller, chunk) {
30403068
}
30413069
}
30423070

3043-
if(readableStreamHasDefaultReader(stream)){
3044-
readableByteStreamControllerProcessReadRequestsUsingQueue(controller);
3045-
if(!readableStreamGetNumReadRequests(stream)){
3071+
// Single consolidated pass over the reader state. The spec routes this
3072+
// through HasDefaultReader / ProcessReadRequestsUsingQueue /
3073+
// GetNumReadRequests / FulfillReadRequest, which would re-run the same
3074+
// reader brand check and re-load the read request list four times on
3075+
// this per-chunk path.
3076+
const{ reader }=stream[kState];
3077+
if(reader!==undefined&&
3078+
reader[kState]!==undefined&&
3079+
reader[kType]==='ReadableStreamDefaultReader'){
3080+
const{ readRequests }=reader[kState];
3081+
if(readRequests.length&&controller[kState].queueTotalSize>0){
3082+
// Only possible when data was enqueued while the stream was not
3083+
// being read; read requests otherwise never coexist with a
3084+
// non-empty queue.
3085+
readableByteStreamControllerProcessReadRequestsUsingQueue(controller);
3086+
}
3087+
if(!readRequests.length){
30463088
readableByteStreamControllerEnqueueChunkToQueue(
30473089
controller,
30483090
transferredBuffer,
@@ -3056,7 +3098,8 @@ function readableByteStreamControllerEnqueue(controller, chunk) {
30563098
}
30573099
consttransferredView=
30583100
newUint8Array(transferredBuffer,byteOffset,byteLength);
3059-
readableStreamFulfillReadRequest(stream,transferredView,false);
3101+
constreadRequest=ArrayPrototypeShift(readRequests);
3102+
readRequest[kChunk](transferredView);
30603103
}
30613104
}elseif(readableStreamHasBYOBReader(stream)){
30623105
readableByteStreamControllerEnqueueChunkToQueue(
@@ -3391,22 +3434,28 @@ function readableByteStreamControllerCancelSteps(controller, reason) {
33913434
returnresult;
33923435
}
33933436

3394-
functionreadableByteStreamControllerFillReadRequestFromQueue(controller,readRequest){
3395-
const{
3396-
queue,
3397-
queueTotalSize,
3398-
}=controller[kState];
3399-
assert(queueTotalSize>0);
3437+
// Dequeues the first chunk of the byte queue as a Uint8Array view,
3438+
// handling queue drain (close-on-empty or pull) before the view is
3439+
// created. This is the [[queueTotalSize]] > 0 arm of the byte
3440+
// controller's pull steps; it is also called directly from the
3441+
// buffered fast paths in ReadableStreamDefaultReader.read() and the
3442+
// async iterator, which resolve with the view without allocating a
3443+
// read request.
3444+
functionreadableByteStreamControllerDequeueChunk(controller){
3445+
assert(controller[kState].queueTotalSize>0);
34003446
const{
34013447
buffer,
34023448
byteOffset,
34033449
byteLength,
3404-
}=ArrayPrototypeShift(queue);
3450+
}=ArrayPrototypeShift(controller[kState].queue);
34053451

34063452
controller[kState].queueTotalSize-=byteLength;
34073453
readableByteStreamControllerHandleQueueDrain(controller);
3408-
constview=newUint8Array(buffer,byteOffset,byteLength);
3409-
readRequest[kChunk](view);
3454+
returnnewUint8Array(buffer,byteOffset,byteLength);
3455+
}
3456+
3457+
functionreadableByteStreamControllerFillReadRequestFromQueue(controller,readRequest){
3458+
readRequest[kChunk](readableByteStreamControllerDequeueChunk(controller));
34103459
}
34113460

34123461
functionreadableByteStreamControllerProcessReadRequestsUsingQueue(controller){

β€Žlib/internal/webstreams/util.jsβ€Ž

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ const {
77
ArrayPrototypePush,
88
ArrayPrototypeShift,
99
AsyncIteratorPrototype,
10+
DataViewPrototypeGetBuffer,
11+
DataViewPrototypeGetByteLength,
12+
DataViewPrototypeGetByteOffset,
1013
FunctionPrototypeCall,
1114
MathMax,
1215
NumberIsNaN,
1316
PromisePrototypeThen,
1417
PromiseReject,
1518
PromiseResolve,
16-
ReflectGet,
1719
Symbol,
20+
TypedArrayPrototypeGetBuffer,
21+
TypedArrayPrototypeGetByteLength,
22+
TypedArrayPrototypeGetByteOffset,
1823
Uint8Array,
1924
}=primordials;
2025

@@ -41,6 +46,10 @@ const {
4146

4247
constassert=require('internal/assert');
4348

49+
const{
50+
isDataView,
51+
}=require('internal/util/types');
52+
4453
const{
4554
validateFunction,
4655
}=require('internal/validators');
@@ -93,20 +102,29 @@ function customInspect(depth, options, name, data) {
93102
return`${name}${inspect(data,opts)}`;
94103
}
95104

96-
// These are defensive to work around the possibility that
97-
// the buffer, byteLength, and byteOffset properties on
98-
// ArrayBuffer and ArrayBufferView's may have been tampered with.
105+
// These use the original prototype getters so that user tampering with
106+
// the buffer, byteLength, and byteOffset properties on ArrayBuffer and
107+
// ArrayBufferView's is not observed. They run once or more per chunk on
108+
// every byte-stream path, so they must not go through a reflective get
109+
// (the previous view.constructor.prototype lookup was both slower and
110+
// spoofable via a user-defined .constructor).
99111

100112
functionArrayBufferViewGetBuffer(view){
101-
returnReflectGet(view.constructor.prototype,'buffer',view);
113+
returnisDataView(view) ?
114+
DataViewPrototypeGetBuffer(view) :
115+
TypedArrayPrototypeGetBuffer(view);
102116
}
103117

104118
functionArrayBufferViewGetByteLength(view){
105-
returnReflectGet(view.constructor.prototype,'byteLength',view);
119+
returnisDataView(view) ?
120+
DataViewPrototypeGetByteLength(view) :
121+
TypedArrayPrototypeGetByteLength(view);
106122
}
107123

108124
functionArrayBufferViewGetByteOffset(view){
109-
returnReflectGet(view.constructor.prototype,'byteOffset',view);
125+
returnisDataView(view) ?
126+
DataViewPrototypeGetByteOffset(view) :
127+
TypedArrayPrototypeGetByteOffset(view);
110128
}
111129

112130
functioncloneAsUint8Array(view){

0 commit comments

Comments
Β (0)