private const int CharStackBufferSize = 32;
private const string PosNumberFormat = "#";
+ private static readonly string[] s_singleDigitStringCache = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
+
private static readonly string[] s_posCurrencyFormats =
{
"$#", "#$", "$ #", "# $"
private static unsafe string UInt32ToDecStr(uint value, int digits)
{
int bufferLength = Math.Max(digits, FormattingHelpers.CountDigits(value));
+
+ // For single-digit values that are very common, especially 0 and 1, just return cached strings.
+ if (bufferLength == 1)
+ {
+ return s_singleDigitStringCache[value];
+ }
+
string result = string.FastAllocateString(bufferLength);
fixed (char* buffer = result)
{
digits = 1;
int bufferLength = Math.Max(digits, FormattingHelpers.CountDigits(value));
+
+ // For single-digit values that are very common, especially 0 and 1, just return cached strings.
+ if (bufferLength == 1)
+ {
+ return s_singleDigitStringCache[value];
+ }
+
string result = string.FastAllocateString(bufferLength);
fixed (char* buffer = result)
{