Browse Source

Derive StringBuilder's size from number of elements

Previously, when ConfigurationPropertyName was building the String
returned from toString() it would use a StringBuilder with the
default initial capacity of 16. For properties with several
elements this was likely to be too small resulting in the builder's
buffer being resized.

This commit sizes the StringBuilder as a multiple of the number of
elements in the name, attempting to strike a balance between
allocating a StringBuilder with an initial capacity that's too
large and wastes memory and an initial capacity that's too small
and requires resizing.

See gh-15760
pull/15799/head
Andy Wilkinson 7 years ago
parent
commit
47b378e373
  1. 5
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java

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

@ -373,8 +373,9 @@ public final class ConfigurationPropertyName @@ -373,8 +373,9 @@ public final class ConfigurationPropertyName
ElementType.DASHED)) {
return this.elements.getSource().toString();
}
StringBuilder result = new StringBuilder();
for (int i = 0; i < getNumberOfElements(); i++) {
int elements = getNumberOfElements();
StringBuilder result = new StringBuilder(elements * 8);
for (int i = 0; i < elements; i++) {
boolean indexed = isIndexed(i);
if (result.length() > 0 && !indexed) {
result.append('.');

Loading…
Cancel
Save