Could String internalize an ArrayBuffer?
On 12/5/15 8:44 AM, Coroutines wrote:
What I want is to be able to view a String through a typed array without duplicating the memory/contents of that string.
The big problem with this is that the actual in-memory representations of strings and arraybuffers can be quite different in implementations right now.
For example, strings typically have a complicated representation that involves multiple fragments, ropes, etc, to make string concatenation fast. Not only that, but to save memory in various engines some strings that only contain values less than 256 are stored with underlying 8-bit units, not 16-bit ones. There has also been talk of using something like UTF-8 for the internal string representation in some engines, to reduce the impedance mismatch with engine consumers (like a browser rendering engine).
Arraybuffers, on the other hand, are typically an actual contiguous chunk of memory to make access fast. And of course they just exposed whatever bytes they contain.
Response inline
On Sun, Dec 6, 2015 at 12:19 AM, Boris Zbarsky <bzbarsky at mit.edu> wrote:
On 12/5/15 8:44 AM, Coroutines wrote:
What I want is to be able to view a String through a typed array without duplicating the memory/contents of that string.
The big problem with this is that the actual in-memory representations of strings and arraybuffers can be quite different in implementations right now.
For example, strings typically have a complicated representation that involves multiple fragments, ropes, etc, to make string concatenation fast. Not only that, but to save memory in various engines some strings that only contain values less than 256 are stored with underlying 8-bit units, not 16-bit ones. There has also been talk of using something like UTF-8 for the internal string representation in some engines, to reduce the impedance mismatch with engine consumers (like a browser rendering engine).
AFAIK most engines already do this.
Arraybuffers, on the other hand, are typically an actual contiguous chunk of memory to make access fast. And of course they just exposed whatever bytes they contain.
And V8, if I understand correctly, uses effectively a linked tree
(closer to a web) of single-character pairs for their strings. This is
not at all contiguous, but makes string slicing and building very
cheap to do. The only realistic way to have a string stored in an
ArrayBuffer is to have it encoded like a C string (er... char*
).
On Sat, Dec 5, 2015 at 9:19 PM, Boris Zbarsky <bzbarsky at mit.edu> wrote:
For example, strings typically have a complicated representation that involves multiple fragments, ropes, etc, to make string concatenation fast.
Ahh, I'm not sure why I didn't think of that... I come from Lua and they do something similar. My favorite data structure has always been:
en.wikipedia.org/wiki/Unrolled_linked_list
Anyway, now my question/proposal seems quite naive.
Conclusion: Just create a duplicate ArrayBuffer from a String and be done. :-)
Coroutines schrieb:
What I want is to be able to view a String through a typed array without duplicating the memory/contents of that string.
The largest problem I see with this is that strings are immutable while typed arrays / buffers do allow indexed write access. So before using strings instead of buffers (or efficiently creating buffers from strings), we'd need to introduce something like ConstTypedArrays.
, Bergi
I'm not sure what I'm asking for/looking for.
What I want is to be able to view a String through a typed array without duplicating the memory/contents of that string.
As I understand it, typed arrays operate over an ArrayBuffer, not Strings.
Does anyone know of a way to do this?
If it's not possible, would it make sense to have String inherit from or internally contain an ArrayBuffer that could be gotten somehow? Maybe with something lie ::valueOf() ?
In the last week I've been playing with typed arrays so I can compare 2 files by word-sized types. The problem I'm having is when I receive a String and not an ArrayBuffer from a third-party library. I'd love it if I could avoid converting the String into a duplicated ArrayBuffer.
Cheers