Skip to content

Commit

Permalink
Remove a few char[] allocations from Xml (#82006)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Feb 12, 2023
1 parent 0a51a37 commit b53c06b
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public override string ToString()
// Make local copy of data to avoid modifying input.
uint[] rgulNumeric = new uint[4] { m_data1, m_data2, m_data3, m_data4 };
int culLen = m_bLen;
char[] pszTmp = new char[s_NUMERIC_MAX_PRECISION + 1]; //Local Character buffer to hold
Span<char> pszTmp = stackalloc char[s_NUMERIC_MAX_PRECISION + 1]; //Local Character buffer to hold
//the decimal digits, from the
//lowest significant to highest significant

Expand Down Expand Up @@ -222,7 +222,7 @@ public override string ToString()
if (m_bScale > 0)
uiResultLen++;

char[] szResult = new char[uiResultLen];
Span<char> szResult = stackalloc char[uiResultLen];
int iCurChar = 0;

if (!fPositive)
Expand Down Expand Up @@ -385,15 +385,15 @@ private static void WriteTimeFullPrecision(StringBuilder sb, int hr, int min, in
fractionDigits--;
fraction /= 10;
}
char[] charArray = new char[fractionDigits];
Span<char> chars = stackalloc char[fractionDigits];
while (fractionDigits > 0)
{
fractionDigits--;
charArray[fractionDigits] = (char)(fraction % 10 + '0');
chars[fractionDigits] = (char)(fraction % 10 + '0');
fraction /= 10;
}
sb.Append('.');
sb.Append(charArray);
sb.Append(chars);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Text;
Expand Down Expand Up @@ -894,7 +895,7 @@ internal override string UniqueId
get
{
// 32-bit integer is split into 5-bit groups, the maximum number of groups is 7
char[] buf = new char[1 + 7 + 1 + 7];
Span<char> buf = stackalloc char[1 + 7 + 1 + 7];
int idx = 0;
int loc;

Expand All @@ -921,7 +922,7 @@ internal override string UniqueId
loc >>= 5;
} while (loc != 0);

return new string(buf, 0, idx);
return buf.Slice(0, idx).ToString();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,6 @@ private static string ConvertToDecimal(double val, int minLen, char zero, string
}

// Add both grouping separators and zero padding to the string representation of a number
#if true
unsafe
{
char* result = stackalloc char[newLen];
Expand Down Expand Up @@ -364,30 +363,6 @@ private static string ConvertToDecimal(double val, int minLen, char zero, string
}
return new string(result, 0, newLen);
}
#else
// Safe version is about 20% slower after NGEN
char[] result = new char[newLen];
char separator = (groupSeparator.Length > 0) ? groupSeparator[0] : ' ';

int oldEnd = oldLen - 1;
int newEnd = newLen - 1;
int cnt = groupSize;

while (true) {
// Move digit to its new location (zero if we've run out of digits)
result[newEnd--] = (oldEnd >= 0) ? (char)(str[oldEnd--] + shift) : zero;
if (newEnd < 0) {
break;
}
if (/*groupSize > 0 && */--cnt == 0) {
// Every groupSize digits insert the separator
result[newEnd--] = separator;
cnt = groupSize;
Debug.Assert(newEnd >= 0, "Separator cannot be the first character");
}
}
return new string(result, 0, newLen);
#endif
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ private void LoadDecimalFormat(NsDecl? stylesheetNsList)
}

char[] DefaultValues = DecimalFormatDecl.Default.Characters;
char[] characters = new char[NumCharAttrs];
Span<char> characters = stackalloc char[NumCharAttrs];
Debug.Assert(NumCharAttrs == DefaultValues.Length);

for (int idx = 0; idx < NumCharAttrs; idx++)
Expand Down

0 comments on commit b53c06b

Please sign in to comment.