Hacker News new | ask | show | jobs
by tredre3 27 days ago
> They did it again to LSP, and that was in 2016.

That one is explainable by the fact that it was developed for VS Code, a typescript/javascript project. Javascript uses UTF-16 for strings (for historical reasons unrelated to Microsoft, though they are the same reasons that made MS use UCS-2 during the same time period).

2 comments

https://en.wikipedia.org/wiki/UTF-16

> UTF-16 is used by the Windows API, and by many programming environments such as Java and Qt. The variable-length character of UTF-16, combined with the fact that most characters are not variable-length (so variable length is rarely tested), has led to many bugs in software, including in Windows itself.

Absolutely, UTF-16 is bad and inexcusable in most contexts. Perhaps you misinterpreted my comment or I wasn't expressing myself properly.

I was trying to say: If all the components in your stack are javascript-based, it makes sense that your RPC-API would also use UTF-16. It wasn't Microsoft pushing it for historical Windows reasons as GP implied, it was dictated by a JS stack they chose but didn't create.

I understood, I was just providing context. Windows, JavaScript, Java are all UTF-16. 65k characters is all you need.
I always thought the js libraries throwing emojies around was stupid.

Then I finally sat down to understand enough JS for LLM slop and the having rockets all over the place wasn't just cringe but the quickest way to show you handled utf 16.

> Javascript uses UTF-16 for strings

I don’t think the spec requires that. charAt indexes on UTF-16 code points, but I don’t remember ever seeing that it has to do that in O(1) time, so that doesn’t imply the implementation has to use UTF-16 internally.

Also, for modern JavaScript, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... says:

“Note: Avoid re-implementing the solutions above using charAt(). The detection of lone surrogates and their pairing is complex, and built-in APIs may be more performant as they directly use the internal representation of the string. Install a polyfill for the APIs mentioned above if necessary.”

Because of that, I would hope most modern implementations to at least have a variant internal representation that is similar to UTF-8.

I wouldn't expect them to. Let us know if you find otherwise!
https://iliazeus.lol/articles/js-string-optimizations-en/

“The ECMAScript standard defines a string as a sequence of 16-bit elements. But in a lot of cases, storing 2 bytes per character is a waste of memory. In fact, quite a lot of strings in most code will be limited to ASCII. That is why V8 has both one-byte and two-byte strings”

https://github.com/v8/v8/blob/main/src/objects/string.h confirms that.

That sounds like strong evidence that V8 doesn't have an internal representation similar to UTF-8: it has one-byte encodings, and it has UTF-16, both of which are flat arrays of UTF-16 elements (and of code points, except for surrogates).

