From abf523dcc2e52c5d2765b4400065104a47aabe41 Mon Sep 17 00:00:00 2001 From: Dejan Jankov Date: Wed, 25 Jul 2018 17:14:56 +0200 Subject: [PATCH] ConfigurationPropertyName performance optimizations Minimize implicit calls to Arrays.copyOf, and remove autoboxing of chars by using optimal StringBuilder#append overload. Closes gh-13414 --- .../source/ConfigurationPropertyName.java | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 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 c5cb44b45f32..07e684a57bdf 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 @@ -304,24 +304,27 @@ public String toString() { } private String toString(CharSequence[] elements) { - StringBuilder result = new StringBuilder(); - for (CharSequence element : elements) { - boolean indexed = isIndexed(element); - if (result.length() > 0 && !indexed) { - result.append("."); - } - if (indexed) { - result.append(element); - } - else { - for (int i = 0; i < element.length(); i++) { - char ch = Character.toLowerCase(element.charAt(i)); - result.append(ch != '_' ? ch : ""); - } - } - } - return result.toString(); - } + int assumedAverageElementLength = 8; + // initial capacity computed to avoid frequent throwing away of StringBuilder's + // char arrays during rapid doubling of arrays in order to satisfy capacity demands. + StringBuilder result = new StringBuilder(elements.length * assumedAverageElementLength); + for (CharSequence element : elements) { + if (isIndexed(element)) { + result.append(element); + } else { + if (result.length() > 0) { + result.append('.'); + } + for (int i = 0; i < element.length(); i++) { + char ch = element.charAt(i); + if (ch != '_') { + result.append(Character.toLowerCase(ch)); + } + } + } + } + return result.toString(); + } @Override public int hashCode() {