[lworld] RFR: 8366214: [lworld] Use Objects.equals in HashMap and ConcurrentHashmap

Roger Riggs rriggs at openjdk.org
Thu Aug 28 13:20:04 UTC 2025


On Wed, 27 Aug 2025 22:18:54 GMT, Chen Liang <liach at openjdk.org> wrote:

>> As observed in [JDK-8366043](https://bugs.openjdk.org/browse/JDK-8366043) [lworld] (LIFE = Legacy Idiom For Equality) causes performance regressions.
>> Updating HashMap and ConcurrentHashMap to use `java.util.Objects.equals` will make it easier to measure performance of options that remove or modify the use of `==`
>> 
>> Replace constructs like:
>> 
>> -                        ((k = e.key) == key || (key != null && key.equals(k))))
>> with:
>> +                        Objects.equals(key, k))
>> 
>> 
>> The changes in ConcurrentHashMap are a bit different due to the use of null as a sentinel.
>> 
>> The order of arguments to the .equals methods must remain the same to ensure compatibility.
>
> src/java.base/share/classes/java/util/HashMap.java line 2026:
> 
>> 2024:                 else if (ph < h)
>> 2025:                     p = pr;
>> 2026:                 else if (Objects.equals(k, (pk = p.key)))
> 
> Stylistic: don't need parentheses around `pk = p.key`

Stylistically, it calls attention to the assignment in the middle of an expression.

> src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java line 669:
> 
>> 667:                     (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
>> 668:                     (v = e.getValue()) != null &&
>> 669:                     (Objects.equals(k, key)) &&
> 
> `k.equals(key)` should be sufficient here.

Maintaining consistency with continuation of the return value.

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

PR Review Comment: https://git.openjdk.org/valhalla/pull/1536#discussion_r2307390334
PR Review Comment: https://git.openjdk.org/valhalla/pull/1536#discussion_r2307392520


More information about the valhalla-dev mailing list