Skip to content
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

8295302: Do not use ArrayList when LambdaForm has a single ClassData #10706

Conversation

iklam
Copy link
Member

@iklam iklam commented Oct 13, 2022

Please review this small optimization. As shown in the JBS issue, most of the generated LambdaForm classes have a single ClassData, so we can get a small footprint/speed improvement.


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-8295302: Do not use ArrayList when LambdaForm has a single ClassData

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 10706

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

Using diff file

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

@bridgekeeper
Copy link

bridgekeeper bot commented Oct 13, 2022

👋 Welcome back iklam! 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 Oct 13, 2022
@openjdk
Copy link

openjdk bot commented Oct 13, 2022

@iklam 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 Oct 13, 2022
@mlbridge
Copy link

mlbridge bot commented Oct 13, 2022

Webrevs

Class<?> invokerClass = LOOKUP.makeHiddenClassDefiner(className, classFile, Set.of())
.defineClass(true, classDataValues());
Copy link
Contributor

@iwanowww iwanowww Oct 13, 2022

Choose a reason for hiding this comment

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

Why not just put it in classDataValues() instead?

    Object classDataValues() {
        final List<ClassData> cd = classData;
        return switch(cd.size()) {
            case 0 -> null; // flatten for zero case
            case 1 -> cd.get(0).value; // flatten for single value case
            case 2 -> List.of(cd.get(0).value, cd.get(1).value);
            ...

And it also covers zero case for free.

Copy link
Member Author

Choose a reason for hiding this comment

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

I fixed it as you suggested. I also changed the method to private to make sure no one else is using this method.

emitIconstInsn(mv, index++);
mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List",
"get", "(I)Ljava/lang/Object;", true);
if (classData.size() == 1) {
Copy link
Contributor

Choose a reason for hiding this comment

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

if (classData.size() < 2) {

It works for zero case, because checkcast always succeeds on nulls.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually the zero case is already handled here:

    static void clinit(ClassWriter cw, String className, List<ClassData> classData) {
        if (classData.isEmpty())
            return;

No <clinit> method is generated because we have no static field to initialize.

Copy link
Contributor

@iwanowww iwanowww left a comment

Choose a reason for hiding this comment

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

Looks good.

@openjdk
Copy link

openjdk bot commented Oct 14, 2022

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

8295302: Do not use ArrayList when LambdaForm has a single ClassData

Reviewed-by: vlivanov, redestad, mchung

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 no new commits pushed to the master branch. If another commit should be pushed before you perform the /integrate command, your PR will be automatically rebased. If you prefer to avoid any potential 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 added the ready Pull request is ready to be integrated label Oct 14, 2022
final List<ClassData> cd = classData;
return switch(cd.size()) {
case 0 -> List.of();
case 1 -> List.of(cd.get(0).value);
case 0 -> null;
Copy link
Member

Choose a reason for hiding this comment

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

List.of() always returns the same singleton instance and does not cause any object allocation. I prefer to keep the classDataValues() to return List<Object>

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I now see why you have to change the signature because of the single element case. I made my comment too quickly.

A couple of suggestions:
Please add a javadoc for this method to describe what this method returns for different cases. It's better to move this method close to clinit as they are tightly coupled.

Copy link
Member Author

Choose a reason for hiding this comment

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

I moved the method and added in javadoc. Could you check if the wording is OK?

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.

Looks good, assuming there are tests calling in with no class data to check that defineClass is fine with null inputs?

@iklam
Copy link
Member Author

iklam commented Oct 17, 2022

Looks good, assuming there are tests calling in with no class data to check that defineClass is fine with null inputs?

Yes, we have two existing test cases that has no class data:

  • java/lang/invoke/CompileThresholdBootstrapTest
  • java/lang/invoke/LoopCombinatorLongSignatureTest

I found them by adding this to InvokerBytecodeGenerator::classDataValues()

    private Object classDataValues() {
        final List<ClassData> cd = classData;
        if (cd.size() == 0) {
            throw new RuntimeException("huh");
        }

If I remove the above hack but keep my patch, the tests passed. So I think this case is covered.

@@ -360,6 +342,29 @@ private void methodEpilogue() {
mv.visitEnd();
}

/**
* Returns an object to pass this.classData to the <clinit> method of the
Copy link
Member

Choose a reason for hiding this comment

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

What about:

Suggested change
* Returns an object to pass this.classData to the <clinit> method of the
* Returns the class data object that will be passed to `Lookup.defineHiddenClass`.
* The classData is loaded in the <clinit> method of the generated class.
* If the class data contains only one single object, this method returns that single object.
* If the class data contains more than one objects, this method returns a List.
*
* This method returns null if no class data.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, the classData is passed here:

    private MemberName loadMethod(byte[] classFile) {
        Class<?> invokerClass = LOOKUP.makeHiddenClassDefiner(className, classFile, Set.of())
                                      .defineClass(true, classDataValues());
        return resolveInvokerMember(invokerClass, invokerName, invokerType);
    }

So it doesn't go through Lookup.defineHiddenClass.

Copy link
Member

Choose a reason for hiding this comment

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

yes, it's an internal version that defines a hidden class, equivalent to calling Lookup::defineHiddenClassWithClassData (typo in my suggested wording), but it will bypass the security permission check and also returns the Class instead of Lookup (saving not to allocate a Lookup object).

Copy link
Member

@mlchung mlchung left a comment

Choose a reason for hiding this comment

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

Thanks for the update. I made the suggested wording. You can push once it's updated.

*/
private Object classDataValues() {
final List<ClassData> cd = classData;
return switch(cd.size()) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return switch(cd.size()) {
return switch (cd.size()) {

@iklam
Copy link
Member Author

iklam commented Oct 19, 2022

Thanks @cl4es @iwanowww @mlchung for the review, and @turbanoff for the suggestion.
/integrate

@openjdk
Copy link

openjdk bot commented Oct 19, 2022

Going to push as commit 8d4c077.
Since your change was applied there have been 16 commits pushed to the master branch:

  • 017e798: 8293939: Move continuation_enter_setup and friends
  • f872467: 8255746: Make PrintCompilation available on a per method level
  • 388a56e: 8294467: Fix sequence-point warnings in Hotspot
  • ceb5b08: 8294468: Fix char-subscripts warnings in Hotspot
  • 7b1c676: 8295662: jdk/incubator/vector tests fail "assert(VM_Version::supports_avx512vlbw()) failed"
  • 5eaf568: 8295668: validate-source failure after JDK-8290011
  • e238920: 8295372: CompactNumberFormat handling of number one with decimal part
  • a5f6e31: 8295456: (ch) sun.nio.ch.Util::checkBufferPositionAligned gives misleading/incorrect error
  • e27bea0: 8290011: IGV: Remove dead code and cleanup
  • d37ce4c: 8290368: Introduce LDAP and RMI protocol-specific object factory filters to JNDI implementation
  • ... and 6 more: https://git.openjdk.org/jdk/compare/3f4964f83d6f03efbee2fb34aa8258d4fc923efb...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Oct 19, 2022
@openjdk openjdk bot closed this Oct 19, 2022
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Oct 19, 2022
@openjdk
Copy link

openjdk bot commented Oct 19, 2022

@iklam Pushed as commit 8d4c077.

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

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.

5 participants