Skip to content

Commit 7729714

Browse files
committed
Simplify map initial sizing
1 parent 411a946 commit 7729714

File tree

2 files changed

+3
-25
lines changed

2 files changed

+3
-25
lines changed

server/src/main/java/org/elasticsearch/common/util/Maps.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public static <K, V> Map<K, V> newMapWithExpectedSize(int expectedSize) {
280280
* @return a new pre-sized {@link HashMap}
281281
*/
282282
public static <K, V> Map<K, V> newHashMapWithExpectedSize(int expectedSize) {
283-
return new HashMap<>(capacity(expectedSize));
283+
return HashMap.newHashMap(expectedSize);
284284
}
285285

286286
/**
@@ -292,7 +292,7 @@ public static <K, V> Map<K, V> newHashMapWithExpectedSize(int expectedSize) {
292292
* @return a new pre-sized {@link HashMap}
293293
*/
294294
public static <K, V> Map<K, V> newConcurrentHashMapWithExpectedSize(int expectedSize) {
295-
return new ConcurrentHashMap<>(capacity(expectedSize));
295+
return new ConcurrentHashMap<>(expectedSize);
296296
}
297297

298298
/**
@@ -304,12 +304,7 @@ public static <K, V> Map<K, V> newConcurrentHashMapWithExpectedSize(int expected
304304
* @return a new pre-sized {@link LinkedHashMap}
305305
*/
306306
public static <K, V> LinkedHashMap<K, V> newLinkedHashMapWithExpectedSize(int expectedSize) {
307-
return new LinkedHashMap<>(capacity(expectedSize));
308-
}
309-
310-
static int capacity(int expectedSize) {
311-
assert expectedSize >= 0;
312-
return expectedSize < 2 ? expectedSize + 1 : (int) (expectedSize / 0.75 + 1.0);
307+
return LinkedHashMap.newLinkedHashMap(expectedSize);
313308
}
314309

315310
/**

server/src/test/java/org/elasticsearch/common/util/MapsTests.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import static org.hamcrest.Matchers.equalTo;
3333
import static org.hamcrest.Matchers.greaterThan;
3434
import static org.hamcrest.Matchers.hasItem;
35-
import static org.hamcrest.Matchers.lessThanOrEqualTo;
3635

3736
public class MapsTests extends ESTestCase {
3837

@@ -270,22 +269,6 @@ public void testFlatten() {
270269
}
271270
}
272271

273-
public void testCapacityIsEnoughForMapToNotBeResized() {
274-
for (int i = 0; i < 1000; i++) {
275-
int size = randomIntBetween(0, 1_000_000);
276-
int capacity = Maps.capacity(size);
277-
assertThat(size, lessThanOrEqualTo((int) (capacity * 0.75f)));
278-
}
279-
}
280-
281-
public void testCapacityForMaxSize() {
282-
assertEquals(Integer.MAX_VALUE, Maps.capacity(Integer.MAX_VALUE));
283-
}
284-
285-
public void testCapacityForZeroSize() {
286-
assertEquals(1, Maps.capacity(0));
287-
}
288-
289272
@SuppressWarnings("unchecked")
290273
private static Object deepGet(String path, Object obj) {
291274
Object cur = obj;

0 commit comments

Comments
 (0)