From 27f4cc3cf724ad6f317eb732026584c427da80c6 Mon Sep 17 00:00:00 2001 From: "ievgen.degtiarenko" Date: Wed, 16 Feb 2022 17:29:48 +0100 Subject: [PATCH 1/3] update `readMap` to avoid resizing map during reading This commit updates readMap to use helper method that presizes map correctly. It guarantees no resizing when adding given numbers of entries to a hashmap. --- .../java/org/elasticsearch/common/io/stream/StreamInput.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java b/server/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java index 2b757c6781b2d..a1e0fa6eef517 100644 --- a/server/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java +++ b/server/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java @@ -607,7 +607,7 @@ public String[] readOptionalStringArray() throws IOException { * If the returned map contains any entries it will be mutable. If it is empty it might be immutable. */ public Map readMap(Writeable.Reader keyReader, Writeable.Reader valueReader) throws IOException { - return readMap(keyReader, valueReader, HashMap::new); + return readMap(keyReader, valueReader, Maps::newMapWithExpectedSize); } public Map readOrderedMap(Writeable.Reader keyReader, Writeable.Reader valueReader) throws IOException { From 7bc45b783f9b10da2110373dea40b88be002a9f8 Mon Sep 17 00:00:00 2001 From: "ievgen.degtiarenko" Date: Wed, 16 Feb 2022 17:55:14 +0100 Subject: [PATCH 2/3] upd --- .../org/elasticsearch/common/io/stream/StreamInput.java | 8 +++----- .../src/main/java/org/elasticsearch/common/util/Maps.java | 8 ++++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java b/server/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java index a1e0fa6eef517..2daf1a4527b0b 100644 --- a/server/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java +++ b/server/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java @@ -60,9 +60,7 @@ import java.util.Collections; import java.util.Date; import java.util.EnumSet; -import java.util.HashMap; import java.util.HashSet; -import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Locale; @@ -607,11 +605,11 @@ public String[] readOptionalStringArray() throws IOException { * If the returned map contains any entries it will be mutable. If it is empty it might be immutable. */ public Map readMap(Writeable.Reader keyReader, Writeable.Reader valueReader) throws IOException { - return readMap(keyReader, valueReader, Maps::newMapWithExpectedSize); + return readMap(keyReader, valueReader, Maps::newHashMapWithExpectedSize); } public Map readOrderedMap(Writeable.Reader keyReader, Writeable.Reader valueReader) throws IOException { - return readMap(keyReader, valueReader, LinkedHashMap::new); + return readMap(keyReader, valueReader, Maps::newLinkedHashMapWithExpectedSize); } private Map readMap(Writeable.Reader keyReader, Writeable.Reader valueReader, IntFunction> constructor) @@ -784,7 +782,7 @@ private Map readLinkedHashMap() throws IOException { if (size9 == 0) { return Collections.emptyMap(); } - Map map9 = new LinkedHashMap<>(size9); + Map map9 = Maps.newLinkedHashMapWithExpectedSize(size9); for (int i = 0; i < size9; i++) { map9.put(readString(), readGenericValue()); } diff --git a/server/src/main/java/org/elasticsearch/common/util/Maps.java b/server/src/main/java/org/elasticsearch/common/util/Maps.java index 72fd2ead73ecb..052b6d08c72f8 100644 --- a/server/src/main/java/org/elasticsearch/common/util/Maps.java +++ b/server/src/main/java/org/elasticsearch/common/util/Maps.java @@ -252,9 +252,17 @@ private static Map flatten(List list, boolean ordered, S * @return a new pre-sized {@link HashMap} */ public static Map newMapWithExpectedSize(int expectedSize) { + return newHashMapWithExpectedSize(expectedSize); + } + + public static Map newHashMapWithExpectedSize(int expectedSize) { return new HashMap<>(capacity(expectedSize)); } + public static Map newLinkedHashMapWithExpectedSize(int expectedSize) { + return new LinkedHashMap<>(capacity(expectedSize)); + } + static int capacity(int expectedSize) { assert expectedSize >= 0; return expectedSize < 2 ? expectedSize + 1 : (int) (expectedSize / 0.75 + 1.0); From 2ab574a044f6141d2167e01a7efdec1304de679b Mon Sep 17 00:00:00 2001 From: Ievgen Degtiarenko Date: Mon, 21 Feb 2022 08:07:21 +0100 Subject: [PATCH 3/3] Update docs/changelog/84045.yaml --- docs/changelog/84045.yaml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 docs/changelog/84045.yaml diff --git a/docs/changelog/84045.yaml b/docs/changelog/84045.yaml new file mode 100644 index 0000000000000..b45380f264d3f --- /dev/null +++ b/docs/changelog/84045.yaml @@ -0,0 +1,5 @@ +pr: 84045 +summary: Update `readMap` to avoid resizing map during reading +area: Infra/Core +type: enhancement +issues: []