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

Use JIT intrinsic for UTF8 encoding in Utf8.TryWrite's AppendLiteral #89376

Merged
merged 2 commits into from Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -155,6 +155,7 @@ public override bool TryGetBytes(ReadOnlySpan<char> chars, Span<byte> bytes, out
return base.TryGetBytes(chars, bytes, out bytesWritten);
}

/// <summary>Same as Encoding.UTF8.TryGetBytes, except with refs, returning the number of bytes written (or -1 if the operation fails), and optimized for a constant input.</summary>
[MethodImpl(MethodImplOptions.NoInlining)]
[Intrinsic] // Can be unrolled by JIT
internal static unsafe int ReadUtf8(ref char input, int inputLength, ref byte output, int outputLength)
Expand Down
Expand Up @@ -410,15 +410,32 @@ public TryWriteInterpolatedStringHandler(int literalLength, int formattedCount,
/// <summary>Writes the specified string to the handler.</summary>
/// <param name="value">The string to write.</param>
/// <returns>true if the value could be formatted to the span; otherwise, false.</returns>
public bool AppendLiteral(string value) => AppendFormatted(value.AsSpan());

// TODO https://github.com/dotnet/csharplang/issues/7072:
// Add this if/when C# supports u8 literals with string interpolation.
// If that happens prior to this type being released, the above AppendLiteral(string)
// should also be removed. If that doesn't happen, we should look into ways to optimize
// the above AppendLiteral, such as by making the underlying encoding operation a JIT
// intrinsic that can emit substitute a "abc"u8 equivalent for an "abc" string literal.
//public bool AppendLiteral(scoped ReadOnlySpan<byte> value) => AppendFormatted(value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] // we want 'value' exposed to the JIT as a constant
public bool AppendLiteral(string value)
{
// The 99.999% for AppendLiteral is to be called with a const string.
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
// ReadUtf8 is a JIT intrinsic that can do the UTF8 encoding at JIT time.
if (RuntimeHelpers.IsKnownConstant(value) && value is not null)
{
Span<byte> dest = _destination.Slice(_pos);
int bytesWritten = UTF8Encoding.UTF8EncodingSealed.ReadUtf8(
ref value.GetRawStringData(), value.Length,
ref MemoryMarshal.GetReference(dest), dest.Length);

if (bytesWritten >= 0)
{
_pos += bytesWritten;
return true;
}

return Fail();
}

// Fall back to formatting as a span (we don't want a literal string to be
// passed off to a custom formatter if there is one, and AppendFormatted for
// a span doesn't whereas it does for a string).
return AppendFormatted(value.AsSpan());
}

/// <summary>Writes the specified value to the handler.</summary>
/// <param name="value">The value to write.</param>
Expand Down