Repository navigation
Optimize utf16 decoding - #165027
Open
ywxt wants to merge 1 commit into
Open
Optimize utf16 decoding#165027ywxt wants to merge 1 commit into
ywxt wants to merge 1 commit into
Conversation
jdu2600
added a commit
to originsec/etw-rs
that referenced
this pull request
Nov 10, 2026
The open PR rewrites std's String::from_utf16 and Wtf8Buf::from_wide (behind OsString::from_wide) with block-wise decoding: 16-unit blocks classified as ASCII, surrogate-free BMP or containing surrogates; a 32-unit bulk ASCII copy entered from any all-ASCII block, so it resumes after a non-ASCII run; a branch-free encoder for BMP blocks; lone surrogates handled in the same pass. Inputs under one block, and the tail, take a scalar per-character path. About 200 lines with seven unsafe blocks, against this function's ~35 safe lines. Ported verbatim into a probe (std-internal encode_utf8_raw_unchecked replaced by an equivalent encoder), run against this function and current std on the PR's own 11 input categories at 4-1000 units plus ETW-shaped strings, pinned, two runs. All three agree with OsString::from_wide on every input. ns per call: input std this PR registry path, 36, ASCII 107 32 30 file path, 62, ASCII 167 36 33 image path, 83, ASCII 216 33 33 user folder "Jürgen", 62 206 55 83 CJK path, 40 177 49 81 CJK, 8 110 38 109 CJK + ASCII, 1000 3179 1069 723 ASCII, 1000 2424 117 68 10% lone surrogates, 100 287 314 108 On ASCII paths the two tie. On short non-ASCII input the PR's per-character tail makes it no faster than std below 16 units, and this is 1.4-2.2x faster up to 40. From 100 units of mixed text the PR wins, up to 3.2x at 1000, and it wins on lone surrogates, where this re-decodes from the start. ETW strings sit where this is equal or ahead, so swapping in std's from_wide would cost short non-ASCII paths. The doc comment says to measure on ETW-length strings once the MSRV includes the PR, not to switch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Amanieu
reviewed
Nov 16, 2026
Amanieu
reviewed
Nov 17, 2026
Amanieu
reviewed
Nov 21, 2026
Contributor
Author
|
This does reduce the additional allocations.
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 PR classifies UTF-16 code units and accumulates ASCII and BMP characters into blocks that can be processed with SIMD.
1. Overall
2. Category
By Language/Input Type: x86-64
By Input Length: x86-64
By Language/Input Type: AArch64
By Input Length: AArch64
3. Regression
Additional Reallocation
Compared to main,
reservemay trigger an additional reallocation.All cases below use strict from_utf16. Times are in ns.
Small Sequence
On small slices, there are regressions.
Error at Beginning
There is SIMD loading for long sequences.
r? @Amanieu
Detailed benchmark results