Although the article doesn't explain this, the one-byte encodings seem to be limited to Latin-1 (though not merely ASCII), in which the values of the bytes are literally the Unicode codepoints. Here we see that ASCII strings and Latin-1 strings are ONE_BYTE_INTERNALIZED_STRING_TYPE, but Greek and Latin-2 strings are INTERNALIZED_STRING_TYPE:

    : ~; node --allow-natives-syntax
    Welcome to Node.js v18.19.0.
    Type ".help" for more information.
    > %DebugPrint("hello")
    DebugPrint: 0x32c6da5c48f9: [String] in OldSpace: #hello
    0xee8766c1369: [Map] in ReadOnlySpace
     - type: ONE_BYTE_INTERNALIZED_STRING_TYPE
     - instance size: variable
     - elements kind: HOLEY_ELEMENTS
     - unused property fields: 0
     - enum length: invalid
     - stable_map
     - back pointer: 0x0ee8766c15b9 <undefined>
     - prototype_validity cell: 0
     - instance descriptors (own) #0: 0x0ee8766c1269 <Other heap object (STRONG_DESCRIPTOR_ARRAY_TYPE)>
     - prototype: 0x0ee8766c1339 <null>
     - constructor: 0x0ee8766c1339 <null>
     - dependent code: 0x0ee8766c1251 <Other heap object (WEAK_ARRAY_LIST_TYPE)>
     - construction counter: 0

    'hello'
    > %DebugPrint("dénouement")
    DebugPrint: 0x19b3181b5941: [String] in OldSpace: #d\xe9nouement
    0xee8766c1369: [Map] in ReadOnlySpace
     - type: ONE_BYTE_INTERNALIZED_STRING_TYPE
     - instance size: variable
     - elements kind: HOLEY_ELEMENTS
     - unused property fields: 0
     - enum length: invalid
     - stable_map
     - back pointer: 0x0ee8766c15b9 <undefined>
     - prototype_validity cell: 0
     - instance descriptors (own) #0: 0x0ee8766c1269 <Other heap object (STRONG_DESCRIPTOR_ARRAY_TYPE)>
     - prototype: 0x0ee8766c1339 <null>
     - constructor: 0x0ee8766c1339 <null>
     - dependent code: 0x0ee8766c1251 <Other heap object (WEAK_ARRAY_LIST_TYPE)>
     - construction counter: 0

    'dénouement'
    > %DebugPrint("dénouement∫")
    DebugPrint: 0x19b3181b7f91: [String] in OldSpace: u#d\xe9nouement\u222b
    0xee8766c2e69: [Map] in ReadOnlySpace
     - type: INTERNALIZED_STRING_TYPE
     - instance size: variable
     - elements kind: HOLEY_ELEMENTS
     - unused property fields: 0
     - enum length: invalid
     - stable_map
     - back pointer: 0x0ee8766c15b9 <undefined>
     - prototype_validity cell: 0
     - instance descriptors (own) #0: 0x0ee8766c1269 <Other heap object (STRONG_DESCRIPTOR_ARRAY_TYPE)>
     - prototype: 0x0ee8766c1339 <null>
     - constructor: 0x0ee8766c1339 <null>
     - dependent code: 0x0ee8766c1251 <Other heap object (WEAK_ARRAY_LIST_TYPE)>
     - construction counter: 0

    'dénouement∫'
    > %DebugPrint("κοκαλος")
    DebugPrint: 0x268799d03a79: [String] in OldSpace: u#\u03ba\u03bf\u03ba\u03b1\u03bb\u03bf\u03c2
    0xee8766c2e69: [Map] in ReadOnlySpace
     - type: INTERNALIZED_STRING_TYPE
     - instance size: variable
     - elements kind: HOLEY_ELEMENTS
     - unused property fields: 0
     - enum length: invalid
     - stable_map
     - back pointer: 0x0ee8766c15b9 <undefined>
     - prototype_validity cell: 0
     - instance descriptors (own) #0: 0x0ee8766c1269 <Other heap object (STRONG_DESCRIPTOR_ARRAY_TYPE)>
     - prototype: 0x0ee8766c1339 <null>
     - constructor: 0x0ee8766c1339 <null>
     - dependent code: 0x0ee8766c1251 <Other heap object (WEAK_ARRAY_LIST_TYPE)>
     - construction counter: 0

    'κοκαλος'
    > %DebugPrint("ł")
    DebugPrint: 0x268799d08681: [String] in OldSpace: u#\u0142
    0xee8766c2e69: [Map] in ReadOnlySpace
     - type: INTERNALIZED_STRING_TYPE
     - instance size: variable
     - elements kind: HOLEY_ELEMENTS
     - unused property fields: 0
     - enum length: invalid
     - stable_map
     - back pointer: 0x0ee8766c15b9 <undefined>
     - prototype_validity cell: 0
     - instance descriptors (own) #0: 0x0ee8766c1269 <Other heap object (STRONG_DESCRIPTOR_ARRAY_TYPE)>
     - prototype: 0x0ee8766c1339 <null>
     - constructor: 0x0ee8766c1339 <null>
     - dependent code: 0x0ee8766c1251 <Other heap object (WEAK_ARRAY_LIST_TYPE)>
     - construction counter: 0

    'ł'