Skip to content

Commit

Permalink
Use String.join() to describe a list of tasks (elastic#28941)
Browse files Browse the repository at this point in the history
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 elastic#28920
includes a report that this operation sometimes takes a long time.
  • Loading branch information
DaveCTurner committed Mar 9, 2018
1 parent 924247d commit 90bde12
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,7 @@ default void clusterStatePublished(ClusterChangedEvent clusterChangedEvent) {
* This allows groupd task description but the submitting source.
*/
default String describeTasks(List<T> tasks) {
return tasks.stream().map(T::toString).reduce((s1,s2) -> {
if (s1.isEmpty()) {
return s2;
} else if (s2.isEmpty()) {
return s1;
} else {
return s1 + ", " + s2;
}
}).orElse("");
return String.join(", ", tasks.stream().map(t -> (CharSequence)t.toString()).filter(t -> t.length() == 0)::iterator);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ private ClusterState applyRequest(ClusterState currentState, PutMappingClusterSt

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

Expand Down

0 comments on commit 90bde12

Please sign in to comment.