Re-use StringBuilder inside iteration (#32759)
authorAlaa Masoud <alaa.masoud@live.com>
Wed, 11 Mar 2020 14:59:59 +0000 (17:59 +0300)
committerGitHub <noreply@github.com>
Wed, 11 Mar 2020 14:59:59 +0000 (10:59 -0400)
* Re-use StringBuilder inside iteration

* another one

* Reuse StringBuilder in tools

* Revert accidental formatting

* pr feedback

* formatting

* 1 more

src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/EventLog.cs
src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/EventLogEntry.cs
src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/Utils.cs

index 0249cb9..a37d1f9 100644 (file)
@@ -807,13 +807,22 @@ namespace System.Diagnostics
 
             int largestNumber = 0;
 
+            StringBuilder sb = null;
             for (int i = 0; i < formatString.Length; i++)
             {
                 if (formatString[i] == '%')
                 {
                     if (formatString.Length > i + 1)
                     {
-                        StringBuilder sb = new StringBuilder();
+                        if (sb is null)
+                        {
+                            sb = new StringBuilder();
+                        }
+                        else
+                        {
+                            sb.Clear();
+                        }
+
                         while (i + 1 < formatString.Length && char.IsDigit(formatString[i + 1]))
                         {
                             sb.Append(formatString[i + 1]);
@@ -833,6 +842,7 @@ namespace System.Diagnostics
                     }
                 }
             }
+
             // Replacement strings are 1 indexed.
             if (largestNumber > insertionStrings.Length)
             {
@@ -848,6 +858,7 @@ namespace System.Diagnostics
 
             return UnsafeTryFormatMessage(hModule, messageNum, insertionStrings);
         }
+
         // FormatMessageW will AV if you don't pass in enough format strings.  If you call TryFormatMessage we ensure insertionStrings
         // is long enough.  You don't want to call this directly unless you're sure insertionStrings is long enough!
         internal static string UnsafeTryFormatMessage(SafeLibraryHandle hModule, uint messageNum, string[] insertionStrings)
index ca73268..c9a8503 100644 (file)
@@ -217,7 +217,7 @@ namespace System.Diagnostics
                     {
                         strings[i] = buf.ToString();
                         i++;
-                        buf = new StringBuilder();
+                        buf.Clear();
                     }
 
                     bufpos += 2;
index af0a279..f7f6dd5 100644 (file)
@@ -1592,6 +1592,7 @@ namespace System.DirectoryServices.ActiveDirectory
 
                     if (needToContinueRangeRetrieval)
                     {
+                        StringBuilder str = new StringBuilder(20);
                         // Now continue with range retrieval if necessary for msDS-HasInstantiatedNCs
                         do
                         {
@@ -1600,7 +1601,7 @@ namespace System.DirectoryServices.ActiveDirectory
                             // this should be greater than 0, since needToContinueRangeRetrieval is true
                             Debug.Assert(ntdsaNamesForRangeRetrieval.Count > 0);
 
-                            StringBuilder str = new StringBuilder(20);
+                            str.Clear();
                             if (ntdsaNamesForRangeRetrieval.Count > 1)
                             {
                                 str.Append("(|");