Skip to content

8300237: Minor improvements in MethodHandles #12025

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from

Conversation

stsypanov
Copy link
Contributor

@stsypanov stsypanov commented Jan 17, 2023

  • MethodType.ptypes() can be used instead of MethodType.parameterList() when we don't need a copy
  • comparison of two lists can be done without Stream.reduce()

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

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 12025

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

Using diff file

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

@bridgekeeper
Copy link

bridgekeeper bot commented Jan 17, 2023

👋 Welcome back stsypanov! 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 openjdk bot added the rfr Pull request is ready for review label Jan 17, 2023
@openjdk
Copy link

openjdk bot commented Jan 17, 2023

@stsypanov 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 core-libs core-libs-dev@openjdk.org label Jan 17, 2023
@mlbridge
Copy link

mlbridge bot commented Jan 17, 2023

Webrevs

final List<Class<?>> empty = List.of();
return lists.stream().reduce((p, q) -> p.size() >= q.size() ? p : q).orElse(empty);
map(MethodType::ptypes).
reduce((p, q) -> p.length >= q.length ? p : q).orElse(EMPTY);
Copy link
Member

@cl4es cl4es Jan 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could avoid the need to introduce EMPTY if you make the stream expression return the longest list directly:

                reduce((p, q) -> {
                    var longest = (p.size() >= q.size()) ? p : q;
                    return List.of(Arrays.copyOfRange(longest, skipSize, longest.size()); // checking isEmpty() is redundant here since we filter on `t.parameterCount() > skipSize`
                }).orElse(List.of());

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using lambdas inside MethodHandles is quite dangerous given that lambdas are initialized using method handles. It may work now because longuestParameterList() is not called when initializing a lambda but it may make any changes in the implementation of lambdas painfull in the future.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Precious little method handle use in lambda bootstrap since JDK 11. Though I agree with the sentiment - having fixed a number of bootstrap issues in the past - MethodHandles is a small step up the abstraction ladder and the code in particular already uses a number of method refs and lambdas.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, two small changes,

  • formatting: usually the method call in a stream are aligned with the '.' at the beginning

     stream
       .filter(...)
      .map(...)
    

    instead of at the end.

  • the reduce is a max(),
    max(Comparator.comparingInt(List::size))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@forax formatting is fixed. As of max() I think we can improve this even more by hoisting max() before calling ptypes()

Copy link
Member

@cl4es cl4es left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove EMPTY (using my earlier suggestion or by simply inlining new Class<?>[0]) and I think this looks OK if framed as a code cleanup. The affected code isn't very performance sensitive as it's only used in the setup for MHs.loop combinators, which are likely to be rare in practice.

@stsypanov stsypanov requested review from forax and cl4es and removed request for forax and cl4es January 17, 2023 14:53
@stsypanov stsypanov requested review from cl4es and removed request for forax January 17, 2023 18:08
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.*;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted

.filter(t -> t.parameterCount() > skipSize)
.max(Comparator.comparing(MethodType::parameterCount))
.map(MethodType::ptypes)
.map(longest -> List.of(Arrays.copyOfRange(longest, skipSize, longest.length)))
Copy link
Member

@forax forax Jan 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think you can fuse these two map() calls

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Copy link
Member

@cl4es cl4es left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

return lists.stream().reduce((p, q) -> p.size() >= q.size() ? p : q).orElse(empty);
.map(MethodHandle::type)
.filter(t -> t.parameterCount() > skipSize)
.max(Comparator.comparing(MethodType::parameterCount))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@forax suggested Comparator.comparingInt here, which may or may not help avoid some boxing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice point, done!

@openjdk
Copy link

openjdk bot commented Jan 17, 2023

@stsypanov 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:

8300237: Minor improvements in MethodHandles

Reviewed-by: redestad

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 214 new commits pushed to the master branch:

  • 15a9186: 8300169: Build failure with clang-15
  • d918042: 8300267: Remove duplicated setter/getter for do_not_unlock
  • 7c8b99e: 8300117: Replace use of JNI_COMMIT mode with mode 0
  • c705673: 8299089: Instrument global jni handles with tag to make them distinguishable
  • 66f7387: 8299074: nmethod marked for deoptimization is not deoptimized
  • 1f438a8: 8282651: ZGC: vmTestbase/gc/ArrayJuggle/ tests fails intermittently with exit code 97
  • f1194dc: 8300109: RISC-V: Improve code generation for MinI/MaxI nodes
  • 89a032d: 8300002: Performance regression caused by non-inlined hot methods due to post call noop instructions
  • 7071397: 8299224: TestReporterStreams.java has bad indentation for legal header
  • 1d8b87d: 8300321: Use link tags in javax.sql.rowset package-info
  • ... and 204 more: https://git.openjdk.org/jdk/compare/41900b57af084e0cfc1453681b24fe5606e11ab2...master

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.

As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@cl4es) but any other Committer may sponsor as well.

➡️ To flag this PR as ready for integration with the above commit message, type /integrate in a new comment. (Afterwards, your sponsor types /sponsor in a new comment to perform the integration).

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jan 17, 2023
@forax
Copy link
Member

forax commented Jan 18, 2023

Looks good to me,
i'm not an official reviewer but claes already review this PR so this is fine.

@stsypanov
Copy link
Contributor Author

/integrate

@openjdk openjdk bot added the sponsor Pull request is ready to be sponsored label Jan 18, 2023
@openjdk
Copy link

openjdk bot commented Jan 18, 2023

@stsypanov
Your change (at version 8566289) is now ready to be sponsored by a Committer.

@cl4es
Copy link
Member

cl4es commented Jan 18, 2023

/sponsor

@openjdk
Copy link

openjdk bot commented Jan 18, 2023

Going to push as commit 754f6e6.
Since your change was applied there have been 218 commits pushed to the master branch:

  • 85d8bac: 8300166: Unused array allocation in ProcessPath.doProcessPath
  • c464ef1: 8292741: Convert JvmtiTagMapTable to ResourceHashtable
  • 1aded82: 8300488: Incorrect usage of CATCH_BAD_ALLOC as a macro call
  • bd5ca95: 8300222: Replace NULL with nullptr in share/logging
  • 15a9186: 8300169: Build failure with clang-15
  • d918042: 8300267: Remove duplicated setter/getter for do_not_unlock
  • 7c8b99e: 8300117: Replace use of JNI_COMMIT mode with mode 0
  • c705673: 8299089: Instrument global jni handles with tag to make them distinguishable
  • 66f7387: 8299074: nmethod marked for deoptimization is not deoptimized
  • 1f438a8: 8282651: ZGC: vmTestbase/gc/ArrayJuggle/ tests fails intermittently with exit code 97
  • ... and 208 more: https://git.openjdk.org/jdk/compare/41900b57af084e0cfc1453681b24fe5606e11ab2...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Jan 18, 2023
@openjdk openjdk bot closed this Jan 18, 2023
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review sponsor Pull request is ready to be sponsored labels Jan 18, 2023
@openjdk
Copy link

openjdk bot commented Jan 18, 2023

@cl4es @stsypanov Pushed as commit 754f6e6.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@stsypanov stsypanov deleted the mh branch January 18, 2023 13:56
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 integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

3 participants