Skip to content

Commit 47b378e

Browse files
committed
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
1 parent 39e2aaa commit 47b378e

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,9 @@ private String buildToString() {
373373
ElementType.DASHED)) {
374374
return this.elements.getSource().toString();
375375
}
376-
StringBuilder result = new StringBuilder();
377-
for (int i = 0; i < getNumberOfElements(); i++) {
376+
int elements = getNumberOfElements();
377+
StringBuilder result = new StringBuilder(elements * 8);
378+
for (int i = 0; i < elements; i++) {
378379
boolean indexed = isIndexed(i);
379380
if (result.length() > 0 && !indexed) {
380381
result.append('.');

0 commit comments

Comments
 (0)