Re: [whatwg/streams] Brainstorming a buffer-reusing version of writable streams (#495)

> The underlying sink doesn't see the buffer until the other buffers in the queue ahead of it have been consumed. So it doesn't get detached immediately. So there is a bit of an async race (not a multithreaded one) between any code that manipulates the buffer, and the underlying sink.

For the buffer returning variant of WritableStream, write() should detach the given buffer immediately I think.

> The underlying sink has no opportunity to give the buffer back to the producer if they want to reuse it.

Did you mean the time until resolution of the returned promise? I understood so as the example you provided is explaining such a scenario.

> We have to reallocate new typed arrays every time through the loop; we cannot reuse the same one.

Good point. tzik also pointed out that if we're using a big ArrayBuffer e.g. for Web Assembly, always transferring a given ArrayBuffer would be very problematic, before. Sorry for not creating an issue for discussing that earlier.

The BYOB readable stream also has the problem. People may want to issue a new async BYOB read on region X and then do some stuff on the other region containing unprocessed contents.

@ricea's solution is simple and good.

I think this kind of usage needs some evolution on the ArrayBuffer API side, e.g. partial detaching. For example, we could introduce a new API like the following:

- a new type of TypedArray named UniqueView lets the code to manipulate contents of the associated UniqueViewArrayBuffer.
- UniqueViewArrayBuffer is never transferred and only UniqueView (normal TypedArray is not available for it) can be created for UniqueViewArrayBuffer.
- UniqueViewArrayBuffer can also be backed by SharedArrayBuffer.
- UniqueView can be transferred and there can be only one UniqueView for each byte in a UniqueViewArrayBuffer which covers the byte.
- uniqueView.return() revokes the view.


-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/streams/issues/495#issuecomment-242273551

Received on Thursday, 25 August 2016 03:43:13 UTC