Skip to content

Commit 626f7f6

Browse files
committed
avoid intermediate map
1 parent 9b432fe commit 626f7f6

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

server/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,28 @@ public <K, V> Map<K, V> readMapValues(final Writeable.Reader<V> valueReader, fin
802802
return map;
803803
}
804804

805+
/**
806+
* Reads a multiple {@code V}-values and then converts them to a {@code Map} using keyMapper.
807+
*
808+
* @param valueReader The value reader
809+
* @param keyMapper function to create a key from a value
810+
* @param constructor map constructor
811+
* @return Never {@code null}.
812+
*/
813+
public <K, V, M extends Map<K, V>> M readMapValues(
814+
final Writeable.Reader<V> valueReader,
815+
final Function<V, K> keyMapper,
816+
final IntFunction<M> constructor
817+
) throws IOException {
818+
final int size = readArraySize();
819+
final M map = constructor.apply(size);
820+
for (int i = 0; i < size; i++) {
821+
V value = valueReader.read(this);
822+
map.put(keyMapper.apply(value), value);
823+
}
824+
return map;
825+
}
826+
805827
/**
806828
* If the returned map contains any entries it will be mutable. If it is empty it might be immutable.
807829
*/

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/action/EsqlExecutionInfo.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ public EsqlExecutionInfo(Predicate<String> skipUnavailablePredicate, boolean inc
108108

109109
public EsqlExecutionInfo(StreamInput in) throws IOException {
110110
this.overallTook = in.readOptionalTimeValue();
111-
this.clusterInfo = new ConcurrentHashMap<>(in.readMapValues(EsqlExecutionInfo.Cluster::new, Cluster::getClusterAlias));
112-
this.includeCCSMetadata = in.getTransportVersion().onOrAfter(TransportVersions.V_8_16_0) && in.readBoolean();
113-
this.isPartial = in.getTransportVersion().onOrAfter(TransportVersions.ESQL_RESPONSE_PARTIAL) && in.readBoolean();
111+
this.clusterInfo = in.readMapValues(EsqlExecutionInfo.Cluster::new, Cluster::getClusterAlias, ConcurrentHashMap::new);
112+
this.includeCCSMetadata = in.getTransportVersion().onOrAfter(TransportVersions.V_8_16_0) ? in.readBoolean() : false;
113+
this.isPartial = in.getTransportVersion().onOrAfter(TransportVersions.ESQL_RESPONSE_PARTIAL) ? in.readBoolean() : false;
114114
this.skipUnavailablePredicate = Predicates.always();
115115
this.relativeStart = null;
116116
if (in.getTransportVersion().onOrAfter(TransportVersions.ESQL_QUERY_PLANNING_DURATION)

0 commit comments

Comments
 (0)