Skip to content

Commit baf50b3

Browse files
stephentoubdotnet-bot
authored andcommitted
Improve bool.TryFormat perf (dotnet/coreclr#18711)
* Improve bool.TryFormat perf Improves throughput by ~50%, by avoiding AsSpan().CopyTo and just writing out the characters one-by-one. I experimented with playing the same tricks around storing the data in a ulong and blitting it out to the destination reinterpreted as a span of ulongs, but it didn't make a measurable improvement and increased the code complexity. * Update Boolean.cs Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
1 parent b17387f commit baf50b3

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

src/Common/src/CoreLib/System/Boolean.cs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,34 @@ public string ToString(IFormatProvider provider)
9797

9898
public bool TryFormat(Span<char> destination, out int charsWritten)
9999
{
100-
string s = m_value ? TrueLiteral : FalseLiteral;
101-
102-
if (s.AsSpan().TryCopyTo(destination))
100+
if (m_value)
103101
{
104-
charsWritten = s.Length;
105-
return true;
102+
if ((uint)destination.Length > 3) // uint cast, per https://github.com/dotnet/coreclr/issues/18688
103+
{
104+
destination[0] = 'T';
105+
destination[1] = 'r';
106+
destination[2] = 'u';
107+
destination[3] = 'e';
108+
charsWritten = 4;
109+
return true;
110+
}
106111
}
107112
else
108113
{
109-
charsWritten = 0;
110-
return false;
114+
if ((uint)destination.Length > 4)
115+
{
116+
destination[0] = 'F';
117+
destination[1] = 'a';
118+
destination[2] = 'l';
119+
destination[3] = 's';
120+
destination[4] = 'e';
121+
charsWritten = 5;
122+
return true;
123+
}
111124
}
125+
126+
charsWritten = 0;
127+
return false;
112128
}
113129

114130
// Determines whether two Boolean objects are equal.

0 commit comments

Comments
 (0)