Re: [whatwg/encoding] TextEncoder and TextDecoder memory issue due resizable/shared buffers (Issue #344)

WebReflection left a comment (whatwg/encoding#344)

> Right?

the first link doesn't explicitly tell me the SharedArrayBuffer or the ArrayBuffer are resizable and the commit link, or the link beside it, gives me a 404.

the second link does not use growable SharedArrayBuffer so I am not sure if those are extra bugs on top of this one.

> Also, it would be much better to state upfront what problem you are trying to solve.

I want to use a resizable SharedArrayBuffer with `TextEncoder.encode` and `TextDecoder.decode` and that is not possible, summarized in this gist which contain a setup for references and tests that either pass or fail when ArrayBuffer or SharedArrayBuffer are resizable: https://gist.github.com/WebReflection/3324b5ac79768c85efbf3b725d6a9d73

The minimal reproducible case is:

```js
const ref = new TextEncoder().encode("hello");

const gsab = new Uint8Array(new SharedArrayBuffer(5, { maxByteLength: 16 }));
gsab.set(ref, 0);

// decoder failing
const decoder = new TextDecoder;
decoder.decode(gsab); // ❌
decoder.decode(gsab.subarray(0)); // ❌

// encoder failing
const encoder = new TextEncoder;
encoder.encodeInto("hello", gsab); // ❌
encoder.encodeInto("hello", gsab.subarray(0)); // ❌
```

If I don't use these primitives or I create manually a typed array and then set it into the SharedArrayBuffer it works but that means duplicated RAM which is not an option on constrained devices when the serialized data could contain also blobs or buffers that will grow twice as much and requires GC to kick in before the memory can be used again.

That isthe issue, I ended up ignoring these APIs because these don't help me with SharedArrayBuffer but it would be great if these API could work with resizable buffers like DataView or other TypedArray can already.

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

Message ID: <whatwg/encoding/issues/344/2827038314@github.com>

Received on Thursday, 24 April 2025 09:57:56 UTC