Skip to content

Commit

Permalink
Merge pull request #8232 from dotnet/backport/pr-8231-to-release/8.0.1xx
Browse files Browse the repository at this point in the history
[release/8.0.1xx] Avoid allocations from calls to CultureInfo.GetCultures
  • Loading branch information
Forgind authored Aug 7, 2024
2 parents 11cd79c + 663a610 commit 4a85a62
Showing 1 changed file with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,14 @@ internal Task ValidateAsync(ValidationScope scope, CancellationToken cancellatio
{
string filename = locFile.Name;
string localeStr = filename.Substring(LocalizationFilePrefix.Length, filename.Length - LocalizationFilePrefix.Length - LocalizationFileExtension.Length);
CultureInfo? locale = null;

CultureInfo? locale = CultureInfo.GetCultures(CultureTypes.AllCultures).FirstOrDefault(c => c.Name.Equals(localeStr, StringComparison.OrdinalIgnoreCase));
if (locale == null)
try
{
// PERF: Avoid calling CultureInfo.GetCultures and searching the results as it heavily allocates on each invocation.
locale = CultureInfo.GetCultureInfo(localeStr);
}
catch (CultureNotFoundException)
{
Logger.LogWarning(LocalizableStrings.LocalizationModelDeserializer_Error_UnknownLocale, localeStr);
}
Expand Down

0 comments on commit 4a85a62

Please sign in to comment.