RFR: 8281631: HashMap copy constructor and putAll can over-allocate table [v21]
XenoAmess
duke at openjdk.java.net
Sun Feb 20 18:43:46 UTC 2022
On Sun, 20 Feb 2022 18:38:35 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 because I don't like to call m.size() twice.
> the original codes did so: one call at the `public IdentityHashMap(Map<? extends K, ? extends V> map)` , and the second inside of `putAll`.
> well in most Map implementations `size()` seems O1, and returns a single int number field, but it actually defers in some Map implementations.
> But java grammar do not allow me to make a local variable before calling constructor in a constructor.
> In other words, `this()` must be the first line in a constructor.
and adding a private constructor should not cause any trouble I guess?
-------------
PR: https://git.openjdk.java.net/jdk/pull/7431
More information about the core-libs-dev
mailing list