whatwg / streams

Streams Standard
https://streams.spec.whatwg.org/
Other
1.34k stars 159 forks source link

Byte streams: write up design rationales and vision for how they are used/what they enable #177

Closed domenic closed 8 years ago

domenic commented 10 years ago

A few things I can think of worth including:

tyoshino commented 9 years ago

Updated domenic's first comment to use as a task list.

domenic commented 9 years ago

I started on this a bit by documenting some recently-discovered constraints in Requirements.md, although it was pretty quick and not too comprehensive.

tyoshino commented 9 years ago

I'd like to prototype the unified byte stream underlying source idea we discussed today.

domenic commented 9 years ago

source calls delegate.enqueue() to enqueue an ArrayBufferView to the [[queue]]

I wonder if we should have the stream implementation enforce the type as Uint8Array.

If the bytes are not sufficient to fill up view fully, source.read(newView) is called where newView specifies the not yet filled region

Alternately, we could just fill up view as much as possible, and have the returned ArrayBufferView be of a shorter byteLength. Just like if read(2) returns a smaller number of bytes than were passed in.

The more interesting case is: if [[queue]] is not empty, but its first chunk is of a larger size than view. Then we need to copy part of that chunk into view and leave the rest in the queue---at least, if implemented in JS. (C++ implementers might have more tools available, to avoid copies, and instead just make the returned Uint8Arrays map to the right memory regions.)

Agreed with everything else. Good detailed analysis.

It's interesting how much shared infrastructure there will be between ReadableByteStream and ReadableStream. I wonder if ReadableByteStream does end up just being a subclass after all. Hmm.

tyoshino commented 9 years ago

I wonder if we should have the stream implementation enforce the type as Uint8Array.

Added to #300.

Alternately, we could just fill up view as much as possible, ...

Sounds good. Added to #300.

The more interesting case is: if [[queue]] is not empty, but its first chunk is of a larger size than view. ...

This makes sense for the non-BYOB reader if we add a size-limiting parameter to its read() method as you discussed last week.

For BYOB, anyway we need to append the data to the given buffer as the consumer may already have written some data in the encapsulated ArrayBuffer and expect the new data to be added right next to the data the region of which is specified by the given view.

Another point I wonder how we should deal with is, whether the partially consumed bytes in the large buffer should be considered as freed or not from backpressure point of view. If we cannot free the partially consumed region, it continues to have memory pressure. So, should we refrain from pulling?

domenic commented 9 years ago

Sorry for the delay on replying to this. It slipped off my to-do list somehow.

Another point I wonder how we should deal with is, whether the partially consumed bytes in the large buffer should be considered as freed or not from backpressure point of view. If we cannot free the partially consumed region, it continues to have memory pressure. So, should we refrain from pulling?

I think we should be able to free the consumed region. Even in JavaScript you could ArrayBuffer.transfer to a smaller buffer which would let the GC collect the consumed part.

domenic commented 8 years ago

This seems to be pretty well taken care of in the latest spec, with good examples and interop.