58 points · by sugawarayuuta · 2y ago · 33 comments
Since that's the point of comparison, what's the Go standard library's strategy? Is it inherently slower than this or does it behave better for different scenarios?
How does this compare to using simd (like simdutf8)?
[1] https://github.com/sugawarayuuta/charcoal/blob/main/docs/ind...
It is basically using 64 bit integer operations to check 8 bytes at a time.
UTF-32 always uses 32 bits per character, so you can easily paralelize it because you can easily tell where a character starts in a bit stream.
UTF-16 can have 16 or 32 bits, so if you have a sequence 2 characters of 16-32 bits, or in UTF-8's case, a sequence of 3 characters that are 8-16-16 bits, they won't fit neatly in a box. You can't just say char_ptr + 32 to land on the 32nd character, making it much harder to paralelize.
In addition, UTF-32 can cause issues on the network due to byte ordering (little endian vs. big endian). There’s really two encodings, UTF-32LE and UTF-32BE.
For all of these reasons, the world has largely standardized on UTF-8, plus for historical reasons UTF-16 in the JVM, CLR, Windows, and JavaScript.
Do you have any plans on proposing to upstream some of your work?
Curious to know if there are any known caveats?
Otherwise, go for it. May seem daunting, but it's not. I've submitted a couple of proposals and patches; pretty happy with the process.
If you have invalid utf-8, go fix whatever code it is that is producing invalid utf-8...
And obviously you don't want to spend more time doing this than necessary.
Not all sequences of bytes are valid UTF-8. A UTF-8 decoder should be prepared for:
invalid bytes
an unexpected continuation byte
a non-continuation byte before the end of the character
the string ending before the end of the character (which can happen in simple string truncation)
an overlong encoding
a sequence that decodes to an invalid code point
https://en.wikipedia.org/wiki/UTF-8