RFR: 8371319: java.lang.reflect.Method#equals doesn't short-circuit with same instances
Chen Liang
liach at openjdk.org
Mon Nov 10 18:06:03 UTC 2025
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
-------------
Commit messages:
- Short-circuiting for equal methods
Changes: https://git.openjdk.org/jdk/pull/28221/files
Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=28221&range=00
Issue: https://bugs.openjdk.org/browse/JDK-8371319
Stats: 88 lines in 2 files changed: 88 ins; 0 del; 0 mod
Patch: https://git.openjdk.org/jdk/pull/28221.diff
Fetch: git fetch https://git.openjdk.org/jdk.git pull/28221/head:pull/28221
PR: https://git.openjdk.org/jdk/pull/28221
More information about the core-libs-dev
mailing list