From 12ff246297a01ebf9cd36460d8b12cd3933fe8f3 Mon Sep 17 00:00:00 2001 From: Egor Bogatov Date: Wed, 9 Oct 2019 03:02:45 +0300 Subject: [PATCH] Optimize ToString() for byte, ushort, uint and ulong (dotnet/coreclr#27056) Signed-off-by: dotnet-bot --- .../System.Private.CoreLib/shared/System/Byte.cs | 2 +- .../shared/System/Number.Formatting.cs | 28 ++++++++-------------- .../System.Private.CoreLib/shared/System/UInt16.cs | 2 +- .../System.Private.CoreLib/shared/System/UInt32.cs | 2 +- .../System.Private.CoreLib/shared/System/UInt64.cs | 2 +- 5 files changed, 14 insertions(+), 22 deletions(-) diff --git a/netcore/System.Private.CoreLib/shared/System/Byte.cs b/netcore/System.Private.CoreLib/shared/System/Byte.cs index 36269a36e33..88d349a4bd8 100644 --- a/netcore/System.Private.CoreLib/shared/System/Byte.cs +++ b/netcore/System.Private.CoreLib/shared/System/Byte.cs @@ -166,7 +166,7 @@ namespace System public override string ToString() { - return Number.FormatUInt32(m_value, null, null); + return Number.UInt32ToDecStr(m_value, -1); } public string ToString(string? format) diff --git a/netcore/System.Private.CoreLib/shared/System/Number.Formatting.cs b/netcore/System.Private.CoreLib/shared/System/Number.Formatting.cs index 2429f28de4d..82ac11670dc 100644 --- a/netcore/System.Private.CoreLib/shared/System/Number.Formatting.cs +++ b/netcore/System.Private.CoreLib/shared/System/Number.Formatting.cs @@ -1205,10 +1205,8 @@ namespace System { 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; } @@ -1217,15 +1215,13 @@ namespace System { 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)); @@ -1243,10 +1239,8 @@ namespace System { 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); } @@ -1276,10 +1270,8 @@ namespace System { 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); } @@ -1481,7 +1473,7 @@ namespace System 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; diff --git a/netcore/System.Private.CoreLib/shared/System/UInt16.cs b/netcore/System.Private.CoreLib/shared/System/UInt16.cs index 10e7d3cbc57..c0b2900f8e6 100644 --- a/netcore/System.Private.CoreLib/shared/System/UInt16.cs +++ b/netcore/System.Private.CoreLib/shared/System/UInt16.cs @@ -68,7 +68,7 @@ namespace System // 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) diff --git a/netcore/System.Private.CoreLib/shared/System/UInt32.cs b/netcore/System.Private.CoreLib/shared/System/UInt32.cs index 53fdb84757c..f4ef1c6ecf3 100644 --- a/netcore/System.Private.CoreLib/shared/System/UInt32.cs +++ b/netcore/System.Private.CoreLib/shared/System/UInt32.cs @@ -78,7 +78,7 @@ namespace System // 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) diff --git a/netcore/System.Private.CoreLib/shared/System/UInt64.cs b/netcore/System.Private.CoreLib/shared/System/UInt64.cs index 2999ba87eca..927d807b387 100644 --- a/netcore/System.Private.CoreLib/shared/System/UInt64.cs +++ b/netcore/System.Private.CoreLib/shared/System/UInt64.cs @@ -77,7 +77,7 @@ namespace System public override string ToString() { - return Number.FormatUInt64(m_value, null, null); + return Number.UInt64ToDecStr(m_value, -1); } public string ToString(IFormatProvider? provider) -- 2.11.4.GIT