From 78ae3ab5ed9e198599a4577aa6ae73f053ac1c02 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Fri, 8 Sep 2017 10:25:01 -0400 Subject: [PATCH] Remove StringBuilder allocations from roundtrip format DateTime parsing --- src/mscorlib/shared/System/Globalization/DateTimeParse.cs | 5 +++-- src/mscorlib/shared/System/Globalization/TimeSpanParse.cs | 5 ++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/mscorlib/shared/System/Globalization/DateTimeParse.cs b/src/mscorlib/shared/System/Globalization/DateTimeParse.cs index cad9ae8..0a1cf9e 100644 --- a/src/mscorlib/shared/System/Globalization/DateTimeParse.cs +++ b/src/mscorlib/shared/System/Globalization/DateTimeParse.cs @@ -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++) { diff --git a/src/mscorlib/shared/System/Globalization/TimeSpanParse.cs b/src/mscorlib/shared/System/Globalization/TimeSpanParse.cs index 6e7d784..8f511b7 100644 --- a/src/mscorlib/shared/System/Globalization/TimeSpanParse.cs +++ b/src/mscorlib/shared/System/Globalization/TimeSpanParse.cs @@ -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 '%': -- 2.7.4