So not "in memory" (although obviously it is in memory) it's when the goal is to fit the totality of the string plus a tag into a register.
If they can't completely encode the 10-11 character value using the 5-bit charset it spills to UTF-16 on the heap.
> It's interesting how network formats and in-memory formats kinda converge in design, because retrieving data from memory to CPU is so expensive now.
Honestly Strings are probably one of the worst examples of this. Strings have like 50 different representations depending on what you're trying to do with them.
1. Wire format for storage and retrieval is generally UTF-8.
2. If you're trying to parse a string you generally want to expand it into code points.
3. If you're trying to sort or compare strings you generally want to expand that into a normalized form like NFC or NFD.
4. If you're trying to edit text, you generally want grapheme clusters.
5. If you're trying to present text, you generally want pixels.
The way to think about it is: "there is no such things as the length of a string without context." Strings are basically a bytecode format for representing concepts.
[1] https://developer.apple.com/documentation/foundation/nsstrin...