Skip to content

Commit

Permalink
Optimize ToString() for byte, ushort, uint and ulong (dotnet/coreclr#…
Browse files Browse the repository at this point in the history
…27056)




Commit migrated from dotnet/coreclr@020639b
  • Loading branch information
EgorBo authored and jkotas committed Oct 9, 2019
1 parent a8951de commit 9282775
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/libraries/System.Private.CoreLib/src/System/Byte.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ private static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, NumberFor

public override string ToString()
{
return Number.FormatUInt32(m_value, null, null);
return Number.UInt32ToDecStr(m_value, -1);
}

public string ToString(string? format)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1205,10 +1205,8 @@ private static unsafe void UInt32ToNumber(uint value, ref NumberBuffer number)
{
while (--digits >= 0 || value != 0)
{
// TODO https://github.com/dotnet/coreclr/issues/3439
uint newValue = value / 10;
*(--bufferEnd) = (byte)(value - (newValue * 10) + '0');
value = newValue;
value = Math.DivRem(value, 10, out uint remainder);
*(--bufferEnd) = (byte)(remainder + '0');
}
return bufferEnd;
}
Expand All @@ -1217,15 +1215,13 @@ private static unsafe void UInt32ToNumber(uint value, ref NumberBuffer number)
{
while (--digits >= 0 || value != 0)
{
// TODO https://github.com/dotnet/coreclr/issues/3439
uint newValue = value / 10;
*(--bufferEnd) = (char)(value - (newValue * 10) + '0');
value = newValue;
value = Math.DivRem(value, 10, out uint remainder);
*(--bufferEnd) = (char)(remainder + '0');
}
return bufferEnd;
}

private static unsafe string UInt32ToDecStr(uint value, int digits)
internal static unsafe string UInt32ToDecStr(uint value, int digits)
{
int bufferLength = Math.Max(digits, FormattingHelpers.CountDigits(value));

Expand All @@ -1243,10 +1239,8 @@ private static unsafe string UInt32ToDecStr(uint value, int digits)
{
do
{
// TODO https://github.com/dotnet/coreclr/issues/3439
uint div = value / 10;
*(--p) = (char)('0' + value - (div * 10));
value = div;
value = Math.DivRem(value, 10, out uint remainder);
*(--p) = (char)(remainder + '0');
}
while (value != 0);
}
Expand Down Expand Up @@ -1276,10 +1270,8 @@ private static unsafe bool TryUInt32ToDecStr(uint value, int digits, Span<char>
{
do
{
// TODO https://github.com/dotnet/coreclr/issues/3439
uint div = value / 10;
*(--p) = (char)('0' + value - (div * 10));
value = div;
value = Math.DivRem(value, 10, out uint remainder);
*(--p) = (char)(remainder + '0');
}
while (value != 0);
}
Expand Down Expand Up @@ -1481,7 +1473,7 @@ private static unsafe void UInt64ToNumber(ulong value, ref NumberBuffer number)
number.CheckConsistency();
}

private static unsafe string UInt64ToDecStr(ulong value, int digits)
internal static unsafe string UInt64ToDecStr(ulong value, int digits)
{
if (digits < 1)
digits = 1;
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/System.Private.CoreLib/src/System/UInt16.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public override int GetHashCode()
// Converts the current value to a String in base-10 with no extra padding.
public override string ToString()
{
return Number.FormatUInt32(m_value, null, null);
return Number.UInt32ToDecStr(m_value, -1);
}

public string ToString(IFormatProvider? provider)
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/System.Private.CoreLib/src/System/UInt32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public override int GetHashCode()
// The base 10 representation of the number with no extra padding.
public override string ToString()
{
return Number.FormatUInt32(m_value, null, null);
return Number.UInt32ToDecStr(m_value, -1);
}

public string ToString(IFormatProvider? provider)
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/System.Private.CoreLib/src/System/UInt64.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public override int GetHashCode()

public override string ToString()
{
return Number.FormatUInt64(m_value, null, null);
return Number.UInt64ToDecStr(m_value, -1);
}

public string ToString(IFormatProvider? provider)
Expand Down

0 comments on commit 9282775

Please sign in to comment.