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
Intrinsify Encoding.UTF8.GetByteCount for constant UTF-16 input #102246
I have been recently staring at a lot of UTF-8 <-> UTF-16 manipulation code (in Garnet and ILCompiler name mangling 😆) and thought that if UTF8EncodingSealed.ReadUTF8 is getting VN expansion when JIT can see the input is CNS, maybe it could be done for Encoding.UTF8.GetByteCount too, which is most commonly used to calculate the buffer for the following (Try)GetBytes call (or if the input fits to the existing buffer).
We of course have GetMaxByteCount for the fast calculation of upper-bound, but doing the str.Length * 3 makes the buffer lengths go over any stackalloc thresholds very quickly.
This is very much inspired after and in theme with #85328 (and then painfully hitting #93501 😢 )
Reacted by neon-sunset, Adeel Mujahid, Raul Santos and Nikolay Zdravkov
The ReadUTF8 expansion seems to be able to handle any object handle that is IsKnownImmutable e.g. frozen objects such as static readonly string fields. Also in my original plan of simplifying UTF-8 string builder with small custom interpolated string handler it is a problem because the string literals aren't passed as utf-8 RVA spans but as normal strings (we don't have dotnet/csharplang#7072 / dotnet/csharplang#9120 / dotnet/csharplang#9121 yet).
Activity
dotnet-policy-service commentedon May 15, 2024
Tagging subscribers to this area: @dotnet/area-system-text-encoding
See info in area-owners.md if you want to be subscribed.
PaulusParssinen commentedon May 15, 2024
Correct area probably CodeGen-coreclr 😆
huoyaoyuan commentedon May 15, 2024
For constant UTF-8 strings, is
u8string literal an option for you? If not, what's preventing it?PaulusParssinen commentedon May 15, 2024
u8is always the better choice if available.The
ReadUTF8expansion seems to be able to handle any object handle that isIsKnownImmutablee.g. frozen objects such asstatic readonly stringfields. Also in my original plan of simplifying UTF-8 string builder with small custom interpolated string handler it is a problem because the string literals aren't passed as utf-8 RVA spans but as normal strings (we don't have dotnet/csharplang#7072 / dotnet/csharplang#9120 / dotnet/csharplang#9121 yet).runtime/src/coreclr/jit/helperexpansion.cpp
Lines 1616 to 1617 in 426edd0
dotnet-policy-service commentedon May 15, 2024
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.
EgorBo commentedon May 15, 2024
I think it should be fairly trivial to intrinsify
GetByteCountindeedPaulusParssinen commentedon Apr 7, 2026
I'm fairly sure this is something even Copilot could handle with the existing expansions acting as examples (ofc deduplicating the logic if added)
JIT: Intrinsify Encoding.UTF8.GetByteCount for constant input
JIT: Intrinsify Encoding.UTF8.GetByteCount for constant input
JIT: Simplify ReadUtf8 immutable data extraction (#135629)