Skip to content

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Conversation

idegtiarenko
Copy link
Contributor

This change simplifies pre-sizing the map.

@idegtiarenko idegtiarenko added >non-issue :Core/Infra/Core Core issues without another label Team:Core/Infra Meta label for core/infra team v9.1.0 labels Apr 14, 2025
@idegtiarenko idegtiarenko requested a review from a team as a code owner April 14, 2025 09:07
@idegtiarenko idegtiarenko requested review from rjernst and prdoyle April 14, 2025 09:07
@elasticsearchmachine
Copy link
Collaborator

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);
Copy link
Contributor Author

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.

Copy link
Member

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);
Copy link
Contributor Author

@idegtiarenko idegtiarenko Apr 14, 2025

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);

Copy link
Member

@original-brownbear original-brownbear left a 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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happened here?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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 <--

Copy link
Member

@rjernst rjernst left a 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);
Copy link
Member

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);
Copy link
Member

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.

Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Core/Infra/Core Core issues without another label >non-issue Team:Core/Infra Meta label for core/infra team v9.1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants