Skip to content

Conversation

@liach
Copy link
Member

@liach liach commented Nov 10, 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

Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8371319: java.lang.reflect.Method#equals doesn't short-circuit with same instances (Enhancement - P4)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/28221/head:pull/28221
$ git checkout pull/28221

Update a local copy of the PR:
$ git checkout pull/28221
$ git pull https://git.openjdk.org/jdk.git pull/28221/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 28221

View PR using the GUI difftool:
$ git pr show -t 28221

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/28221.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Nov 10, 2025

👋 Welcome back liach! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Nov 10, 2025

@liach This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8371319: java.lang.reflect.Method#equals doesn't short-circuit with same instances

Reviewed-by: jvernee

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 7 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot changed the title 8371319 8371319: java.lang.reflect.Method#equals doesn't short-circuit with same instances Nov 10, 2025
@openjdk openjdk bot added the core-libs core-libs-dev@openjdk.org label Nov 10, 2025
@openjdk
Copy link

openjdk bot commented Nov 10, 2025

@liach The following label will be automatically applied to this pull request:

  • core-libs

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the rfr Pull request is ready for review label Nov 10, 2025
@mlbridge
Copy link

mlbridge bot commented Nov 10, 2025

Webrevs

@forax
Copy link
Member

forax commented Nov 10, 2025

Hello Chen,
Beware !

You may have also to test with ZGC (during the relocation phase),
because == may not be as fast as a pointer check.

@liach
Copy link
Member Author

liach commented Nov 10, 2025

Remi, are you talking about my optimization code or the == case in my benchmark?
I think the Method/Constructor equals implementations are already == heavy; it might not hurt that much with one more == before the array content comparisons.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Nov 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core-libs core-libs-dev@openjdk.org ready Pull request is ready to be integrated rfr Pull request is ready for review

Development

Successfully merging this pull request may close these issues.

3 participants