Optimize HttpHeaders.getAcceptLanguageAsLocales

The HttpHeaders.getAcceptLanguageAsLocales was incurring overhead from
using a Stream, as well as calling the fairly expensive
Locale.getDisplayName method.

Switch to using an ArrayList, and skipping over wildcard ranges to avoid
needing to check the display name.

Closes gh-32318
This commit is contained in:
Patrick Strawderman
2024-02-20 17:16:56 -08:00
committed by Arjen Poutsma
parent d45c0e6b8a
commit beb415dfa3
2 changed files with 17 additions and 8 deletions

View File

@@ -527,10 +527,14 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
if (ranges.isEmpty()) {
return Collections.emptyList();
}
return ranges.stream()
.map(range -> Locale.forLanguageTag(range.getRange()))
.filter(locale -> StringUtils.hasText(locale.getDisplayName()))
.toList();
List<Locale> locales = new ArrayList<>(ranges.size());
for (Locale.LanguageRange range : ranges) {
if (!range.getRange().startsWith("*")) {
locales.add(Locale.forLanguageTag(range.getRange()));
}
}
return locales;
}
/**