From b53c06bee442fc2613cd66475ece38c54f02214f Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Sun, 12 Feb 2023 16:12:37 -0500 Subject: [PATCH] Remove a few char[] allocations from Xml (#82006) --- .../src/System/Xml/BinaryXml/SqlUtils.cs | 10 ++++---- .../Xml/Cache/XPathDocumentNavigator.cs | 5 ++-- .../src/System/Xml/Xsl/Runtime/XslNumber.cs | 25 ------------------- .../src/System/Xml/Xsl/Xslt/XsltLoader.cs | 2 +- 4 files changed, 9 insertions(+), 33 deletions(-) diff --git a/src/libraries/System.Private.Xml/src/System/Xml/BinaryXml/SqlUtils.cs b/src/libraries/System.Private.Xml/src/System/Xml/BinaryXml/SqlUtils.cs index d73d3f0874368..baa7791860622 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/BinaryXml/SqlUtils.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/BinaryXml/SqlUtils.cs @@ -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 pszTmp = stackalloc char[s_NUMERIC_MAX_PRECISION + 1]; //Local Character buffer to hold //the decimal digits, from the //lowest significant to highest significant @@ -222,7 +222,7 @@ public override string ToString() if (m_bScale > 0) uiResultLen++; - char[] szResult = new char[uiResultLen]; + Span szResult = stackalloc char[uiResultLen]; int iCurChar = 0; if (!fPositive) @@ -385,15 +385,15 @@ private static void WriteTimeFullPrecision(StringBuilder sb, int hr, int min, in fractionDigits--; fraction /= 10; } - char[] charArray = new char[fractionDigits]; + Span 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); } } diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Cache/XPathDocumentNavigator.cs b/src/libraries/System.Private.Xml/src/System/Xml/Cache/XPathDocumentNavigator.cs index 122bd285efca3..44a90fc9acb25 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Cache/XPathDocumentNavigator.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Cache/XPathDocumentNavigator.cs @@ -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; @@ -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 buf = stackalloc char[1 + 7 + 1 + 7]; int idx = 0; int loc; @@ -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(); } } diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Xsl/Runtime/XslNumber.cs b/src/libraries/System.Private.Xml/src/System/Xml/Xsl/Runtime/XslNumber.cs index 120c9469af68e..cc897eaa57493 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Xsl/Runtime/XslNumber.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Xsl/Runtime/XslNumber.cs @@ -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]; @@ -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 } } } diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Xsl/Xslt/XsltLoader.cs b/src/libraries/System.Private.Xml/src/System/Xml/Xsl/Xslt/XsltLoader.cs index 25fbf84a3fb9c..91ddc06d99758 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Xsl/Xslt/XsltLoader.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Xsl/Xslt/XsltLoader.cs @@ -953,7 +953,7 @@ private void LoadDecimalFormat(NsDecl? stylesheetNsList) } char[] DefaultValues = DecimalFormatDecl.Default.Characters; - char[] characters = new char[NumCharAttrs]; + Span characters = stackalloc char[NumCharAttrs]; Debug.Assert(NumCharAttrs == DefaultValues.Length); for (int idx = 0; idx < NumCharAttrs; idx++)