RFR: 8271623: Omit enclosing instance fields from inner classes that don't use it [v4]

Jan Lahoda jlahoda at openjdk.java.net
Mon Nov 22 22:01:16 UTC 2021


On Mon, 15 Nov 2021 20:18:06 GMT, Liam Miller-Cushon <cushon at openjdk.org> wrote:

>> This change omits the synthetic `this$0` field from inner classes that do not access any enclosing instance state.
>
> Liam Miller-Cushon has updated the pull request with a new target base due to a merge or a rebase. The incremental webrev excludes the unrelated changes brought in by the merge/rebase. The pull request contains four additional commits since the last revision:
> 
>  - Don't remove enclosing instance fields from serializable classes without serialVersionUIDs
>  - Enable enclosing instance optimization for --release 18 and newer
>  - Add a test case with nested inner classes
>  - 8271623: Omit enclosing instance fields from inner classes that don't use it

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.)

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java line 2251:

> 2249: 
> 2250:            for (JCTree def : tree.defs) {
> 2251:                 if (!TreeInfo.isInitialConstructor(def)) {

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));
                }
            }

-------------

PR: https://git.openjdk.java.net/jdk/pull/4966


More information about the compiler-dev mailing list