StefanRRichter commented on code in PR #22788:
URL: https://github.com/apache/flink/pull/22788#discussion_r1235425320


##########
flink-core/src/main/java/org/apache/flink/util/CollectionUtil.java:
##########
@@ -133,4 +140,71 @@ public static <K, V> Map<K, V> map(Map.Entry<K, V>... 
entries) {
         }
         return Collections.unmodifiableMap(map);
     }
+
+    /**
+     * Creates a new {@link HashMap} of the expected size, i.e. a hash map 
that will not rehash if
+     * expectedSize many keys are inserted, considering the load factor.
+     *
+     * @param expectedSize the expected size of the created hash map.
+     * @return a new hash map instance with enough capacity for the expected 
size.
+     * @param <K> the type of keys maintained by this map.
+     * @param <V> the type of mapped values.
+     */
+    public static <K, V> HashMap<K, V> newHashMapWithExpectedSize(int 
expectedSize) {
+        return new HashMap<>(computeRequiredCapacity(expectedSize), 
HASH_MAP_DEFAULT_LOAD_FACTOR);
+    }
+
+    /**
+     * Creates a new {@link LinkedHashMap} of the expected size, i.e. a hash 
map that will not
+     * rehash if expectedSize many keys are inserted, considering the load 
factor.
+     *
+     * @param expectedSize the expected size of the created hash map.
+     * @return a new hash map instance with enough capacity for the expected 
size.
+     * @param <K> the type of keys maintained by this map.
+     * @param <V> the type of mapped values.
+     */
+    public static <K, V> LinkedHashMap<K, V> 
newLinkedHashMapWithExpectedSize(int expectedSize) {
+        return new LinkedHashMap<>(
+                computeRequiredCapacity(expectedSize), 
HASH_MAP_DEFAULT_LOAD_FACTOR);

Review Comment:
   Yes, that was the plan because every place that initializes a map with the 
end size to avoid rehashing is actually causing a guaranteed rehash to happen 
today. But I'd do that in a separate refactoring PR.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to