Skip to content
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

JIT: Intrinsify UTF16->UTF8 conversion for string literals (Encoding.UTF8.TryGetBytes) #85328

Merged
merged 15 commits into from
Jul 23, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Update UTF8Encoding.Sealed.cs
EgorBo authored Jul 23, 2023
commit 19d2a1ac150c737ede1c1c9f3ff78d155bf4c598
Original file line number Diff line number Diff line change
@@ -150,25 +150,13 @@ private unsafe string GetStringForSmallInput(byte[] bytes)
}

/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool TryGetBytes(ReadOnlySpan<char> chars, Span<byte> bytes, out int bytesWritten)
{
// returns -1 if the input couldn't be encoded or the output buffer was too small
int written = ReadUtf8(
ref MemoryMarshal.GetReference(chars), chars.Length,
ref MemoryMarshal.GetReference(bytes), bytes.Length);

if (written >= 0)
{
bytesWritten = written;
return true;
}
bytesWritten = 0;
return false;
return base.TryGetBytes(chars, bytes, out bytesWritten);
}

[MethodImpl(MethodImplOptions.NoInlining)]
[Intrinsic] // Can be unrolled by JIT if input points to a constant string (with constant inputLength).
[Intrinsic] // Can be unrolled by JIT
internal static unsafe int ReadUtf8(ref char input, int inputLength, ref byte output, int outputLength)
{
fixed (char* pInput = &input)