[lworld] RFR: 8286824: [valhalla] WeakHashMap for value classes

Mandy Chung mchung at openjdk.org
Thu Jul 7 23:01:18 UTC 2022


On Thu, 30 Jun 2022 14:03:31 GMT, Roger Riggs <rriggs 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 46:

> 44:  * mapping for a given key will not prevent the key from being discarded by the
> 45:  * garbage collector, that is, made finalizable, finalized, and then reclaimed.
> 46:  * For keys that are {@linkplain Class#isValue() Value objects}, the retention of the

should this be "value objects" rather than "Value objects"?  This comment applies to all occurrences.

src/java.base/share/classes/java/util/WeakHashMap.java line 81:

> 79:  * method.
> 80:  *
> 81:  * <p> <i>Update needed for Value Objects:

can you file a JBS issue to follow up this?

src/java.base/share/classes/java/util/WeakHashMap.java line 231:

> 229:      * The default {@code ValuePolicy} is {@link ValuePolicy#SOFT} unless
> 230:      * the system property {@systemProperty java.util.WeakHashMap.valueKeyRetention}
> 231:      * is equal to the name of a {@link ValuePolicy} enum.

Is the value of the system property case-sensitive?  should specify.

src/java.base/share/classes/java/util/WeakHashMap.java line 244:

> 242:     /**
> 243:      * Constructs a new, empty {@code WeakHashMap} with the given initial
> 244:      * capacity and the given load factor.

and the given value policy.

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.

src/java.base/share/classes/java/util/WeakHashMap.java line 517:

> 515:      *         previously associated {@code null} with {@code key}.)
> 516:      * @throws UnsupportedOperationException if {@code key} is a Value object
> 517:      *         and the {@link #valuePolicy() valuePolicy} is {@link ValuePolicy#THROW}.

`putAll` and `putIfAbsent` would need spec update.

src/java.base/share/classes/java/util/WeakHashMap.java line 867:

> 865:     /**
> 866:      * A SoftEntry is used for value class keys in which the entries are retained
> 867:      * until there is some memory pressure.  A anchor object is used as the referent

s/a anchor/an anchor/

src/java.base/share/classes/java/util/WeakHashMap.java line 875:

> 873:      */
> 874:     private static class SoftEntry<K, V> extends Entry<K, V> {
> 875:         Object realKey;

can make it a final field.

src/java.base/share/classes/java/util/WeakHashMap.java line 920:

> 918:      */
> 919:     private static class StrongEntry<K, V> extends Entry<K, V> {
> 920:         Object realKey;

can be a final field

src/java.base/share/classes/java/util/WeakHashMap.java line 1534:

> 1532:     /**
> 1533:      * Enum for the ValuePolicy; when putting a key and value into a WeakHashMap
> 1534:      * determines how keys that are Value objects are retained (or not).

`@since valhalla`

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

PR: https://git.openjdk.org/valhalla/pull/718



More information about the valhalla-dev mailing list