Skip to content

Commit c2d0c59

Browse files
authored
Pre-size collections (#126382)
1 parent 284121a commit c2d0c59

File tree

3 files changed

+18
-33
lines changed

3 files changed

+18
-33
lines changed

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/optimizer/rules/logical/CombineProjections.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ private static List<NamedExpression> combineProjections(List<? extends NamedExpr
117117
// collect named expressions declaration in the lower list
118118
AttributeMap.Builder<NamedExpression> namedExpressionsBuilder = AttributeMap.builder();
119119
// while also collecting the alias map for resolving the source (f1 = 1, f2 = f1, etc..)
120-
AttributeMap.Builder<Expression> aliasesBuilder = AttributeMap.builder();
120+
AttributeMap.Builder<Expression> aliasesBuilder = AttributeMap.builder(lower.size());
121121
for (NamedExpression ne : lower) {
122122
// record the alias
123123
aliasesBuilder.put(ne.toAttribute(), Alias.unwrap(ne));
@@ -128,7 +128,7 @@ private static List<NamedExpression> combineProjections(List<? extends NamedExpr
128128
namedExpressionsBuilder.put(ne.toAttribute(), as.replaceChild(aliasesBuilder.build().resolve(child, child)));
129129
}
130130
}
131-
List<NamedExpression> replaced = new ArrayList<>();
131+
List<NamedExpression> replaced = new ArrayList<>(upper.size());
132132
var namedExpressions = namedExpressionsBuilder.build();
133133

134134
// replace any matching attribute with a lower alias (if there's a match)

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/planner/Layout.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77

88
package org.elasticsearch.xpack.esql.planner;
99

10+
import org.elasticsearch.common.util.Maps;
1011
import org.elasticsearch.xpack.esql.core.expression.NameId;
1112
import org.elasticsearch.xpack.esql.core.expression.NamedExpression;
1213
import org.elasticsearch.xpack.esql.core.type.DataType;
1314

1415
import java.util.ArrayList;
1516
import java.util.Collection;
1617
import java.util.Collections;
17-
import java.util.HashMap;
1818
import java.util.List;
1919
import java.util.Map;
2020
import java.util.Set;
@@ -65,7 +65,7 @@ class Builder {
6565
private final List<ChannelSet> channels;
6666

6767
public Builder() {
68-
channels = new ArrayList<>();
68+
this(new ArrayList<>());
6969
}
7070

7171
Builder(List<ChannelSet> channels) {
@@ -76,7 +76,7 @@ public Builder() {
7676
* Appends a new channel to the layout. The channel is mapped to one or more attribute ids.
7777
*/
7878
public Builder append(ChannelSet set) {
79-
if (set.nameIds.size() < 1) {
79+
if (set.nameIds.isEmpty()) {
8080
throw new IllegalArgumentException("Channel must be mapped to at least one id.");
8181
}
8282
channels.add(set);
@@ -104,7 +104,11 @@ public Builder append(Collection<? extends NamedExpression> attributes) {
104104
* Build a new {@link Layout}.
105105
*/
106106
public Layout build() {
107-
Map<NameId, ChannelAndType> layout = new HashMap<>();
107+
int size = 0;
108+
for (ChannelSet channel : channels) {
109+
size += channel.nameIds.size();
110+
}
111+
Map<NameId, ChannelAndType> layout = Maps.newHashMapWithExpectedSize(size);
108112
int numberOfChannels = 0;
109113
for (ChannelSet set : channels) {
110114
int channel = numberOfChannels++;

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/planner/LocalExecutionPlanner.java

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.elasticsearch.common.lucene.BytesRefs;
1212
import org.elasticsearch.common.settings.Settings;
1313
import org.elasticsearch.common.util.BigArrays;
14+
import org.elasticsearch.common.util.Maps;
1415
import org.elasticsearch.compute.Describable;
1516
import org.elasticsearch.compute.aggregation.AggregatorMode;
1617
import org.elasticsearch.compute.data.Block;
@@ -117,9 +118,6 @@
117118
import org.elasticsearch.xpack.esql.session.Configuration;
118119

119120
import java.util.ArrayList;
120-
import java.util.HashMap;
121-
import java.util.HashSet;
122-
import java.util.LinkedHashMap;
123121
import java.util.List;
124122
import java.util.Map;
125123
import java.util.Objects;
@@ -501,8 +499,8 @@ private PhysicalOperation planGrok(GrokExec grok, LocalExecutionPlannerContext c
501499
Layout.Builder layoutBuilder = source.layout.builder();
502500
List<Attribute> extractedFields = grok.extractedFields();
503501
layoutBuilder.append(extractedFields);
504-
Map<String, Integer> fieldToPos = new HashMap<>(extractedFields.size());
505-
Map<String, ElementType> fieldToType = new HashMap<>(extractedFields.size());
502+
Map<String, Integer> fieldToPos = Maps.newHashMapWithExpectedSize(extractedFields.size());
503+
Map<String, ElementType> fieldToType = Maps.newHashMapWithExpectedSize(extractedFields.size());
506504
ElementType[] types = new ElementType[extractedFields.size()];
507505
List<Attribute> extractedFieldsFromPattern = grok.pattern().extractedFields();
508506
for (int i = 0; i < extractedFields.size(); i++) {
@@ -559,7 +557,8 @@ private PhysicalOperation planEnrich(EnrichExec enrich, LocalExecutionPlannerCon
559557
private PhysicalOperation planRerank(RerankExec rerank, LocalExecutionPlannerContext context) {
560558
PhysicalOperation source = plan(rerank.child(), context);
561559

562-
Map<ColumnInfoImpl, EvalOperator.ExpressionEvaluator.Factory> rerankFieldsEvaluatorSuppliers = new LinkedHashMap<>();
560+
Map<ColumnInfoImpl, EvalOperator.ExpressionEvaluator.Factory> rerankFieldsEvaluatorSuppliers = Maps
561+
.newLinkedHashMapWithExpectedSize(rerank.rerankFields().size());
563562

564563
for (var rerankField : rerank.rerankFields()) {
565564
rerankFieldsEvaluatorSuppliers.put(
@@ -734,31 +733,13 @@ private PhysicalOperation planProject(ProjectExec project, LocalExecutionPlanner
734733
List<Integer> projectionList = new ArrayList<>(projections.size());
735734

736735
Layout.Builder layout = new Layout.Builder();
737-
Map<Integer, Layout.ChannelSet> inputChannelToOutputIds = new HashMap<>();
738-
for (int index = 0, size = projections.size(); index < size; index++) {
739-
NamedExpression ne = projections.get(index);
740-
741-
NameId inputId = null;
742-
if (ne instanceof Alias a) {
743-
inputId = ((NamedExpression) a.child()).id();
744-
} else {
745-
inputId = ne.id();
746-
}
736+
for (NamedExpression ne : projections) {
737+
NameId inputId = ne instanceof Alias a ? ((NamedExpression) a.child()).id() : ne.id();
747738
Layout.ChannelAndType input = source.layout.get(inputId);
748739
if (input == null) {
749740
throw new IllegalStateException("can't find input for [" + ne + "]");
750741
}
751-
Layout.ChannelSet channelSet = inputChannelToOutputIds.get(input.channel());
752-
if (channelSet == null) {
753-
channelSet = new Layout.ChannelSet(new HashSet<>(), input.type());
754-
channelSet.nameIds().add(ne.id());
755-
layout.append(channelSet);
756-
} else {
757-
channelSet.nameIds().add(ne.id());
758-
}
759-
if (channelSet.type() != input.type()) {
760-
throw new IllegalArgumentException("type mismatch for aliases");
761-
}
742+
layout.append(ne);
762743
projectionList.add(input.channel());
763744
}
764745

0 commit comments

Comments
 (0)