Skip to content

Commit 61c0a80

Browse files
authored
Merge pull request #8231 from ToddGrun/GetCultureInfoPerf
Avoid allocations from calls to CultureInfo.GetCultures
2 parents b372327 + 917f1f7 commit 61c0a80

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/Microsoft.TemplateEngine.Orchestrator.RunnableProjects/DirectoryBasedTemplate.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,14 @@ internal Task ValidateAsync(ValidationScope scope, CancellationToken cancellatio
136136
{
137137
string filename = locFile.Name;
138138
string localeStr = filename.Substring(LocalizationFilePrefix.Length, filename.Length - LocalizationFilePrefix.Length - LocalizationFileExtension.Length);
139+
CultureInfo? locale = null;
139140

140-
CultureInfo? locale = CultureInfo.GetCultures(CultureTypes.AllCultures).FirstOrDefault(c => c.Name.Equals(localeStr, StringComparison.OrdinalIgnoreCase));
141-
if (locale == null)
141+
try
142+
{
143+
// PERF: Avoid calling CultureInfo.GetCultures and searching the results as it heavily allocates on each invocation.
144+
locale = CultureInfo.GetCultureInfo(localeStr);
145+
}
146+
catch (CultureNotFoundException)
142147
{
143148
Logger.LogWarning(LocalizableStrings.LocalizationModelDeserializer_Error_UnknownLocale, localeStr);
144149
}

0 commit comments

Comments
 (0)