From 47b378e37361ad9709fdfd11e398820843820a95 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 23 Jan 2019 14:49:16 +0000 Subject: [PATCH] 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 --- .../context/properties/source/ConfigurationPropertyName.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java index 0af5f76fdc5..ad3e5b3350c 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java @@ -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('.');