RFR: 8281631: HashMap copy constructor and putAll can over-allocate table [v21]
liach
duke at openjdk.java.net
Sun Feb 20 19:14:53 UTC 2022
On Sun, 20 Feb 2022 18:44:09 GMT, XenoAmess <duke at openjdk.java.net> wrote:
>> src/java.base/share/classes/java/util/IdentityHashMap.java line 281:
>>
>>> 279: * @throws NullPointerException if the specified map is null
>>> 280: */
>>> 281: private IdentityHashMap(Map<? extends K, ? extends V> map, int expectedSize) {
>>
>> Why are you writing a new constructor when you can just change the old call to `this(m.size());`?
>
>> @liach implementations `size()` seems O1, and returns a single int number field, but it actually defers in some Map implementations.
>
> @liach for example, in ConcurrentSkipListMap and ConcurrentHashMap, `size()` function is far complicated than reading a field, thus calling it twice meaninglessly is not a wise choice.
Imo you should just remove the `if (expectedSize == 0)` check than using this somewhat ugly trick to avoid calling `size()` twice (the second call is only used for this relatively useless fast-path, especially for the concurrent collections you refer to)
-------------
PR: https://git.openjdk.java.net/jdk/pull/7431
More information about the core-libs-dev
mailing list