RFR: 8256167: Convert JDK use of `Reference::get` to `Reference::refersTo`

Peter Levart plevart at openjdk.java.net
Fri Dec 4 20:38:11 UTC 2020


On Fri, 4 Dec 2020 20:16:14 GMT, Mandy Chung <mchung at openjdk.org> wrote:

>> src/java.management/share/classes/com/sun/jmx/mbeanserver/WeakIdentityHashMap.java line 127:
>> 
>>> 125:             T got = get();
>>> 126:             return got != null && wr.refersTo(got);
>>> 127:         }
>> 
>> Here you could pre-screen the get() with:
>> 
>> if (this.refersTo(null) != wr.refersTo(null)) return false;
>> 
>> In case one of the two WeakReference(s) is cleared and the other is not, they are not equal and you don't call this.get() in such case.
>
> `expunge` is called at `get`, `put`, and `remove`.   While there is a chance that a referent might be cleared,  I will leave this as is and this patch simply replaces to use `refersTo`.

You're right. Where it would matter is in expunge() where the Map keys that get polled off the reference queue are all already cleared and when HashMap.remove(key) is called with such cleared key, it is this cleared key that is the target of key.equals(other) method invocation (and not vice versa), so it doesn't matter if .get() is called on such key as it is already cleared.

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

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


More information about the core-libs-dev mailing list