LANG-1675 - Improve performance of StringUtils.join for primitives (#812)

* String.join - boolean - use StringBuilder

* Replaced StringJoiner by StringBuilder

* Fix: formatting, removed condition in for

* Use substring instead of deleteCharAt. Computed StringBuilder size for - char, boolean join methods

* Reformating
This commit is contained in:
Hubert 2022-04-03 18:52:56 +02:00 committed by GitHub
parent 0d729dd729
commit 0f7d484882
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 41 additions and 39 deletions

View File

@ -3930,11 +3930,13 @@ public class StringUtils {
if (endIndex - startIndex <= 0) {
return EMPTY;
}
final StringJoiner joiner = newStringJoiner(delimiter);
final StringBuilder stringBuilder = new StringBuilder(array.length * 5 + array.length - 1);
for (int i = startIndex; i < endIndex; i++) {
joiner.add(String.valueOf(array[i]));
stringBuilder
.append(array[i])
.append(delimiter);
}
return joiner.toString();
return stringBuilder.substring(0, stringBuilder.length() - 1);
}
/**
@ -4007,11 +4009,13 @@ public class StringUtils {
if (endIndex - startIndex <= 0) {
return EMPTY;
}
final StringJoiner joiner = newStringJoiner(delimiter);
final StringBuilder stringBuilder = new StringBuilder();
for (int i = startIndex; i < endIndex; i++) {
joiner.add(String.valueOf(array[i]));
stringBuilder
.append(array[i])
.append(delimiter);
}
return joiner.toString();
return stringBuilder.substring(0, stringBuilder.length() - 1);
}
/**
@ -4084,11 +4088,13 @@ public class StringUtils {
if (endIndex - startIndex <= 0) {
return EMPTY;
}
final StringJoiner joiner = newStringJoiner(delimiter);
final StringBuilder stringBuilder = new StringBuilder(array.length * 2 - 1);
for (int i = startIndex; i < endIndex; i++) {
joiner.add(String.valueOf(array[i]));
stringBuilder
.append(array[i])
.append(delimiter);
}
return joiner.toString();
return stringBuilder.substring(0, stringBuilder.length() - 1);
}
/**
@ -4161,11 +4167,13 @@ public class StringUtils {
if (endIndex - startIndex <= 0) {
return EMPTY;
}
final StringJoiner joiner = newStringJoiner(delimiter);
final StringBuilder stringBuilder = new StringBuilder();
for (int i = startIndex; i < endIndex; i++) {
joiner.add(String.valueOf(array[i]));
stringBuilder
.append(array[i])
.append(delimiter);
}
return joiner.toString();
return stringBuilder.substring(0, stringBuilder.length() - 1);
}
/**
@ -4238,11 +4246,13 @@ public class StringUtils {
if (endIndex - startIndex <= 0) {
return EMPTY;
}
final StringJoiner joiner = newStringJoiner(delimiter);
final StringBuilder stringBuilder = new StringBuilder();
for (int i = startIndex; i < endIndex; i++) {
joiner.add(String.valueOf(array[i]));
stringBuilder
.append(array[i])
.append(delimiter);
}
return joiner.toString();
return stringBuilder.substring(0, stringBuilder.length() - 1);
}
/**
@ -4315,11 +4325,13 @@ public class StringUtils {
if (endIndex - startIndex <= 0) {
return EMPTY;
}
final StringJoiner joiner = newStringJoiner(delimiter);
final StringBuilder stringBuilder = new StringBuilder();
for (int i = startIndex; i < endIndex; i++) {
joiner.add(String.valueOf(array[i]));
stringBuilder
.append(array[i])
.append(delimiter);
}
return joiner.toString();
return stringBuilder.substring(0, stringBuilder.length() - 1);
}
/**
@ -4601,11 +4613,13 @@ public class StringUtils {
if (endIndex - startIndex <= 0) {
return EMPTY;
}
final StringJoiner joiner = newStringJoiner(delimiter);
final StringBuilder stringBuilder = new StringBuilder();
for (int i = startIndex; i < endIndex; i++) {
joiner.add(String.valueOf(array[i]));
stringBuilder
.append(array[i])
.append(delimiter);
}
return joiner.toString();
return stringBuilder.substring(0, stringBuilder.length() - 1);
}
/**
@ -4664,17 +4678,7 @@ public class StringUtils {
* @since 2.0
*/
public static String join(final Object[] array, final char delimiter, final int startIndex, final int endIndex) {
if (array == null) {
return null;
}
if (endIndex - startIndex <= 0) {
return EMPTY;
}
final StringJoiner joiner = newStringJoiner(delimiter);
for (int i = startIndex; i < endIndex; i++) {
joiner.add(toStringOrEmpty(array[i]));
}
return joiner.toString();
return join(array, String.valueOf(delimiter), startIndex, endIndex);
}
/**
@ -4826,11 +4830,13 @@ public class StringUtils {
if (endIndex - startIndex <= 0) {
return EMPTY;
}
final StringJoiner joiner = newStringJoiner(delimiter);
final StringBuilder stringBuilder = new StringBuilder();
for (int i = startIndex; i < endIndex; i++) {
joiner.add(String.valueOf(array[i]));
stringBuilder
.append(array[i])
.append(delimiter);
}
return joiner.toString();
return stringBuilder.substring(0, stringBuilder.length() - 1);
}
/**
@ -5522,10 +5528,6 @@ public class StringUtils {
return str.substring(pos, pos + len);
}
private static StringJoiner newStringJoiner(final char delimiter) {
return new StringJoiner(String.valueOf(delimiter));
}
/**
* <p>
* Similar to <a