From de9bff27e9c5900037987b499695237ea942d6a5 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Tue, 18 Apr 2023 00:38:16 -0400 Subject: [PATCH] Avoid unnecessary array allocation in DateTimeParse When using DateTime{Offset}.ParseExact with a culture that employs genitive month names, the public DateTimeFormatInfo.AbbreviatedMonthGenitiveNames and DateTimeFormatInfo.MonthGenitiveNames properties are being accessed to get a string[] of the month names, but those properties clone. If we instead access the internal non-cloning version, we save on an array allocation on each parse. --- .../src/System/Globalization/DateTimeFormatInfo.cs | 2 +- .../src/System/Globalization/DateTimeParse.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeFormatInfo.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeFormatInfo.cs index 6d07fa3ff9cb10..e661479b8e4f98 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeFormatInfo.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeFormatInfo.cs @@ -1204,7 +1204,7 @@ internal string InternalGetMonthName(int month, MonthNameStyles style, bool abbr /// Retrieve the array which contains the month names in genitive form. /// If this culture does not use the genitive form, the normal month name is returned. /// - private string[] InternalGetGenitiveMonthNames(bool abbreviated) + internal string[] InternalGetGenitiveMonthNames(bool abbreviated) { if (abbreviated) { diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeParse.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeParse.cs index ca1594b194eb31..ddf5bae5452cbc 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeParse.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeParse.cs @@ -3353,7 +3353,7 @@ private static bool MatchAbbreviatedMonthName(ref __DTString str, DateTimeFormat // Search genitive form. if ((dtfi.FormatFlags & DateTimeFormatFlags.UseGenitiveMonth) != 0) { - int tempResult = str.MatchLongestWords(dtfi.AbbreviatedMonthGenitiveNames, ref maxMatchStrLen); + int tempResult = str.MatchLongestWords(dtfi.InternalGetGenitiveMonthNames(abbreviated: true), ref maxMatchStrLen); // We found a longer match in the genitive month name. Use this as the result. // tempResult + 1 should be the month value. @@ -3453,7 +3453,7 @@ private static bool MatchMonthName(ref __DTString str, DateTimeFormatInfo dtfi, // Search genitive form. if ((dtfi.FormatFlags & DateTimeFormatFlags.UseGenitiveMonth) != 0) { - int tempResult = str.MatchLongestWords(dtfi.MonthGenitiveNames, ref maxMatchStrLen); + int tempResult = str.MatchLongestWords(dtfi.InternalGetGenitiveMonthNames(abbreviated: false), ref maxMatchStrLen); // We found a longer match in the genitive month name. Use this as the result. // The result from MatchLongestWords is 0 ~ length of word array. // So we increment the result by one to become the month value.