RFR: 8371319: java.lang.reflect.Method#equals doesn't short-circuit with same instances
Chen Liang
liach at openjdk.org
Fri Nov 14 22:58:23 UTC 2025
On Mon, 10 Nov 2025 17:59:02 GMT, Chen Liang <liach at openjdk.org> wrote:
> There is currently no shortcut for fast equals for Method or Constructor. While the comparison for declaring class, name (interned strings' identity), and return type are very efficient with identity comparison, the comparison for parameter type is not so much: We always have to iterate through every parameter.
>
> Luckily, even though we are burdened with the different method and constructor instance due to setAccessible, their parameter type arrays are shared most of the time; in particular, in the same root method/constructor hierarchy, all instances share the same method object. Thus, we can perform a `==` check on the incoming array to provide a fast path.
>
> Benchmark numbers before and after:
>
> Benchmark Mode Cnt Score Error Units
> ExecutableCompareBenchmark.distinctParams avgt 5 1.189 ± 0.024 ns/op
> ExecutableCompareBenchmark.equalMethods avgt 5 2.449 ± 0.033 ns/op
> ExecutableCompareBenchmark.sameMethodObject avgt 5 0.541 ± 0.027 ns/op
>
> Benchmark Mode Cnt Score Error Units
> ExecutableCompareBenchmark.distinctParams avgt 5 1.186 ± 0.042 ns/op
> ExecutableCompareBenchmark.equalMethods avgt 5 1.078 ± 0.049 ns/op
> ExecutableCompareBenchmark.sameMethodObject avgt 5 0.395 ± 0.018 ns/op
Thanks for the reviews! Tier 1-3 passes.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/28221#issuecomment-3534972806
More information about the core-libs-dev
mailing list