Improve performance of CompositePropertySource#getPropertyNames

Create LinkedHashSet with a initialCapacity, prevent under the hood
table resize cost in continuous add operations. Reduce bootstrap time
in the case of large properties.

See gh-27236
This commit is contained in:
shawyeok
2021-08-02 11:55:04 +08:00
committed by Stephane Nicoll
parent e1826d2322
commit b67a381fbe
2 changed files with 89 additions and 3 deletions

View File

@@ -78,15 +78,22 @@ public class CompositePropertySource extends EnumerablePropertySource<Object> {
@Override
public String[] getPropertyNames() {
Set<String> names = new LinkedHashSet<>();
List<String[]> namesList = new ArrayList<>(this.propertySources.size());
int total = 0;
for (PropertySource<?> propertySource : this.propertySources) {
if (!(propertySource instanceof EnumerablePropertySource<?> enumerablePropertySource)) {
throw new IllegalStateException(
"Failed to enumerate property names due to non-enumerable property source: " + propertySource);
}
names.addAll(Arrays.asList(enumerablePropertySource.getPropertyNames()));
String[] names = enumerablePropertySource.getPropertyNames();
namesList.add(names);
total += names.length;
}
return StringUtils.toStringArray(names);
Set<String> allNames = new LinkedHashSet<>(total);
for (String[] names : namesList) {
allNames.addAll(Arrays.asList(names));
}
return StringUtils.toStringArray(allNames);
}