summaryrefslogtreecommitdiff
path: root/src/System.Private.CoreLib/shared/System/Buffers
diff options
context:
space:
mode:
authorStephen Toub <stoub@microsoft.com>2018-07-20 03:56:57 -0700
committerGitHub <noreply@github.com>2018-07-20 03:56:57 -0700
commit6860d110a843e882357d82d2a72205343339b11b (patch)
tree21a19adec954c9d46fd90d19e7fa30049d65b53e /src/System.Private.CoreLib/shared/System/Buffers
parent799b2a30a685a739d59a22b26b50e5da8616446f (diff)
downloadcoreclr-6860d110a843e882357d82d2a72205343339b11b.tar.gz
coreclr-6860d110a843e882357d82d2a72205343339b11b.tar.bz2
coreclr-6860d110a843e882357d82d2a72205343339b11b.zip
Improve throughput of TimeSpan.ToString/TryFormat with "g"/"G" (#19051)
* Improve throughput of TimeSpan.ToString/TryFormat with "g"/"G" TimeSpan has three standard formats: "c", "g", and "G". Yesterday I updated its implementation with throughput improvements for "c" (the default) based on porting the design from Utf8Formatter; this PR does so for "g"/"G". Initially I wasn't going to handle "g"/"G" as they factor in culture (Utf8Formatter doesn't), but even with accessing the current culture there are still significant wins to be had. I was also going to keep the "c" and "g"/"G" implementations separate, to avoid bogging down the default "c" formatting with additional conditions needed to support "g"/"G", but the overhead incurred for that turns out to be minimal enough that it's worth keeping one implementation rather than two mostly-similar ones... the impact on "c" is mostly within noise. This PR makes a significant throughput improvement for "g"/"G" formatting. It also removes several unnecessary allocations, such that TryFormat with "g"/"G" is now allocation-free (and ToString just allocates the asked-for string). * Address PR feedback
Diffstat (limited to 'src/System.Private.CoreLib/shared/System/Buffers')
-rw-r--r--src/System.Private.CoreLib/shared/System/Buffers/Text/FormattingHelpers.CountDigits.cs29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/System.Private.CoreLib/shared/System/Buffers/Text/FormattingHelpers.CountDigits.cs b/src/System.Private.CoreLib/shared/System/Buffers/Text/FormattingHelpers.CountDigits.cs
index 709ac4fba6..b6140adbad 100644
--- a/src/System.Private.CoreLib/shared/System/Buffers/Text/FormattingHelpers.CountDigits.cs
+++ b/src/System.Private.CoreLib/shared/System/Buffers/Text/FormattingHelpers.CountDigits.cs
@@ -128,5 +128,34 @@ namespace System.Buffers.Text
return digits;
}
+
+
+ // Counts the number of trailing '0' digits in a decimal number.
+ // e.g., value = 0 => retVal = 0, valueWithoutTrailingZeros = 0
+ // value = 1234 => retVal = 0, valueWithoutTrailingZeros = 1234
+ // value = 320900 => retVal = 2, valueWithoutTrailingZeros = 3209
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static int CountDecimalTrailingZeros(uint value, out uint valueWithoutTrailingZeros)
+ {
+ int zeroCount = 0;
+
+ if (value != 0)
+ {
+ while (true)
+ {
+ uint temp = value / 10;
+ if (value != (temp * 10))
+ {
+ break;
+ }
+
+ value = temp;
+ zeroCount++;
+ }
+ }
+
+ valueWithoutTrailingZeros = value;
+ return zeroCount;
+ }
}
}