[lworld] RFR: 8286824: [valhalla] WeakHashMap for value classes
Roger Riggs
rriggs at openjdk.org
Fri Jul 8 16:10:01 UTC 2022
On Thu, 7 Jul 2022 22:41:55 GMT, Mandy Chung <mchung at openjdk.org> wrote:
>> WeakHashMap uses WeakReference for keys, allowing it to retain only keys and values that are strongly referenced elsewhere.
>>
>> The behavior of WeakReference (or any Reference class) is not defined for value objects.
>>
>> Four (4) behaviors are proposed for WeakHashMap to give the developer a choice:
>> - SOFT - Keys that are value objects are retained until memory pressure clears soft references; then the keys that are value objects are removed automatically
>> - STRONG - Keys are value objects are retained until they are explicitly removed
>> - THROW - Keys that are value objects can not be put into the map; UnsupportedOperationException is thrown
>> - DISCARD - Keys that are value objects are silently discarded; as if they were removed immediately without being visible
>>
>> The default is SOFT - key/values are removed when there is memory pressure.
>> Entries will be removed automatically at some point, though less deterministically than with WeakReferences.
>
> src/java.base/share/classes/java/util/WeakHashMap.java line 515:
>
>> 513: * {@code null} if there was no mapping for {@code key}.
>> 514: * (A {@code null} return can also indicate that the map
>> 515: * previously associated {@code null} with {@code key}.)
>
> Need to specify: A null return can also indicate the entry is discarded if the value policy is DISCARD and the key is a value object.
A subtle point... The WeakHashmap *may* contain an identity class instance that compares as `equals()` with the value object. In that case, the instance would be returned from `getEntry`.
Similarly, `put()` would return the previous entry (that was `equals`) to a value entry that is discarded.
This will need some more spec detail.
-------------
PR: https://git.openjdk.org/valhalla/pull/718
More information about the valhalla-dev
mailing list