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
This commit is contained in:
Andy Wilkinson
2019-01-23 14:49:16 +00:00
parent 39e2aaa41c
commit 47b378e373

View File

@@ -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('.');