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

8271623: Omit enclosing instance fields from inner classes that don't use it #4966

Closed
wants to merge 8 commits into from

Conversation

cushon
Copy link
Contributor

@cushon cushon commented Aug 3, 2021

This change omits the synthetic this$0 field from inner classes that do not access any enclosing instance state.


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed

Issue

  • JDK-8271623: Omit enclosing instance fields from inner classes that don't use it

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/4966/head:pull/4966
$ git checkout pull/4966

Update a local copy of the PR:
$ git checkout pull/4966
$ git pull https://git.openjdk.java.net/jdk pull/4966/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 4966

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

Using diff file

Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/4966.diff

@bridgekeeper
Copy link

bridgekeeper bot commented Aug 3, 2021

👋 Welcome back cushon! 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 Aug 3, 2021

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

  • compiler

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 compiler compiler-dev@openjdk.org label Aug 3, 2021
@openjdk openjdk bot added the rfr Pull request is ready for review label Aug 3, 2021
@mlbridge
Copy link

mlbridge bot commented Aug 3, 2021

Webrevs

@jddarcy
Copy link
Member

jddarcy commented Aug 3, 2021

/csr needed

@openjdk openjdk bot added the csr Pull request needs approved CSR before integration label Aug 3, 2021
@openjdk
Copy link

openjdk bot commented Aug 3, 2021

@jddarcy has indicated that a compatibility and specification (CSR) request is needed for this pull request.
@cushon please create a CSR request for issue JDK-8271623. This pull request cannot be integrated until the CSR request is approved.

@liach
Copy link
Member

liach commented Aug 3, 2021

I hope we have a test case that ensures the field is not erroneously removed when this nested class doesn't use the field, but another nested class within this nested class does. An example:

public class Second {

  int t = 3;

  public static void main(String... a) {
    var q = new Second();
    var p = q.new Third().new Fourth();
    q.t = 6;
    p.print();
  }
  
  public class Third {
    public class Fourth {
      public void print() {
        System.out.println(Second.this.t);
      }
    }
  }
}

@cushon
Copy link
Contributor Author

cushon commented Aug 3, 2021

@liach thanks, I added another test case with nested inner classes.

The current implementation handles that correctly, because the nested classes are lowered first, so any uses of the enclosing instance's this$ field will be recorded before it is finished being lowered.

@jddarcy
Copy link
Member

jddarcy commented Aug 3, 2021

For the CSR review of this change, I'll request a corpus run be initiated.

Depending on the results of that run, the use of the new code generation idiom here may be triggered on -source/--release level. In other words, always use this$ in -source/--release 17 and earlier and omit it (when possible) on 18 and later.

If you have not done so already, please also run the core reflection regression tests with this change.

While the code generation idioms of javac are not a supported contract, applications can come to reply on them and change this idiom for earlier source levels may by too large of a behavioral compatibility change in that use-case.

@bridgekeeper
Copy link

bridgekeeper bot commented Aug 31, 2021

@cushon This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

@bridgekeeper
Copy link

bridgekeeper bot commented Sep 28, 2021

@cushon This pull request has been inactive for more than 8 weeks and will now be automatically closed. If you would like to continue working on this pull request in the future, feel free to reopen it! This can be done using the /open pull request command.

@bridgekeeper bridgekeeper bot closed this Sep 28, 2021
@cushon
Copy link
Contributor Author

cushon commented Oct 9, 2021

/open

@openjdk openjdk bot reopened this Oct 9, 2021
@openjdk
Copy link

openjdk bot commented Oct 9, 2021

@cushon This pull request is now open

@cushon
Copy link
Contributor Author

cushon commented Oct 9, 2021

@openjdk openjdk bot removed the csr Pull request needs approved CSR before integration label Oct 26, 2021
@cushon
Copy link
Contributor Author

cushon commented Nov 4, 2021

The CSR has been approved, I think this is ready for review. Is anyone interested in taking a look?

@fweimer-rh
Copy link

Is it desirable to do this change for Serializable inner classes? If they do not define serialVersionUID, it won't be possible to deserialize existing data.

@cushon
Copy link
Contributor Author

cushon commented Nov 15, 2021

Is it desirable to do this change for Serializable inner classes? If they do not define serialVersionUID, it won't be possible to deserialize existing data.

@fweimer-rh thanks, I added logic to disable the optimization for Serializable inner classes which do not define serialVersionUID

