Skip to content

Intrinsify Encoding.UTF8.GetByteCount for constant UTF-16 input #102246

Open

Description

@PaulusParssinen

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 😢 )

Activity

  1. added
    untriaged
    on May 15, 2024
  2. dotnet-policy-service commented on May 15, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-text-encoding
    See info in area-owners.md if you want to be subscribed.

  3. PaulusParssinen commented on May 15, 2024

    @PaulusParssinen
    ContributorAuthor

    Correct area probably CodeGen-coreclr 😆

  4. huoyaoyuan commented on May 15, 2024

    @huoyaoyuan
    Member

    For constant UTF-8 strings, is u8 string literal an option for you? If not, what's preventing it?

  5. added
    area-CodeGen-coreclr
    and removed
    untriaged
    on May 15, 2024
  6. self-assigned this
    on May 15, 2024
  7. added this to the Future milestone on May 15, 2024
  8. PaulusParssinen commented on May 15, 2024

    @PaulusParssinen
    ContributorAuthor

    u8 is always the better choice if available.

    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).

    // We mostly expect string literal objects here, but let's be more agile just in case
    if (!info.compCompHnd->isObjectImmutable(strObj))

  9. dotnet-policy-service commented on May 15, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
    See info in area-owners.md if you want to be subscribed.

  10. EgorBo commented on May 15, 2024

    @EgorBo
    Member

    I think it should be fairly trivial to intrinsify GetByteCount indeed

  11. PaulusParssinen commented on Apr 7, 2026

    @PaulusParssinen
    ContributorAuthor

    I'm fairly sure this is something even Copilot could handle with the existing expansions acting as examples (ofc deduplicating the logic if added)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclr

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions