-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
API proposal: Obsolete RuntimeHelpers.OffsetToStringData #31406
Comments
The C++/CLI headers are also using this ( |
Have we validated that VB and F# don't use this API either? What about C++/CLI? |
F#: Opened dotnet/fsharp#9251 to track the compiler work. VB: I cannot figure out how to pin a string in VB. Can somebody confirm that the language even has compiler support? C++/CLI: I'll open a work item if somebody tells me where to do so. Can somebody confirm that C++/CLI is supported on .NET 5.0+? FWIW I wouldn't hold up the obsoletion work while waiting for these. At this point we're talking about the IL that the compilers spew, which I can't fathom would be broken by non-runtime-observable obsoletion of API surface. |
WPF is using it actively |
It's supported for Windows and (as @weltkante said) is used by WPF. |
|
Work in F# is here: dotnet/fsharp#9252 |
The earlier VB comment should've read "Can somebody confirm that VB even has support for pinning strings?" My brain is taking a long weekend. |
VB can't use unsafe code right? |
namespace System.Runtime.CompilerServices
{
public sealed class RuntimeHelpers
{
[Obsolete("Use string.GetPinnableReference() instead.")]
public static int OffsetToStringData { get; }
}
} |
API proposal
Justification
The
RuntimeHelpers.OffsetToStringData
property is the last remaining public API which exposes the concept of thestring
object containing inline UTF-16 data just after the object header. It's used by compilers to pinstring
instances, generally (but not always) using the following pseudocode:Because of this pattern throughout existing libraries, adding support for things like string compaction (where the backing data might be ASCII instead of UTF-16) becomes difficult and error-prone.
In .NET Core 3.0, we added an API
string.GetPinnableReference()
which takes advantage of the new pattern-basedfixed
statement support added in C# 7.3. This new API would allow us to intercept the call immediately before the pin operation, allowing the runtime to fix up the data as necessary. When targeting netcoreapp3.0, the compiler will prefer the newGetPinnableReference
API over the oldRuntimeHelpers
API.As more and more DLLs are built which target netcoreapp3.0+, we should gradually see the ecosystem move over to the new API, allowing us to experiment with changing the internal layout of the
string
type once a critical mass of DLLs has migrated away from the old APIs.Since the main consumer of the old API is the C# compiler itself, I don't anticipate most code bases seeing new warnings after upgrade. The compiler will automatically prefer the new API anyway. The reason I propose obsoleting the old API is so tooling authors and devs writing ref emit code or otherwise manipulating IL directly will see the warning and know that they should instead be calling a new more future-proof API.
The text was updated successfully, but these errors were encountered: