Skip to content

Commit 90bde12

Browse files
committed
Use String.join() to describe a list of tasks (#28941)
This change replaces the use of string concatenation with a call to String.join(). String concatenation might be quadratic, unless the compiler can optimise it away, whereas String.join() is more reliably linear. There can sometimes be a large number of pending ClusterState update tasks and #28920 includes a report that this operation sometimes takes a long time.
1 parent 924247d commit 90bde12

File tree

2 files changed

+2
-10
lines changed

2 files changed

+2
-10
lines changed

core/src/main/java/org/elasticsearch/cluster/ClusterStateTaskExecutor.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,7 @@ default void clusterStatePublished(ClusterChangedEvent clusterChangedEvent) {
5555
* This allows groupd task description but the submitting source.
5656
*/
5757
default String describeTasks(List<T> tasks) {
58-
return tasks.stream().map(T::toString).reduce((s1,s2) -> {
59-
if (s1.isEmpty()) {
60-
return s2;
61-
} else if (s2.isEmpty()) {
62-
return s1;
63-
} else {
64-
return s1 + ", " + s2;
65-
}
66-
}).orElse("");
58+
return String.join(", ", tasks.stream().map(t -> (CharSequence)t.toString()).filter(t -> t.length() == 0)::iterator);
6759
}
6860

6961
/**

core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataMappingService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ private ClusterState applyRequest(ClusterState currentState, PutMappingClusterSt
350350

351351
@Override
352352
public String describeTasks(List<PutMappingClusterStateUpdateRequest> tasks) {
353-
return tasks.stream().map(PutMappingClusterStateUpdateRequest::type).reduce((s1, s2) -> s1 + ", " + s2).orElse("");
353+
return String.join(", ", tasks.stream().map(t -> (CharSequence)t.type())::iterator);
354354
}
355355
}
356356

0 commit comments

Comments
 (0)