Skip to content

Commit 5705b66

Browse files
committed
Add Maps.newLinkedHashMapWithExpectedSize
1 parent 74b5bfd commit 5705b66

File tree

21 files changed

+62
-40
lines changed

21 files changed

+62
-40
lines changed

modules/aggs-matrix-stats/src/main/java/org/elasticsearch/search/aggregations/matrix/stats/ParsedMatrixStats.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
package org.elasticsearch.search.aggregations.matrix.stats;
1010

11+
import org.elasticsearch.common.util.Maps;
1112
import org.elasticsearch.search.aggregations.ParsedAggregation;
1213
import org.elasticsearch.xcontent.ObjectParser;
1314
import org.elasticsearch.xcontent.ParseField;
@@ -202,14 +203,14 @@ static class ParsedMatrixStatsResult {
202203
);
203204

204205
RESULT_PARSER.declareObject((ParsedMatrixStatsResult result, Map<String, Object> covars) -> {
205-
result.covariances = new LinkedHashMap<>(covars.size());
206+
result.covariances = Maps.newLinkedHashMapWithExpectedSize(covars.size());
206207
for (Map.Entry<String, Object> covar : covars.entrySet()) {
207208
result.covariances.put(covar.getKey(), mapValueAsDouble(covar.getValue()));
208209
}
209210
}, (p, c) -> p.mapOrdered(), new ParseField(InternalMatrixStats.Fields.COVARIANCE));
210211

