Skip to content

API proposal: Obsolete RuntimeHelpers.OffsetToStringData #31406

Closed

Description

@GrabYourPitchforks

API proposal

namespace System.Runtime.CompilerServices
{
    public sealed class RuntimeHelpers
    {
        [Obsolete("Use string.GetPinnableReference() instead.")]
        public static int OffsetToStringData { get; }
    }
}

Justification

The RuntimeHelpers.OffsetToStringData property is the last remaining public API which exposes the concept of the string object containing inline UTF-16 data just after the object header. It's used by compilers to pin string instances, generally (but not always) using the following pseudocode:

fixed string fixedLocal = theStringInstance;
char* pchStr = (char*)((byte*)theStringInstance + RuntimeHelpers.OffsetToStringData);
// use 'pchStr' here

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-based fixed 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 new GetPinnableReference API over the old RuntimeHelpers 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.

Activity

  1. transferred this issue fromdotnet/corefxon Feb 1, 2020
  2. added this to the 5.0 milestone on Feb 1, 2020
  3. added
    untriaged
    on Feb 23, 2020
  4. removed
    untriaged
    on Mar 3, 2020
  5. weltkante commented on Mar 9, 2020

    @weltkante
    Contributor

    The C++/CLI headers are also using this (vcclr.h uses OffsetToStringData in PtrToStringChars which is used throughout various other headers to implement various string conversion helpers).

  6. added
    blocking
    on May 6, 2020
  7. terrajobst commented on May 20, 2020

    @terrajobst
    Contributor

    When targeting netcoreapp3.0, the compiler will prefer the new GetPinnableReference API over the old RuntimeHelpers API.

    Have we validated that VB and F# don't use this API either? What about C++/CLI?

  8. GrabYourPitchforks commented on May 20, 2020

    @GrabYourPitchforks
    MemberAuthor

    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.

  9. weltkante commented on May 20, 2020

    @weltkante
    Contributor

    Can somebody confirm that C++/CLI is supported on .NET 5.0+?

    WPF is using it actively

  10. terrajobst commented on May 21, 2020

    @terrajobst
    Contributor

    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+?

    It's supported for Windows and (as @weltkante said) is used by WPF.

  11. terrajobst commented on May 21, 2020

    @terrajobst
    Contributor

    VB: I cannot figure out how to pin a string in VB. Can somebody confirm that the language even has compiler support?

    @jaredpar

  12. cartermp commented on May 21, 2020

    @cartermp

    Work in F# is here: dotnet/fsharp#9252

  13. GrabYourPitchforks commented on May 22, 2020

    @GrabYourPitchforks
    MemberAuthor

    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.

  14. danmoseley commented on May 22, 2020

    @danmoseley
    Contributor

    VB can't use unsafe code right?

  15. added
    api-approved
    and removed
    blocking
    on May 26, 2020
  16. terrajobst commented on May 26, 2020

    @terrajobst
    Contributor
    • Looks good.
    namespace System.Runtime.CompilerServices
    {
        public sealed class RuntimeHelpers
        {
            [Obsolete("Use string.GetPinnableReference() instead.")]
            public static int OffsetToStringData { get; }
        }
    }
  17. ghost closed this as completedin #35722on Jun 9, 2020
  18. ghost locked as resolved and limited conversation to collaborators on Dec 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedarea-System.Runtime

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions