Re: [whatwg/streams] Garbage-free streaming fetch into a WebAssembly (Shared)ArrayBuffer? (#1057)

> This code does not generate excessive typed array views

While **your code** does not construct extra typed array views, **the stream itself** definitely does. Every `data.value` is a new `Uint8Array`, which will be garbage collected after your `onChunkDownloaded` returns.

Moreover, the `Uint8Array`s constructed by the stream will all have their own separate backing `ArrayBuffer` (in `view.buffer`). And this is precisely where most of the overhead comes from: while a typed array like `Uint8Array` is fairly lightweight (it merely *points* to a slice of data), an `ArrayBuffer` needs to allocate its backing memory to actually hold the data.

With a BYOB reader, you construct a new `ArrayBuffer` (and its backing memory) *once*, and then you let the stream fill in a *view* on that buffer. Yes, the stream has to construct a new view when it returns the result (because it might not have filled the *whole* view), but again: these views are lightweight.

> I wonder if there would be a way to get to a JS GC free + zero copy way of downloading files?

I doubt you can eliminate *all* garbage collection, and I don't think that should be a goal. Even if you were to eliminate the typed arrays by making the API closer to something like C++'s `fread` (e.g. `read(buffer, offset, length)`), that call would still return a `Promise` - which will *also* need to be GC'd after it resolves.

Trying to get rid of all these small objects would make the API much harder to use, since you'd have to get rid of a lot of useful abstractions like typed arrays and promises. I'd argue that this would feel "alien" for most JavaScript developers.

-- 
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/1057#issuecomment-661795361

Received on Tuesday, 21 July 2020 11:18:48 UTC