Remove StringBuilder allocations from roundtrip format DateTime parsing
authorStephen Toub <stoub@microsoft.com>
Fri, 8 Sep 2017 14:25:01 +0000 (10:25 -0400)
committerStephen Toub <stoub@microsoft.com>
Fri, 6 Oct 2017 12:27:42 +0000 (08:27 -0400)
src/mscorlib/shared/System/Globalization/DateTimeParse.cs
src/mscorlib/shared/System/Globalization/TimeSpanParse.cs

index cad9ae8..0a1cf9e 100644 (file)
@@ -4201,11 +4201,12 @@ new DS[] { DS.ERROR, DS.TX_NNN,  DS.TX_NNN,  DS.TX_NNN,  DS.ERROR,   DS.ERROR,
                     break;
                 case '\"':
                 case '\'':
-                    StringBuilder enquotedString = new StringBuilder();
+                    StringBuilder enquotedString = StringBuilderCache.Acquire();
                     // Use ParseQuoteString so that we can handle escape characters within the quoted string.
                     if (!TryParseQuoteString(format.Value, format.Index, enquotedString, out tokenLen))
                     {
                         result.SetFailure(ParseFailureKind.FormatWithParameter, nameof(SR.Format_BadQuote), ch);
+                        StringBuilderCache.Release(enquotedString);
                         return (false);
                     }
                     format.Index += tokenLen - 1;
@@ -4213,7 +4214,7 @@ new DS[] { DS.ERROR, DS.TX_NNN,  DS.TX_NNN,  DS.TX_NNN,  DS.ERROR,   DS.ERROR,
                     // Some cultures uses space in the quoted string.  E.g. Spanish has long date format as:
                     // "dddd, dd' de 'MMMM' de 'yyyy".  When inner spaces flag is set, we should skip whitespaces if there is space
                     // in the quoted string.
-                    String quotedStr = enquotedString.ToString();
+                    String quotedStr = StringBuilderCache.GetStringAndRelease(enquotedString);
 
                     for (int i = 0; i < quotedStr.Length; i++)
                     {
index 6e7d784..8f511b7 100644 (file)
@@ -1292,15 +1292,18 @@ namespace System.Globalization
 
                     case '\'':
                     case '\"':
-                        StringBuilder enquotedString = new StringBuilder();
+                        StringBuilder enquotedString = StringBuilderCache.Acquire();
                         if (!DateTimeParse.TryParseQuoteString(format.AsReadOnlySpan(), i, enquotedString, out tokenLen))
                         {
+                            StringBuilderCache.Release(enquotedString);
                             return result.SetFailure(ParseFailureKind.FormatWithParameter, nameof(SR.Format_BadQuote), ch);
                         }
                         if (!ParseExactLiteral(ref tokenizer, enquotedString))
                         {
+                            StringBuilderCache.Release(enquotedString);
                             return result.SetFailure(ParseFailureKind.Format, nameof(SR.Format_InvalidString));
                         }
+                        StringBuilderCache.Release(enquotedString);
                         break;
 
                     case '%':