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
Accessing folded conversion of string literals to UTF-8 from user code is problematic. #93501
In scope of continued improvement of UTF-8 usability in .NET, thanks to #85328, the upcoming release has the ability to fold conversion of string literals to UTF-8 at callsites where JIT/AOT can recognize it.
Based on the results of reviewing Utf8.TryWriteInterpolatedStringHandler internals and the first third-party implementation at https://github.com/Cysharp/Utf8StringInterpolation, it appears, however, that only Utf8.TryWriteInterpolatedStringHandler itself is able to take advantage of this optimization, with UTF8EncodingSealed.ReadUtf8 remaining otherwise not accessible outside of CoreLib through normal means.
This means that alternate [InterpolatedStringHandler] implementations which enable scenarios like Utf8String.Format($"Today is {DateTime.Now}") are not able to (easily) use this optimization.
Proposal
As suggested by @EgorBo , one of the options could be including ReadUtf8 call into Encoding.UTF8.TryGetBytes which would enable the following patterns to be optimized:
Nesting Utf8.TryWriteInterpolatedStringHandler in another interpolated string handler, which is forced to duplicate stored state and re-create nested handler each time the buffer needs to be resized
Writing an unsafe proxy in IL with suppressed visibility checks that would resolve static Encoding.UTF8.s_default and corresponding UTF8EncodingSealed.ReadUtf8 (I'm not sure if this will work but in theory it may be possible)
Reacted by Egor Bogatov, Md Sadman Chowdhury, Yoshifumi Kawai, Paulus Pärssinen and Raul Santos
Activity
ghost added
needs-area-labelAn area label is needed to ensure this gets routed to the appropriate area owners
@EgorBo Small update: while revisiting interpolated handlers implementation in U8String, in the end, with UnsafeAccessor it was possible to access the folded conversion.
Activity
EgorBo commentedon Oct 14, 2023
Marking as codegen as it needs some improvements there first. Marking as future, but will definitely give it a try in 9.0.
neon-sunset commentedon Apr 24, 2024
@EgorBo Small update: while revisiting interpolated handlers implementation in U8String, in the end, with
UnsafeAccessorit was possible to access the folded conversion.It even unrolls the copies, honestly, performance win is too tempting to just go with this (bad) option 😅
Edit: this was one step away from safely accessing it, at the cost of slight codegen regression - https://github.com/U8String/U8String/blob/main/Sources/U8String/Shared/U8Interpolation.cs#L250-L270
EgorBo commentedon Apr 24, 2024
Haha, nice! Although, just keep in mind that we don't guarantee API stability for internal stuff 🙂