Skip to content

Commit 34d1db9

Browse files
committed
Pre-size collections
1 parent 24909ca commit 34d1db9

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
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: 4 additions & 10 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;
@@ -734,16 +735,9 @@ private PhysicalOperation planProject(ProjectExec project, LocalExecutionPlanner
734735
List<Integer> projectionList = new ArrayList<>(projections.size());
735736

736737
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-
}
738+
Map<Integer, Layout.ChannelSet> inputChannelToOutputIds = Maps.newHashMapWithExpectedSize(projections.size());
739+
for (NamedExpression ne : projections) {
740+
NameId inputId = ne instanceof Alias a ? ((NamedExpression) a.child()).id() : ne.id();
747741
Layout.ChannelAndType input = source.layout.get(inputId);
748742
if (input == null) {
749743
throw new IllegalStateException("can't find input for [" + ne + "]");

0 commit comments

Comments
 (0)