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 authored Mar 9, 2018
1 parent a93b0fc commit 80ce3fa
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

public interface ClusterStateTaskExecutor<T> {
/**
Expand Down Expand Up @@ -55,15 +56,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 @@ -352,7 +352,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 80ce3fa

Please sign in to comment.