Skip to content

Commit 08857b4

Browse files
committed
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
1 parent 6194545 commit 08857b4

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
import java.util.ArrayList;
2020
import java.util.List;
2121
import java.util.Locale;
22+
import java.util.Map;
2223
import java.util.function.BiPredicate;
2324

2425
import org.springframework.boot.context.properties.source.ConfigurationPropertyName.ToStringFormat;
26+
import org.springframework.util.ConcurrentReferenceHashMap;
2527

2628
/**
2729
* {@link PropertyMapper} for system environment variables. Names are mapped by removing
@@ -39,6 +41,8 @@ final class SystemEnvironmentPropertyMapper implements PropertyMapper {
3941

4042
public static final PropertyMapper INSTANCE = new SystemEnvironmentPropertyMapper();
4143

44+
private final Map<String, ConfigurationPropertyName> propertySourceNameCache = new ConcurrentReferenceHashMap<>();
45+
4246
@Override
4347
public List<String> map(ConfigurationPropertyName configurationPropertyName) {
4448
List<String> mapped = new ArrayList<>(4);
@@ -57,7 +61,12 @@ private void addIfMissing(List<String> list, String value) {
5761

5862
@Override
5963
public ConfigurationPropertyName map(String propertySourceName) {
60-
return convertName(propertySourceName);
64+
ConfigurationPropertyName configurationPropertyName = this.propertySourceNameCache.get(propertySourceName);
65+
if (configurationPropertyName == null) {
66+
configurationPropertyName = convertName(propertySourceName);
67+
this.propertySourceNameCache.put(propertySourceName, configurationPropertyName);
68+
}
69+
return configurationPropertyName;
6170
}
6271

6372
private ConfigurationPropertyName convertName(String propertySourceName) {

0 commit comments

Comments
 (0)