RFR: 8324718: Add a static function to java.util.Objects to simplify object equality checks [v6]

Chen Liang liach at openjdk.org
Sat Jan 27 17:35:29 UTC 2024


On Sat, 27 Jan 2024 07:52:56 GMT, David Alayachew <duke at openjdk.org> wrote:

>> Adding a function to Objects in order to facilitate equality checking and enhance readability. You simply specify the 2 objects that you want to check for equality, and then provide the functions which will be used to provide the values that we will check for equality.
>
> David Alayachew has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Rather than reiterating the precondition, let's explain why the method failed

I strongly disrecommend adding a library method that will become a performance burden. This method is not helpful as it is hard to optimize; I would recommend creating an alternative with this signature:

public static <T> BiPredicate<T, T> equalsBy(List<Function<T, ?>> getters)


If this getters list is constant (like from `List.of`), JIT can actually constant-fold the resulting predicate, making it much faster than your API once inlined.

Your current notation doesn't offer any opportunity for JIT compiler to treat the getters as a constant to perform optimizations.

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

PR Comment: https://git.openjdk.org/jdk/pull/17603#issuecomment-1913268703


More information about the core-libs-dev mailing list