Cache SystemEnvironmentPropertyMapper configuration property names

Add a static cache to `SystemEnvironmentPropertyMapper` for property
source name to configuration property name conversion. This update
should help ease object allocations for system environment names which
are unlikely to change often.

Closes gh-14121
This commit is contained in:
Phillip Webb
2025-11-05 14:30:31 -08:00
parent 619454548b
commit 08857b4c7f
@@ -19,9 +19,11 @@ package org.springframework.boot.context.properties.source;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.BiPredicate;
import org.springframework.boot.context.properties.source.ConfigurationPropertyName.ToStringFormat;
import org.springframework.util.ConcurrentReferenceHashMap;
/**
* {@link PropertyMapper} for system environment variables. Names are mapped by removing
@@ -39,6 +41,8 @@ final class SystemEnvironmentPropertyMapper implements PropertyMapper {
public static final PropertyMapper INSTANCE = new SystemEnvironmentPropertyMapper();
private final Map<String, ConfigurationPropertyName> propertySourceNameCache = new ConcurrentReferenceHashMap<>();
@Override
public List<String> map(ConfigurationPropertyName configurationPropertyName) {
List<String> mapped = new ArrayList<>(4);
@@ -57,7 +61,12 @@ final class SystemEnvironmentPropertyMapper implements PropertyMapper {
@Override
public ConfigurationPropertyName map(String propertySourceName) {
return convertName(propertySourceName);
ConfigurationPropertyName configurationPropertyName = this.propertySourceNameCache.get(propertySourceName);
if (configurationPropertyName == null) {
configurationPropertyName = convertName(propertySourceName);
this.propertySourceNameCache.put(propertySourceName, configurationPropertyName);
}
return configurationPropertyName;
}
private ConfigurationPropertyName convertName(String propertySourceName) {