std.unicode: Add ASCII fast path to UTF-16 <-> UTF-8 conversion functions #17797
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This takes the ASCII fast path added for UTF-8 validation in #17329 (plus the changes in #17659) and applies the same concept to both directions of UTF-8 <-> UTF-16 conversion.
I'm new to SIMD stuff so feedback on the implementation is appreciated.
Benchmark results
Benchmark code
ReleaseFast
short.ascii:utf8->utf16.buf
short.ascii:utf8->utf16.alloc
short.ascii:utf16->utf8.buf
short.ascii:utf16->utf8.alloc
long.ascii:utf8->utf16.buf
long.ascii:utf8->utf16.alloc
long.ascii:utf16->utf8.buf
long.ascii:utf16->utf8.alloc
Debug
short.ascii:utf8->utf16.buf
short.ascii:utf8->utf16.alloc
short.ascii:utf16->utf8.buf
short.ascii:utf16->utf8.alloc
long.ascii:utf8->utf16.buf
long.ascii:utf8->utf16.alloc
long.ascii:utf16->utf8.buf
long.ascii:utf16->utf8.alloc
Performance for non-ASCII strings is ~unchanged. More complicated techniques would be needed to apply SIMD to conversion of non-ASCII codepoints:
I was slightly expecting this to make a noticeable difference on Windows for filesystem-heavy use cases, but there was no difference in the test I used in this post (recursively copying a giant nested directory). Also no difference in the runtime of the standard library tests.