Skip to content

Commit 83725f8

Browse files
committed
Add containsDescendantOfCache for system property source
Add a simple cache that is used for the system environment property source to cache `containsDescendantOf` results since descendants cannot be used due to the more complicated algorithm. Closes gh-44863
1 parent d9d206a commit 83725f8

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SpringIterableConfigurationPropertySource.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.springframework.core.env.PropertySource;
3939
import org.springframework.core.env.StandardEnvironment;
4040
import org.springframework.core.env.SystemEnvironmentPropertySource;
41+
import org.springframework.util.ConcurrentReferenceHashMap;
4142

4243
/**
4344
* {@link ConfigurationPropertySource} backed by an {@link EnumerablePropertySource}.
@@ -59,13 +60,16 @@ class SpringIterableConfigurationPropertySource extends SpringConfigurationPrope
5960

6061
private volatile ConfigurationPropertyName[] configurationPropertyNames;
6162

63+
private final Map<ConfigurationPropertyName, ConfigurationPropertyState> containsDescendantOfCache;
64+
6265
SpringIterableConfigurationPropertySource(EnumerablePropertySource<?> propertySource,
6366
boolean systemEnvironmentSource, PropertyMapper... mappers) {
6467
super(propertySource, systemEnvironmentSource, mappers);
6568
assertEnumerablePropertySource();
6669
boolean immutable = isImmutablePropertySource();
6770
this.ancestorOfCheck = getAncestorOfCheck(mappers);
6871
this.cache = new SoftReferenceConfigurationPropertyCache<>(immutable);
72+
this.containsDescendantOfCache = (!systemEnvironmentSource) ? null : new ConcurrentReferenceHashMap<>();
6973
}
7074

7175
private BiPredicate<ConfigurationPropertyName, ConfigurationPropertyName> getAncestorOfCheck(
@@ -145,13 +149,24 @@ public ConfigurationPropertyState containsDescendantOf(ConfigurationPropertyName
145149
: ConfigurationPropertyState.PRESENT;
146150
}
147151
}
152+
result = (this.containsDescendantOfCache != null) ? this.containsDescendantOfCache.get(name) : null;
153+
if (result == null) {
154+
result = (!ancestorOfCheck(name)) ? ConfigurationPropertyState.ABSENT : ConfigurationPropertyState.PRESENT;
155+
if (this.containsDescendantOfCache != null) {
156+
this.containsDescendantOfCache.put(name, result);
157+
}
158+
}
159+
return result;
160+
}
161+
162+
private boolean ancestorOfCheck(ConfigurationPropertyName name) {
148163
ConfigurationPropertyName[] candidates = getConfigurationPropertyNames();
149164
for (ConfigurationPropertyName candidate : candidates) {
150165
if (candidate != null && this.ancestorOfCheck.test(name, candidate)) {
151-
return ConfigurationPropertyState.PRESENT;
166+
return true;
152167
}
153168
}
154-
return ConfigurationPropertyState.ABSENT;
169+
return false;
155170
}
156171

157172
private ConfigurationPropertyName[] getConfigurationPropertyNames() {

0 commit comments

Comments
 (0)