Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Improve bool.TryFormat perf #18711

Merged
merged 2 commits into from
Jun 29, 2018
Merged
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
30 changes: 23 additions & 7 deletions src/System.Private.CoreLib/shared/System/Boolean.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,34 @@ public string ToString(IFormatProvider provider)

public bool TryFormat(Span<char> destination, out int charsWritten)
{
string s = m_value ? TrueLiteral : FalseLiteral;

if (s.AsSpan().TryCopyTo(destination))
if (m_value)
{
charsWritten = s.Length;
return true;
if ((uint)destination.Length > 3) // uint cast, per https://github.com/dotnet/coreclr/issues/18688
{
destination[0] = 'T';
destination[1] = 'r';
destination[2] = 'u';
destination[3] = 'e';
charsWritten = 4;
return true;
}
}
else
{
charsWritten = 0;
return false;
if ((uint)destination.Length > 4)
{
destination[0] = 'F';
destination[1] = 'a';
destination[2] = 'l';
destination[3] = 's';
destination[4] = 'e';
charsWritten = 5;
return true;
}
}

charsWritten = 0;
return false;
}

// Determines whether two Boolean objects are equal.
Expand Down