From 90bde12af73d0f3797ebaa91fbfcf51038c60746 Mon Sep 17 00:00:00 2001 From: David Turner Date: Fri, 9 Mar 2018 09:57:53 +0000 Subject: [PATCH] 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. --- .../cluster/ClusterStateTaskExecutor.java | 10 +--------- .../cluster/metadata/MetaDataMappingService.java | 2 +- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/cluster/ClusterStateTaskExecutor.java b/core/src/main/java/org/elasticsearch/cluster/ClusterStateTaskExecutor.java index 3693447cfb6d2..6cc892d15e63c 100644 --- a/core/src/main/java/org/elasticsearch/cluster/ClusterStateTaskExecutor.java +++ b/core/src/main/java/org/elasticsearch/cluster/ClusterStateTaskExecutor.java @@ -55,15 +55,7 @@ default void clusterStatePublished(ClusterChangedEvent clusterChangedEvent) { * This allows groupd task description but the submitting source. */ default String describeTasks(List 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); } /** diff --git a/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataMappingService.java b/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataMappingService.java index 865b58c468a52..f647984f7ce2e 100644 --- a/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataMappingService.java +++ b/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataMappingService.java @@ -350,7 +350,7 @@ private ClusterState applyRequest(ClusterState currentState, PutMappingClusterSt @Override public String describeTasks(List tasks) { - return tasks.stream().map(PutMappingClusterStateUpdateRequest::type).reduce((s1, s2) -> s1 + ", " + s2).orElse(""); + return String.join(", ", tasks.stream().map(t -> (CharSequence)t.type())::iterator); } }