Optimize ToString() for byte, ushort, uint and ulong (dotnet/coreclr#27056)
authorEgor Bogatov <egorbo@gmail.com>
Wed, 9 Oct 2019 00:02:45 +0000 (03:02 +0300)
committerJan Kotas <jkotas@microsoft.com>
Wed, 9 Oct 2019 00:02:45 +0000 (17:02 -0700)
Commit migrated from https://github.com/dotnet/coreclr/commit/020639bdb3580ddf6ddc7e5723310d9175fba44c

src/libraries/System.Private.CoreLib/src/System/Byte.cs
src/libraries/System.Private.CoreLib/src/System/Number.Formatting.cs
src/libraries/System.Private.CoreLib/src/System/UInt16.cs
src/libraries/System.Private.CoreLib/src/System/UInt32.cs
src/libraries/System.Private.CoreLib/src/System/UInt64.cs

index 36269a3..88d349a 100644 (file)
@@ -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)
index 2429f28..82ac116 100644 (file)
@@ -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;
index 10e7d3c..c0b2900 100644 (file)
@@ -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)
index 53fdb84..f4ef1c6 100644 (file)
@@ -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)
index 2999ba8..927d807 100644 (file)
@@ -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)