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

Remove now unnecessary IsKnownConstant special-casing in interpolated string handlers #83762

Merged
merged 1 commit into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -4040,65 +4040,6 @@ public TryWriteInterpolatedStringHandler(int literalLength, int formattedCount,
/// <returns>true if the value could be formatted to the span; otherwise, false.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool AppendLiteral(string value)
{
if (RuntimeHelpers.IsKnownConstant(value))
{
// See comment on inlining and special-casing in DefaultInterpolatedStringHandler.AppendLiteral.

if (value.Length == 1)
{
Span<char> destination = _destination;
int pos = _pos;
if ((uint)pos < (uint)destination.Length)
{
destination[pos] = value[0];
_pos = pos + 1;
return true;
}

return Fail();
}

if (value.Length == 2)
{
Span<char> destination = _destination;
int pos = _pos;
if ((uint)pos < destination.Length - 1)
{
Unsafe.WriteUnaligned(
ref Unsafe.As<char, byte>(ref Unsafe.Add(ref MemoryMarshal.GetReference(destination), pos)),
Unsafe.ReadUnaligned<int>(ref Unsafe.As<char, byte>(ref value.GetRawStringData())));
_pos = pos + 2;
return true;
}

return Fail();
}

if (value.Length == 4)
{
Span<char> destination = _destination;
int pos = _pos;
if ((uint)pos < destination.Length - 3)
{
Unsafe.WriteUnaligned(
ref Unsafe.As<char, byte>(ref Unsafe.Add(ref MemoryMarshal.GetReference(destination), pos)),
Unsafe.ReadUnaligned<long>(ref Unsafe.As<char, byte>(ref value.GetRawStringData())));
_pos = pos + 4;
return true;
}

return Fail();
}
}

return AppendStringDirect(value);
}

/// <summary>Writes the specified string to the handler.</summary>
/// <param name="value">The string to write.</param>
/// <returns>true if the value could be appended to the span; otherwise, false.</returns>
private bool AppendStringDirect(string value)
{
if (value.TryCopyTo(_destination.Slice(_pos)))
{
Expand Down Expand Up @@ -4170,7 +4111,7 @@ public bool AppendFormatted<T>(T value)
s = value?.ToString();
}

return s is null || AppendStringDirect(s);
return s is null || AppendLiteral(s);
}

/// <summary>Writes the specified value to the handler.</summary>
Expand Down Expand Up @@ -4226,7 +4167,7 @@ public bool AppendFormatted<T>(T value, string? format)
s = value?.ToString();
}

return s is null || AppendStringDirect(s);
return s is null || AppendLiteral(s);
}

/// <summary>Writes the specified value to the handler.</summary>
Expand Down Expand Up @@ -4390,7 +4331,7 @@ private bool AppendCustomFormatter<T>(T value, string? format)

if (formatter is not null && formatter.Format(format, value, _provider) is string customFormatted)
{
return AppendStringDirect(customFormatted);
return AppendLiteral(customFormatted);
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,75 +133,6 @@ internal void Clear()
/// <param name="value">The string to write.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendLiteral(string value)
{
if (RuntimeHelpers.IsKnownConstant(value))
{
// AppendLiteral is expected to be called by compiler-generated code with a literal string.
// By inlining it, the method body is exposed to the constant length of that literal, allowing the JIT to
// prune away the irrelevant cases. This effectively enables multiple implementations of AppendLiteral,
// special-cased on and optimized for the literal's length. We special-case lengths 1 and 2 because
// they're very common, e.g.
// 1: ' ', '.', '-', '\t', etc.
// 2: ", ", "0x", "=>", ": ", etc.
// and length 4 because it can similarly be done with a single read/write.

if (value.Length == 1)
{
int pos = _pos;
Span<char> chars = _chars;
if ((uint)pos < (uint)chars.Length)
{
chars[pos] = value[0];
_pos = pos + 1;
}
else
{
GrowThenCopyString(value);
}
return;
}

if (value.Length == 2)
{
int pos = _pos;
if ((uint)pos < _chars.Length - 1)
{
Unsafe.WriteUnaligned(
ref Unsafe.As<char, byte>(ref Unsafe.Add(ref MemoryMarshal.GetReference(_chars), pos)),
Unsafe.ReadUnaligned<int>(ref Unsafe.As<char, byte>(ref value.GetRawStringData())));
_pos = pos + 2;
}
else
{
GrowThenCopyString(value);
}
return;
}

if (value.Length == 4)
{
int pos = _pos;
if ((uint)pos < _chars.Length - 3)
{
Unsafe.WriteUnaligned(
ref Unsafe.As<char, byte>(ref Unsafe.Add(ref MemoryMarshal.GetReference(_chars), pos)),
Unsafe.ReadUnaligned<long>(ref Unsafe.As<char, byte>(ref value.GetRawStringData())));
_pos = pos + 4;
}
else
{
GrowThenCopyString(value);
}
return;
}
}

AppendStringDirect(value);
}

/// <summary>Writes the specified string to the handler.</summary>
/// <param name="value">The string to write.</param>
private void AppendStringDirect(string value)
{
if (value.TryCopyTo(_chars.Slice(_pos)))
{
Expand Down Expand Up @@ -350,7 +281,7 @@ public void AppendFormatted<T>(T value)

if (s is not null)
{
AppendStringDirect(s);
AppendLiteral(s);
}
}

Expand Down Expand Up @@ -412,7 +343,7 @@ public void AppendFormatted<T>(T value, string? format)

if (s is not null)
{
AppendStringDirect(s);
AppendLiteral(s);
}
}

Expand Down Expand Up @@ -596,7 +527,7 @@ private void AppendCustomFormatter<T>(T value, string? format)

if (formatter is not null && formatter.Format(format, value, _provider) is string customFormatted)
{
AppendStringDirect(customFormatted);
AppendLiteral(customFormatted);
}
}

Expand Down Expand Up @@ -646,7 +577,7 @@ private void EnsureCapacityForAdditionalChars(int additionalChars)
}
}

/// <summary>Fallback for fast path in <see cref="AppendStringDirect"/> when there's not enough space in the destination.</summary>
/// <summary>Fallback for fast path in <see cref="AppendLiteral(string)"/> when there's not enough space in the destination.</summary>
/// <param name="value">The string to write.</param>
[MethodImpl(MethodImplOptions.NoInlining)]
private void GrowThenCopyString(string value)
Expand Down