You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Native support for wchar_t* strings (internationalisation) #1691
Describe the solution you'd like
As more and more systems are supporting internationalisation, wchar_t* is getting used more often then char* for strings. Can you please add native support for wchar_t* strings? Thanks
Describe alternatives you've considered
Alternatives is to convert string each time, on the fly but it is not feasible as memory needs to be allocated/deallocated causing overheads and problem with the responsibility of code that ideally should do cleanups. For effective use of Simdjson its ideal to have it support wchar_t* strings natively.
The simdjson library is UTF-8 and frequently points back at the original document which is, by the standard, UTF-8. This will not change. We will not support non-standard JSON inputs that rely on UTF-16 or UTF-32. JSON is UTF-8 as a requirement. If you work with JSON in a performance-sensitive manner, working with UTF-8 directly will always be faster and better. The UTF-8 is the native format of the web.
About types... The wchar_t predates C++11 and is system-specific. It is not a type that we would ever support because it is implementation-defined... it is not the same type under Linux and under Windows.
A better option are the char16_t and char32_t types which correspond to UTF-16 and UTF-32 formats respectively.
There is also a type corresponding to UTF-8 in C++20 (char8_t), but we use char for that purpose under simdjson, as is often done. In the future, we could just replace it with char8_t but we have abstained from doing so until now because not everyone has C++20 support.
No matter what, since the original document must be UTF-8, the production of UTF-16 and UTF-32 string requires memory allocation and transcoding. It is not possible to have UTF-16 or UTF-32 support natively: someone must allocate memory and transcode the UTF-8 strings there.
We decided that it was best to provide the transcoding functions as a separate feature. Thus we created the simdutf library. The simdutf library offers fast (SIMD-accelerated) transcoding functions.
For optimal performance, I expect that most people will want to control memory allocation and transcoding. Though it is relatively efficient, it is not free.
It would be doable to support UTF-16 and UTF-32 transcoding from within simdjson , for convenience, but all it would be do is allocate a string and transcode into it. There is no magic. It is not entirely clear what benefit, other than some convenience, that an integration of simdutf into simdjson would bring. If you have ideas or can elaborate, that would be great.
Can a preprocessor be used to use something else instead of char, so we could simply change the preprocessor definition to compile as a the desired char type?
It would have been good if SimdJSON supported it natively, so we could use it without continuously transcoding all the time
Indeed. You will find that simdutf relies on char16_t extensively and it requires C++11.
Yes, I meant UTF-16. I assumed SimdJSON could do this on the fly with little changes. Can a preprocessor be used to use something else instead of char, so we could simply change the preprocessor definition to compile as a the desired char type? It would have been good if SimdJSON supported it natively, so we could use it without continuously transcoding all the time
UTF-8 requires 1, 2, 3, 4 bytes per character. UTF-16 requires 2 or 4 bytes per character. So a Unicode character uses 1, 2, 3 or 4 char8_t values OR 1 or 2 char16_t values.
On typical JSON data, UTF-16 would require twice as many bytes.
For all sorts of reasons including better compression and backward compatibility with ASCII, UTF-8 is the de facto standard on the web. XML, HTML, JSON: they are all UTF-8 by default. For JSON, UTF-8 is a requirement (for public use). I would consider UTF-8 to be the native string format of the web.
The JSON input will be UTF-8. That's what the standard prescribes. For internal use, you may, of course, convert your strings to UTF-16 or UTF-32, or something else, but if you have done so, then transcoding (UTF-8 to UTF-16) occurred. It is likely that you have roughly doubled the size of your documents if you went to UTF-16.
Now, transcoding from UTF-8 to UTF-16 is important. That is precisely why we wrote a whole library (simdutf) to do it.
Activity
lemire commentedon Aug 9, 2021
The simdjson library is UTF-8 and frequently points back at the original document which is, by the standard, UTF-8. This will not change. We will not support non-standard JSON inputs that rely on UTF-16 or UTF-32. JSON is UTF-8 as a requirement. If you work with JSON in a performance-sensitive manner, working with UTF-8 directly will always be faster and better. The UTF-8 is the native format of the web.
About types... The
wchar_tpredates C++11 and is system-specific. It is not a type that we would ever support because it is implementation-defined... it is not the same type under Linux and under Windows.A better option are the
char16_tandchar32_ttypes which correspond to UTF-16 and UTF-32 formats respectively.There is also a type corresponding to UTF-8 in C++20 (
char8_t), but we usecharfor that purpose under simdjson, as is often done. In the future, we could just replace it withchar8_tbut we have abstained from doing so until now because not everyone has C++20 support.No matter what, since the original document must be UTF-8, the production of UTF-16 and UTF-32 string requires memory allocation and transcoding. It is not possible to have UTF-16 or UTF-32 support natively: someone must allocate memory and transcode the UTF-8 strings there.
We decided that it was best to provide the transcoding functions as a separate feature. Thus we created the simdutf library. The simdutf library offers fast (SIMD-accelerated) transcoding functions.
For optimal performance, I expect that most people will want to control memory allocation and transcoding. Though it is relatively efficient, it is not free.
It would be doable to support UTF-16 and UTF-32 transcoding from within simdjson , for convenience, but all it would be do is allocate a string and transcode into it. There is no magic. It is not entirely clear what benefit, other than some convenience, that an integration of simdutf into simdjson would bring. If you have ideas or can elaborate, that would be great.
Related issues: #1682 and #1130
xbotuk commentedon Aug 9, 2021
Thank you for your reply.
Yes, I meant UTF-16. I assumed SimdJSON could do this on the fly with little changes.
char16_t is available from C++11 https://en.cppreference.com/w/cpp/keyword/char16_t
Can a preprocessor be used to use something else instead of char, so we could simply change the preprocessor definition to compile as a the desired char type?
It would have been good if SimdJSON supported it natively, so we could use it without continuously transcoding all the time
lemire commentedon Aug 9, 2021
Indeed. You will find that simdutf relies on
char16_textensively and it requires C++11.UTF-8 requires 1, 2, 3, 4 bytes per character. UTF-16 requires 2 or 4 bytes per character. So a Unicode character uses 1, 2, 3 or 4
char8_tvalues OR 1 or 2char16_tvalues.On typical JSON data, UTF-16 would require twice as many bytes.
For all sorts of reasons including better compression and backward compatibility with ASCII, UTF-8 is the de facto standard on the web. XML, HTML, JSON: they are all UTF-8 by default. For JSON, UTF-8 is a requirement (for public use). I would consider UTF-8 to be the native string format of the web.
The JSON input will be UTF-8. That's what the standard prescribes. For internal use, you may, of course, convert your strings to UTF-16 or UTF-32, or something else, but if you have done so, then transcoding (UTF-8 to UTF-16) occurred. It is likely that you have roughly doubled the size of your documents if you went to UTF-16.
Now, transcoding from UTF-8 to UTF-16 is important. That is precisely why we wrote a whole library (simdutf) to do it.
Please give simdutf a try!
GH-50627: [C++] Migrate from_string.cc to simdjson (#50653)