Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions server/src/main/java/org/elasticsearch/common/util/Maps.java
Original file line number Diff line number Diff line change
Expand Up @@ -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?

}

/**
Expand All @@ -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

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.

}

/**
Expand All @@ -304,12 +304,7 @@ public static <K, V> Map<K, V> newConcurrentHashMapWithExpectedSize(int expected
* @return a new pre-sized {@link LinkedHashMap}
*/
public static <K, V> LinkedHashMap<K, V> 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);
return LinkedHashMap.newLinkedHashMap(expectedSize);
}

/**
Expand Down
17 changes: 0 additions & 17 deletions server/src/test/java/org/elasticsearch/common/util/MapsTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.lessThanOrEqualTo;

public class MapsTests extends ESTestCase {

Expand Down Expand Up @@ -270,22 +269,6 @@ public void testFlatten() {
}
}

public void testCapacityIsEnoughForMapToNotBeResized() {
for (int i = 0; i < 1000; i++) {
int size = randomIntBetween(0, 1_000_000);
int capacity = Maps.capacity(size);
assertThat(size, lessThanOrEqualTo((int) (capacity * 0.75f)));
}
}

public void testCapacityForMaxSize() {
assertEquals(Integer.MAX_VALUE, Maps.capacity(Integer.MAX_VALUE));
}

public void testCapacityForZeroSize() {
assertEquals(1, Maps.capacity(0));
}

@SuppressWarnings("unchecked")
private static Object deepGet(String path, Object obj) {
Object cur = obj;
Expand Down