Skip to content

Commit c792014

Browse files
committed
pre-size attribute set/map
1 parent 0464717 commit c792014

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/expression/AttributeMap.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
package org.elasticsearch.xpack.esql.core.expression;
88

9+
import org.elasticsearch.common.util.Maps;
910
import org.elasticsearch.common.util.set.Sets;
1011
import org.elasticsearch.xpack.esql.core.QlIllegalArgumentException;
1112

@@ -169,21 +170,25 @@ public AttributeMap() {
169170
delegate = new LinkedHashMap<>();
170171
}
171172

173+
public AttributeMap(int expectedSize) {
174+
delegate = Maps.newLinkedHashMapWithExpectedSize(expectedSize);
175+
}
176+
172177
public AttributeMap(Attribute key, E value) {
173178
delegate = new LinkedHashMap<>();
174179
add(key, value);
175180
}
176181

177182
public AttributeMap<E> combine(AttributeMap<E> other) {
178-
AttributeMap<E> combine = new AttributeMap<>();
183+
AttributeMap<E> combine = new AttributeMap<>(this.size() + other.size());
179184
combine.addAll(this);
180185
combine.addAll(other);
181186

182187
return combine;
183188
}
184189

185190
public AttributeMap<E> subtract(AttributeMap<E> other) {
186-
AttributeMap<E> diff = new AttributeMap<>();
191+
AttributeMap<E> diff = new AttributeMap<>(this.size());
187192
for (Entry<AttributeWrapper, E> entry : this.delegate.entrySet()) {
188193
if (other.delegate.containsKey(entry.getKey()) == false) {
189194
diff.delegate.put(entry.getKey(), entry.getValue());
@@ -197,7 +202,7 @@ public AttributeMap<E> intersect(AttributeMap<E> other) {
197202
AttributeMap<E> smaller = (other.size() > size() ? this : other);
198203
AttributeMap<E> larger = (smaller == this ? other : this);
199204

200-
AttributeMap<E> intersect = new AttributeMap<>();
205+
AttributeMap<E> intersect = new AttributeMap<>(smaller.size());
201206
for (Entry<AttributeWrapper, E> entry : smaller.delegate.entrySet()) {
202207
if (larger.delegate.containsKey(entry.getKey())) {
203208
intersect.delegate.put(entry.getKey(), entry.getValue());
@@ -385,7 +390,7 @@ public static <E> Builder<E> builder(AttributeMap<E> map) {
385390
}
386391

387392
public static class Builder<E> {
388-
private AttributeMap<E> map = new AttributeMap<>();
393+
private final AttributeMap<E> map = new AttributeMap<>();
389394

390395
private Builder() {}
391396

x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/expression/AttributeSet.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@ public AttributeSet() {
3030
delegate = new AttributeMap<>();
3131
}
3232

33+
public AttributeSet(int expectedSize) {
34+
delegate = new AttributeMap<>(expectedSize);
35+
}
36+
3337
public AttributeSet(Attribute attr) {
3438
delegate = new AttributeMap<>(attr, PRESENT);
3539
}
3640

3741
public AttributeSet(Collection<? extends Attribute> attr) {
38-
delegate = new AttributeMap<>();
39-
42+
delegate = new AttributeMap<>(attr.size());
4043
for (Attribute a : attr) {
4144
delegate.add(a, PRESENT);
4245
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/plan/QueryPlan.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,15 @@ public AttributeSet outputSet() {
4848

4949
public AttributeSet inputSet() {
5050
if (lazyInputSet == null) {
51-
List<Attribute> attrs = new ArrayList<>();
51+
int size = 0;
5252
for (PlanType child : children()) {
53-
attrs.addAll(child.output());
53+
size += child.output().size();
5454
}
55-
lazyInputSet = new AttributeSet(attrs);
55+
var attributes = new AttributeSet(size);
56+
for (PlanType child : children()) {
57+
attributes.addAll(child.output());
58+
}
59+
lazyInputSet = attributes;
5660
}
5761
return lazyInputSet;
5862
}

0 commit comments

Comments
 (0)