-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Simplify map initial sizing #126767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Simplify map initial sizing #126767
Conversation
Pinging @elastic/es-core-infra (Team:Core/Infra) |
@@ -280,7 +280,7 @@ public static <K, V> Map<K, V> newMapWithExpectedSize(int expectedSize) { | |||
* @return a new pre-sized {@link HashMap} | |||
*/ | |||
public static <K, V> Map<K, V> newHashMapWithExpectedSize(int expectedSize) { | |||
return new HashMap<>(capacity(expectedSize)); | |||
return HashMap.newHashMap(expectedSize); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since 19 both HashMap and LinkedHashMap could be presized using standard lib factories.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: perhaps then (doesn't have to be here) we should convert all the uses of this helper to use HashMap.newHashMap directly?
@@ -292,7 +292,7 @@ public static <K, V> Map<K, V> newHashMapWithExpectedSize(int expectedSize) { | |||
* @return a new pre-sized {@link HashMap} | |||
*/ | |||
public static <K, V> Map<K, V> newConcurrentHashMapWithExpectedSize(int expectedSize) { | |||
return new ConcurrentHashMap<>(capacity(expectedSize)); | |||
return new ConcurrentHashMap<>(expectedSize); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ConcurrentHashMap does this sizing computation internally:
according to the javadoc: Params: initialCapacity – The implementation performs internal sizing to accommodate this many elements.
and constructor code:
long size = (long)(1.0 + (long)initialCapacity / loadFactor);
int cap = (size >= (long)MAXIMUM_CAPACITY) ?
MAXIMUM_CAPACITY : tableSizeFor((int)size);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
@@ -293,25 +293,25 @@ public void testToXContentWithMultipleProjects() throws IOException { | |||
}, | |||
"projects": [ | |||
{ | |||
"id": "tb5W0bx765nDVIwqJPw92G", | |||
"id": "3LftaL7hgfXAsF60Gm6jcD", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happened here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like our calculation creates a bigger maps in some cases. This results in a different iteration order in several tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expected size=0 old_capacity=1 new_capacity=0 <--
expected size=1 old_capacity=2 new_capacity=2
expected size=2 old_capacity=3 new_capacity=3
expected size=3 old_capacity=5 new_capacity=4 <--
expected size=4 old_capacity=6 new_capacity=6
expected size=5 old_capacity=7 new_capacity=7
expected size=6 old_capacity=9 new_capacity=8 <--
expected size=7 old_capacity=10 new_capacity=10
expected size=8 old_capacity=11 new_capacity=11
expected size=9 old_capacity=13 new_capacity=12 <--
expected size=10 old_capacity=14 new_capacity=14
expected size=11 old_capacity=15 new_capacity=15
expected size=12 old_capacity=17 new_capacity=16 <--
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All 3 of these cases seem to suggest we don't need the utility methods anymore?
@@ -280,7 +280,7 @@ public static <K, V> Map<K, V> newMapWithExpectedSize(int expectedSize) { | |||
* @return a new pre-sized {@link HashMap} | |||
*/ | |||
public static <K, V> Map<K, V> newHashMapWithExpectedSize(int expectedSize) { | |||
return new HashMap<>(capacity(expectedSize)); | |||
return HashMap.newHashMap(expectedSize); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: perhaps then (doesn't have to be here) we should convert all the uses of this helper to use HashMap.newHashMap directly?
@@ -292,7 +292,7 @@ public static <K, V> Map<K, V> newHashMapWithExpectedSize(int expectedSize) { | |||
* @return a new pre-sized {@link HashMap} | |||
*/ | |||
public static <K, V> Map<K, V> newConcurrentHashMapWithExpectedSize(int expectedSize) { | |||
return new ConcurrentHashMap<>(capacity(expectedSize)); | |||
return new ConcurrentHashMap<>(expectedSize); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to the above note, this utility method doesn't seem to be doing anything anymore, so perhaps it should be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(here and above comment) Yeap, newMapWithExpectedSize, newHashMapWithExpectedSize, newConcurrentHashMapWithExpectedSize, newLinkedHashMapWithExpectedSize can all be inlined.
This change simplifies pre-sizing the map.