On Thu, 6 Jul 2023 13:27:15 GMT, Roger Riggs <rri...@openjdk.org> wrote:

>> `WeakReferenceKey::equals` and `SoftReferenceKey::equals`:
>> 
>> Instead of `return Objects.equals(get(), obj);`, suggest to do:
>> 
>> 
>>     return refersTo(obj);
>
>> @mlchung Not sure I catch the nuance. `refersTo` doesn't work if the search 
>> key is constructed. For example, if the key reference is a `record Pair(int 
>> a, int b) {}`, then `map.containsKey(new Pair(10, 20))` would locate with 
>> equals but not with `refersTo`.
> 
> Using `get()` on a reference can momentarily create a strong reference to the 
> item. `refersTo()` does not.
> But since your map is holding proxies to the object,  `refersTo()` would be 
> to the proxy, not the object; so that's not an option.

> @RogerRiggs `WeakReferenceKey` is a `WeakReference` with the key as the 
> referent. I might be missing something?

The comparison of the unwrapped key using `refersTo` is an identity comparison; 
but the usual comparison for Maps uses `equals`.  Using `refersTo` would limit 
the use of ReferencedKeyMap to cases where the original key requires identity 
comparision. (I missed Jim's earlier comment that pointed this out.)

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

PR Comment: https://git.openjdk.org/jdk/pull/14684#issuecomment-1624135067

Reply via email to