4 ms·
Wait. A programming language written this side of 2010 stored strings in a bastardized sometimes-ansii sometimes-utf16? blink Why on earth not just use utf8 f
by ComputerGuru 8y ago
Wait. A programming language written this side of 2010 stored strings in a bastardized sometimes-ansii sometimes-utf16?
blink
Why on earth not just use utf8 from the start? Surely no micro-optimization made possible by their choice could be worth such a convoluted design?
- dep_b 8y agoProbably backwards compatibility with NSString
- Aqua_Geek 8y agoIt might be due to interop with Objective-C — Foundation's NSString uses UTF-16: https://developer.apple.com/documentation/foundation/nsstring https://developer.apple.com/documentation/foundation/nsstrin...
- ubernostrum 8y agoPython (as of version 3.3) does something similar. Source code is assumed UTF-8 by default, and all other string creation requires explicit conversion from bytes. So when creating and storing a string object, Python looks at the widest code point that will be in the string, and chooses the narrowest encoding -- latin-1, UCS-2, or UCS-4 -- that can store the string as fixed-width units. This gets all the benefits people usually want from "just use UTF-8", and then some -- strings containing only code points in the latin-1 range (not just the ASCII range) take one byte per code point -- and also keeps the property of code units being fixed width no matter what's in the string. Which means programmers don't have to deal with leaky abstractions that are all too common in languages that expose "Unicode" strings which are really byte sequences in some particular encoding of Unicode.
- rectang 8y agoThe tradeoff is that at unpredictable moments, memory requirements for string content can quadruple. Python is inexorably committed to the idioms which depend assume fixed width characters -- there's no persuading the community to use e.g. functions to obtain substrings rather than array indexes. So this is an understandable design decision.
- ubernostrum 8y agoassume fixed width characters Python strings are not iterables of characters. They're iterables of Unicode code points. This is why leaking the internal storage up to the programmer is problematic; prior to 3.3, you'd routinely see artifacts of the internal storage (like surrogate pairs) which broke the "strings are iterables of code points" abstraction. e.g. functions to obtain substrings rather than array indexes Strings are iterables of code points. Indexing into a string yields the code point at the requested index. While I'd like to have an abstraction for sequences of graphemes, strings-as-code-points is not the worst thing that a language could do. And all the "just use this thing that does exactly the same thing with a different name because I want indexing/length but also want to insist people don't call them that" is frankly pointless.
- rectang 8y ago> And all the "just use this thing that does exactly the same thing with a different name because I want indexing/length but also want to insist people don't call them that" is frankly pointless. Array index syntax over variable width data is problematic: either deceptively expensive -- O(n) for what looks like an O(1) operation -- or wrong. I suspect in that we agree. As for the alternative, I'm talking about Tom Christiansen's argument here: https://bugs.python.org/msg142041 https://bugs.python.org/msg142041 To paraphrase Tom's examples, this usage of array indexes is more idiomatic... s = "for finding the biggest of all the strings" x_at = s.index("big") y_at = s.index("the", x_at) some = s[x_at:y_at] print("GOT", some) ... than this, which the Python community would never adopt: import re s = "for finding the biggest of all the strings" some = re.search("(big.*?)the", s).group(1) print("GOT", some) The first involves more logical actions indexing into the string. If those operations are both O(1) and correct, then that's not a serious problem, which is what justifies the Python 3.3+ design. However, in terms of language design for handling Unicode strings, I prefer the tradeoffs of the second idiom: a single O(n) operation which is relatively easy to anticipate and plan for, rather than unpredictable memory blowups.
- masklinn 8y ago
- josephg 8y agoThere's a huge performance benefit to using a complex string type like this in user-facing applications. The reason is that most strings are really small - like, a few bytes small. When strings are smaller than pointers, allocating them on the heap is silly and inefficient. So NSString uses a bunch of wacky encodings internally to pack very small strings into the 8 byte pointer that would otherwise be used to point to an object on the heap. I suspect that microoptimizations like this have done a lot of the work in making iOS outperform Android clock-for-clock. Although I suppose right now thats less of a big deal given how fast their A-series chips are as well.
- kevin_thibedeau 8y agoMost of iOS is native code. That's why it beats Android.
- saagarjha 8y ago> So NSString uses a bunch of wacky encodings internally to pack very small strings into the 8 byte pointer that would otherwise be used to point to an object on the heap. Nitpick: these pointers are odd, so they can't point to a valid object on the heap (malloc is 16-byte aligned on macOS). So it makes sense to reuse them by tagging them and packing a string (or number, or date) into the remaining bits to save on an allocation.
- ken 8y agoThe article says: Chinese text is "over 3x faster than before", and "ASCII benefits even more, despite the old model having a dedicated storage representation and fast paths for ASCII-only strings". That sounds like a pretty good reason to me.
- akvadrako 8y agoUTF-8 is very inefficient for some character sets - of course it shouldn't be the only option.
- masklinn 8y agoBecause Swift was built on the foundations (heh) of and to cheaply interact with Objective-C, and Obj-C is of that misguided generation of languages which believed UCS2 was a good idea, and later had to back-define it into UTF-16.