Browse Source

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
pull/47991/head
Phillip Webb 1 month ago
parent
commit
08857b4c7f
  1. 11
      core/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SystemEnvironmentPropertyMapper.java

11
core/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SystemEnvironmentPropertyMapper.java

@ -19,9 +19,11 @@ package org.springframework.boot.context.properties.source;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import java.util.function.BiPredicate; import java.util.function.BiPredicate;
import org.springframework.boot.context.properties.source.ConfigurationPropertyName.ToStringFormat; 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 * {@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(); public static final PropertyMapper INSTANCE = new SystemEnvironmentPropertyMapper();
private final Map<String, ConfigurationPropertyName> propertySourceNameCache = new ConcurrentReferenceHashMap<>();
@Override @Override
public List<String> map(ConfigurationPropertyName configurationPropertyName) { public List<String> map(ConfigurationPropertyName configurationPropertyName) {
List<String> mapped = new ArrayList<>(4); List<String> mapped = new ArrayList<>(4);
@ -57,7 +61,12 @@ final class SystemEnvironmentPropertyMapper implements PropertyMapper {
@Override @Override
public ConfigurationPropertyName map(String propertySourceName) { 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) { private ConfigurationPropertyName convertName(String propertySourceName) {

Loading…
Cancel
Save