/** Omit unused enclosing instance fields from inner classes that don't access enclosing
* instance state.
*/
public boolean omitUnusedPrivateSyntheticEnclosingInstanceFields() {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: what about a shorter name for this one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

SGTM, maybe optimizeOuterThis? I'm open to suggestions :)

Copy link
Contributor

@vicente-romero-oracle vicente-romero-oracle left a comment

Choose a reason for hiding this comment

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

made a first review of the code, looks good, still I would like to give another pass on it. Comment, do you still need to force the access to the outer class in all the test cases or does it make sense to provide a new golden file for some of them when applicable?

Copy link
Contributor

@lahodaj lahodaj left a comment

Choose a reason for hiding this comment

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

I think overall it seems sensible. Regarding compatibility impact, I'd suggest to make sure it is understood (e.g. in the release notes) the behavior may be altered by instances being GCed sooner. (While it typically may be desirable, it might cause issues for some.)

tree.defs = tree.defs.prepend(otdef);
enterSynthetic(tree.pos(), otdef.sym, currentClass.members());

for (JCTree def : tree.defs) {
if (!TreeInfo.isInitialConstructor(def)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: given the rest of the for loop is fairly short, I'd recommend to consider not using a negation, but rather doing the work inside the if. (Although, this may be a personal preference, so only for consideration.) I.e. something like:

          for (JCTree def : tree.defs) {
                if (TreeInfo.isInitialConstructor(def)) {
                    JCMethodDecl mdef = (JCMethodDecl) def;
                    mdef.body.stats = mdef.body.stats.prepend(
                        initOuterThis(mdef.body.pos, mdef.params.head.sym));
                }
            }

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, thanks

@cushon
Copy link
Contributor Author

cushon commented Nov 22, 2021

do you still need to force the access to the outer class in all the test cases or does it make sense to provide a new golden file for some of them when applicable?

Thanks @vicente-romero-oracle for taking a first pass! For the tests that are about details of inner and outer classes, I wanted to make sure I didn't regress coverage for anything that was specifically testing this$. Taking another pass over the tests, I think for most of the attributes/Synthetic tests it makes more sense to just update the assertions. Let me know if you see any others where there's a better fix than adding an explicit enclosing instance reference, and I'm happy to update them also.

I think overall it seems sensible. Regarding compatibility impact, I'd suggest to make sure it is understood (e.g. in the release notes) the behavior may be altered by instances being GCed sooner. (While it typically may be desirable, it might cause issues for some.)

@lahodaj Thanks! I created the sub-task for the release notes and added a note there as a reminder to mention that: https://bugs.openjdk.java.net/browse/JDK-8277604

Copy link
Contributor

@vicente-romero-oracle vicente-romero-oracle left a comment

Choose a reason for hiding this comment

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

lgtm, thanks for fixing this!

@openjdk
Copy link

openjdk bot commented Nov 23, 2021

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

8271623: Omit enclosing instance fields from inner classes that don't use it

Reviewed-by: vromero, jlahoda

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

  • 7b67a49: 8261847: performance of java.lang.Record::toString should be improved
  • 38802ad: 8254108: ciReplay: Support incremental inlining

Please see this link for an up-to-date comparison between the source branch of this pull request and 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 added the ready Pull request is ready to be integrated label Nov 23, 2021
@cushon
Copy link
Contributor Author

cushon commented Nov 23, 2021

@vicente-romero-oracle thanks for the review!

I rebased and pushed one more commit to test/langtools/tools/javac/classfiles/attributes/Synthetic/BridgeMethodsForLambdaTest.java to restore an -XD flag that was accidentally removed.

Copy link
Contributor

@lahodaj lahodaj 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 to me.

@cushon
Copy link
Contributor Author

cushon commented Nov 23, 2021

/integrate

@openjdk
Copy link

openjdk bot commented Nov 23, 2021

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

  • 0320672: 8277451: java.lang.reflect.Field::set on static field with invalid argument type should throw IAE
  • e8acac2: 8277350: runtime/jni/checked/TestPrimitiveArrayCriticalWithBadParam.java times out
  • 8a44e09: 8268725: jshell does not support the --enable-native-access option
  • 7b67a49: 8261847: performance of java.lang.Record::toString should be improved
  • 38802ad: 8254108: ciReplay: Support incremental inlining

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot closed this Nov 23, 2021
@openjdk openjdk bot added integrated Pull request has been integrated and removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Nov 23, 2021
@openjdk
Copy link

openjdk bot commented Nov 23, 2021

@cushon Pushed as commit ea85e01.

💡 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
compiler compiler-dev@openjdk.org integrated Pull request has been integrated
6 participants