From 874a296a76d8c301bc38399a7158220e367dc8dc Mon Sep 17 00:00:00 2001 From: Christoph Dreis Date: Thu, 22 Sep 2022 11:13:48 +0200 Subject: [PATCH 1/2] Avoid resizing of Maps created by CollectionUtils See gh-29190 --- .../java/org/springframework/util/CollectionUtils.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/CollectionUtils.java b/spring-core/src/main/java/org/springframework/util/CollectionUtils.java index d1e9354abda9..3658223bf773 100644 --- a/spring-core/src/main/java/org/springframework/util/CollectionUtils.java +++ b/spring-core/src/main/java/org/springframework/util/CollectionUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -85,7 +85,8 @@ public static boolean isEmpty(@Nullable Map map) { * @see #newLinkedHashMap(int) */ public static HashMap newHashMap(int expectedSize) { - return new HashMap<>((int) (expectedSize / DEFAULT_LOAD_FACTOR), DEFAULT_LOAD_FACTOR); + int capacity = (int) Math.ceil(expectedSize / (double) DEFAULT_LOAD_FACTOR); + return new HashMap<>(capacity, DEFAULT_LOAD_FACTOR); } /** @@ -102,7 +103,8 @@ public static HashMap newHashMap(int expectedSize) { * @see #newHashMap(int) */ public static LinkedHashMap newLinkedHashMap(int expectedSize) { - return new LinkedHashMap<>((int) (expectedSize / DEFAULT_LOAD_FACTOR), DEFAULT_LOAD_FACTOR); + int capacity = (int) Math.ceil(expectedSize / (double) DEFAULT_LOAD_FACTOR); + return new LinkedHashMap<>(capacity, DEFAULT_LOAD_FACTOR); } /** From 7309fe9f2e18e32f531c8c7bd4549cb863fb2399 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 26 Sep 2022 09:00:10 +0200 Subject: [PATCH 2/2] Polish "Avoid resizing of Maps created by CollectionUtils" See gh-29190 --- .../java/org/springframework/util/CollectionUtils.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/CollectionUtils.java b/spring-core/src/main/java/org/springframework/util/CollectionUtils.java index 3658223bf773..79e9800a9e03 100644 --- a/spring-core/src/main/java/org/springframework/util/CollectionUtils.java +++ b/spring-core/src/main/java/org/springframework/util/CollectionUtils.java @@ -85,8 +85,7 @@ public static boolean isEmpty(@Nullable Map map) { * @see #newLinkedHashMap(int) */ public static HashMap newHashMap(int expectedSize) { - int capacity = (int) Math.ceil(expectedSize / (double) DEFAULT_LOAD_FACTOR); - return new HashMap<>(capacity, DEFAULT_LOAD_FACTOR); + return new HashMap<>(computeMapInitialCapacity(expectedSize), DEFAULT_LOAD_FACTOR); } /** @@ -103,8 +102,11 @@ public static HashMap newHashMap(int expectedSize) { * @see #newHashMap(int) */ public static LinkedHashMap newLinkedHashMap(int expectedSize) { - int capacity = (int) Math.ceil(expectedSize / (double) DEFAULT_LOAD_FACTOR); - return new LinkedHashMap<>(capacity, DEFAULT_LOAD_FACTOR); + return new LinkedHashMap<>(computeMapInitialCapacity(expectedSize), DEFAULT_LOAD_FACTOR); + } + + private static int computeMapInitialCapacity(int expectedSize) { + return (int) Math.ceil(expectedSize / (double) DEFAULT_LOAD_FACTOR); } /**