211212
RESULT_PARSER.declareObject((ParsedMatrixStatsResult result, Map<String, Object> correls) -> {
212-
result.correlations = new LinkedHashMap<>(correls.size());
213+
result.correlations = Maps.newLinkedHashMapWithExpectedSize(correls.size());
213214
for (Map.Entry<String, Object> correl : correls.entrySet()) {
214215
result.correlations.put(correl.getKey(), mapValueAsDouble(correl.getValue()));
215216
}

server/src/internalClusterTest/java/org/elasticsearch/index/seqno/RetentionLeaseIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.elasticsearch.cluster.routing.ShardRouting;
1717
import org.elasticsearch.common.settings.Setting;
1818
import org.elasticsearch.common.settings.Settings;
19+
import org.elasticsearch.common.util.Maps;
1920
import org.elasticsearch.core.TimeValue;
2021
import org.elasticsearch.index.IndexService;
2122
import org.elasticsearch.index.IndexSettings;
@@ -297,7 +298,7 @@ public void testBackgroundRetentionLeaseSync() throws Exception {
297298
.getShardOrNull(new ShardId(resolveIndex("index"), 0));
298299
// we will add multiple retention leases and expect to see them synced to all replicas
299300
final int length = randomIntBetween(1, 8);
300-
final Map<String, RetentionLease> currentRetentionLeases = new LinkedHashMap<>(length);
301+
final Map<String, RetentionLease> currentRetentionLeases = Maps.newLinkedHashMapWithExpectedSize(length);
301302
final List<String> ids = new ArrayList<>(length);
302303
for (int i = 0; i < length; i++) {
303304
final String id = randomValueOtherThanMany(currentRetentionLeases.keySet()::contains, () -> randomAlphaOfLength(8));

server/src/main/java/org/elasticsearch/cluster/routing/RoutingNode.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
package org.elasticsearch.cluster.routing;
1010

1111
import org.elasticsearch.cluster.node.DiscoveryNode;
12+
import org.elasticsearch.common.util.Maps;
1213
import org.elasticsearch.core.Nullable;
1314
import org.elasticsearch.index.Index;
1415
import org.elasticsearch.index.shard.ShardId;
1516

1617
import java.util.ArrayList;
1718
import java.util.Collection;
1819
import java.util.Collections;
19-
import java.util.HashMap;
2020
import java.util.Iterator;
2121
import java.util.LinkedHashMap;
2222
import java.util.LinkedHashSet;
@@ -44,7 +44,7 @@ public class RoutingNode implements Iterable<ShardRouting> {
4444

4545
private final LinkedHashSet<ShardRouting> relocatingShards;
4646

47-
private final HashMap<Index, LinkedHashSet<ShardRouting>> shardsByIndex;
47+
private final Map<Index, LinkedHashSet<ShardRouting>> shardsByIndex;
4848

4949
public RoutingNode(String nodeId, DiscoveryNode node, ShardRouting... shards) {
5050
this(nodeId, node, buildShardRoutingMap(shards));
@@ -74,7 +74,7 @@ private RoutingNode(RoutingNode original) {
7474
this.shards = new LinkedHashMap<>(original.shards);
7575
this.relocatingShards = new LinkedHashSet<>(original.relocatingShards);
7676
this.initializingShards = new LinkedHashSet<>(original.initializingShards);
77-
this.shardsByIndex = new LinkedHashMap<>(original.shardsByIndex.size());
77+
this.shardsByIndex = Maps.newLinkedHashMapWithExpectedSize(original.shardsByIndex.size());
7878
for (Map.Entry<Index, LinkedHashSet<ShardRouting>> entry : original.shardsByIndex.entrySet()) {
7979
shardsByIndex.put(entry.getKey(), new LinkedHashSet<>(entry.getValue()));
8080
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ private Map<String, Object> readLinkedHashMap() throws IOException {
784784
if (size9 == 0) {
785785
return Collections.emptyMap();
786786
}
787-
Map<String, Object> map9 = new LinkedHashMap<>(size9);
787+
Map<String, Object> map9 = Maps.newLinkedHashMapWithExpectedSize(size9);
788788
for (int i = 0; i < size9; i++) {
789789
map9.put(readString(), readGenericValue());
790790
}

server/src/main/java/org/elasticsearch/common/util/Maps.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,18 @@ public static <K, V> Map<K, V> newMapWithExpectedSize(int expectedSize) {
255255
return new HashMap<>(capacity(expectedSize));
256256
}
257257

258+
/**
259+
* Returns a linked hash map with a capacity sufficient to keep expectedSize elements without being resized.
260+
*
261+
* @param expectedSize the expected amount of elements in the map
262+
* @param <K> the key type
263+
* @param <V> the value type
264+
* @return a new pre-sized {@link LinkedHashMap}
265+
*/
266+
public static <K, V> Map<K, V> newLinkedHashMapWithExpectedSize(int expectedSize) {
267+
return new LinkedHashMap<>(capacity(expectedSize));
268+
}
269+
258270
static int capacity(int expectedSize) {
259271
assert expectedSize >= 0;
260272
return expectedSize < 2 ? expectedSize + 1 : (int) (expectedSize / 0.75 + 1.0);

server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/SearchHighlightContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
package org.elasticsearch.search.fetch.subphase.highlight;
1010

1111
import org.apache.lucene.search.Query;
12+
import org.elasticsearch.common.util.Maps;
1213
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.BoundaryScannerType;
1314

1415
import java.util.Arrays;
1516
import java.util.Collection;
1617
import java.util.HashMap;
17-
import java.util.LinkedHashMap;
1818
import java.util.Locale;
1919
import java.util.Map;
2020
import java.util.Set;
@@ -30,7 +30,7 @@ public SearchHighlightContext(Collection<Field> fields) {
3030

3131
public SearchHighlightContext(Collection<Field> fields, boolean globalForceSource) {
3232
assert fields != null;
33-
this.fields = new LinkedHashMap<>(fields.size());
33+
this.fields = Maps.newLinkedHashMapWithExpectedSize(fields.size());
3434
for (Field field : fields) {
3535
this.fields.put(field.field, field);
3636
}

server/src/main/java/org/elasticsearch/search/suggest/SuggestionSearchContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99

1010
import org.apache.lucene.analysis.Analyzer;
1111
import org.apache.lucene.util.BytesRef;
12+
import org.elasticsearch.common.util.Maps;
1213
import org.elasticsearch.index.query.SearchExecutionContext;
1314

14-
import java.util.LinkedHashMap;
1515
import java.util.Map;
1616

1717
public class SuggestionSearchContext {
1818

19-
private final Map<String, SuggestionContext> suggestions = new LinkedHashMap<>(4);
19+
private final Map<String, SuggestionContext> suggestions = Maps.newLinkedHashMapWithExpectedSize(4);
2020

2121
public void addSuggestion(String name, SuggestionContext suggestion) {
2222
suggestions.put(name, suggestion);

server/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggestion.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.elasticsearch.common.io.stream.StreamOutput;
1515
import org.elasticsearch.common.lucene.Lucene;
1616
import org.elasticsearch.common.text.Text;
17+
import org.elasticsearch.common.util.Maps;
1718
import org.elasticsearch.search.SearchHit;
1819
import org.elasticsearch.search.suggest.Suggest;
1920
import org.elasticsearch.search.suggest.Suggest.Suggestion;
@@ -27,7 +28,6 @@
2728
import java.util.HashMap;
2829
import java.util.HashSet;
2930
import java.util.Iterator;
30-
import java.util.LinkedHashMap;
3131
import java.util.List;
3232
import java.util.Map;
3333
import java.util.Objects;
@@ -274,7 +274,7 @@ public Option(StreamInput in) throws IOException {
274274
this.hit = new SearchHit(in);
275275
}
276276
int contextSize = in.readInt();
277-
this.contexts = new LinkedHashMap<>(contextSize);
277+
this.contexts = Maps.newLinkedHashMapWithExpectedSize(contextSize);
278278
for (int i = 0; i < contextSize; i++) {
279279
String contextName = in.readString();
280280
int nContexts = in.readVInt();

server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/AutoDateHistogramAggregatorTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.elasticsearch.cluster.metadata.IndexMetadata;
2828
import org.elasticsearch.common.CheckedBiConsumer;
2929
import org.elasticsearch.common.settings.Settings;
30+
import org.elasticsearch.common.util.Maps;
3031
import org.elasticsearch.index.IndexSettings;
3132
import org.elasticsearch.index.mapper.BooleanFieldMapper;
3233
import org.elasticsearch.index.mapper.DateFieldMapper;
@@ -57,7 +58,6 @@
5758
import java.util.Arrays;
5859
import java.util.Collections;
5960
import java.util.HashMap;
60-
import java.util.LinkedHashMap;
6161
import java.util.List;
6262
import java.util.Locale;
6363
import java.util.Map;
@@ -969,7 +969,7 @@ private void indexSampleData(List<ZonedDateTime> dataset, RandomIndexWriter inde
969969
}
970970

971971
private Map<String, Integer> bucketCountsAsMap(InternalAutoDateHistogram result) {
972-
LinkedHashMap<String, Integer> map = new LinkedHashMap<>(result.getBuckets().size());
972+
Map<String, Integer> map = Maps.newLinkedHashMapWithExpectedSize(result.getBuckets().size());
973973
result.getBuckets().stream().forEach(b -> {
974974
Object old = map.put(b.getKeyAsString(), Math.toIntExact(b.getDocCount()));
975975
assertNull(old);
@@ -978,7 +978,7 @@ private Map<String, Integer> bucketCountsAsMap(InternalAutoDateHistogram result)
978978
}
979979

980980
private Map<String, Double> maxAsMap(InternalAutoDateHistogram result) {
981-
LinkedHashMap<String, Double> map = new LinkedHashMap<>(result.getBuckets().size());
981+
Map<String, Double> map = Maps.newLinkedHashMapWithExpectedSize(result.getBuckets().size());
982982
result.getBuckets().stream().forEach(b -> {
983983
InternalMax max = b.getAggregations().get("max");
984984
Object old = map.put(b.getKeyAsString(), max.getValue());

x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/stats/Metrics.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package org.elasticsearch.xpack.eql.stats;
99

1010
import org.elasticsearch.common.metrics.CounterMetric;
11+
import org.elasticsearch.common.util.Maps;
1112
import org.elasticsearch.xpack.core.watcher.common.stats.Counters;
1213

1314
import java.util.Collections;
@@ -40,7 +41,7 @@ public String toString() {
4041
public Metrics() {
4142
Map<QueryMetric, Map<OperationType, CounterMetric>> qMap = new LinkedHashMap<>();
4243
for (QueryMetric metric : QueryMetric.values()) {
43-
Map<OperationType, CounterMetric> metricsMap = new LinkedHashMap<>(OperationType.values().length);
44+
Map<OperationType, CounterMetric> metricsMap = Maps.newLinkedHashMapWithExpectedSize(OperationType.values().length);
4445
for (OperationType type : OperationType.values()) {
4546
metricsMap.put(type, new CounterMetric());
4647
}
@@ -49,7 +50,7 @@ public Metrics() {
4950
}
5051
opsByTypeMetrics = Collections.unmodifiableMap(qMap);
5152

52-
Map<FeatureMetric, CounterMetric> fMap = new LinkedHashMap<>(FeatureMetric.values().length);
53+
Map<FeatureMetric, CounterMetric> fMap = Maps.newLinkedHashMapWithExpectedSize(FeatureMetric.values().length);
5354
for (FeatureMetric featureMetric : FeatureMetric.values()) {
5455
fMap.put(featureMetric, new CounterMetric());
5556
}

0 commit comments

Comments
 (0)