RFR: 8281631: HashMap copy constructor and putAll can over-allocate table [v28]

Stuart Marks smarks at openjdk.java.net
Thu Mar 10 22:25:46 UTC 2022


On Thu, 10 Mar 2022 16:22:29 GMT, XenoAmess <duke at openjdk.java.net> wrote:

>> test/jdk/java/util/HashMap/WhiteBoxResizeTest.java line 116:
>> 
>>> 114:     }
>>> 115: 
>>> 116:     void putN(Map<Integer, Integer> map, int n) {
>> 
>> @stuart-marks well we know this is correct for WeakHashMap when n < IntegerCache.high because we have Integer constant pool, but I think it be better to manlly make it sure it is referenced, as IntegerCache.high is configurable.
>
> @stuart-marks please have a look in changes in the latest commit, I think we'd better to manually create references for keys like that.

Good point about WeakHashMap! I don't think we need a separate table. Since the value is held by a strong reference, using the same reference as the key will prevent the key's weak reference from being cleared. I'd suggest this:

    for (int i = 0; i < n; i++) {
        Integer obj = i; // prevent WeakHashMap from clearing keys
        map.put(obj, obj);
    }

-------------

PR: https://git.openjdk.java.net/jdk/pull/7431


More information about the core-libs-dev mailing list