> From: "Brian Goetz" <brian.go...@oracle.com> > To: "daniel smith" <daniel.sm...@oracle.com>, "valhalla-spec-experts" > <valhalla-spec-experts@openjdk.java.net> > Sent: Friday, June 3, 2022 6:21:26 PM > Subject: Re: Anonymous value classes
> There is no chance to get any calling-convention optimization here, since the > concrete class name will not show up in any method descriptor (or preload > attribute). There is no chance to get any heap flattening here, since the > concrete class name will not show up in any field descriptor or `newarray` > operand. Nope, anonymous classes are anonymous only for Java not in the bytecode, by example var box = new Object() { }; Supplier<?> supplier = () -> box; var list = Arrays.asList(box); is translated to Code: 0: new #7 // class AnonymousClassNameLeaking$1 3: dup 4: invokespecial #9 // Method AnonymousClassNameLeaking$1."<init>":()V 7: astore_1 8: aload_1 9: invokedynamic #10, 0 // InvokeDynamic #0:get:(LAnonymousClassNameLeaking$1;)Ljava/util/function/Supplier; 14: astore_2 15: iconst_1 16: anewarray #7 // class AnonymousClassNameLeaking$1 19: dup 20: iconst_0 21: aload_1 22: aastore 23: invokestatic #14 // Method java/util/Arrays.asList:([Ljava/lang/Object;)Ljava/util/List; 26: astore_3 Here, the anonymous class name appears as parameter of invokedynamic, at runtime the field (box is captured) of the lambda proxy is also typed LAnonymousClassNameLeaking$1; and any varargs will create an array of the anonymous class. Rémi > On 6/3/2022 12:15 PM, Dan Smith wrote: >> Our javac prototype has long included support for a 'value' keyword after >> 'new' >> to indicate that an anonymous class is a value class: >> Runnable r = new value Runnable() { >> public void run() { x.foo(); } >> }; >> Is this something we'd like to preserve as a language feature? >> Arguments for: >> - Allows the semantics of "I don't need identity" to be conveyed (often true >> for >> anonymous classes). >> - Gives the JVM more information for optimization. If we don't need a heap >> object, evaluating the expression may be a no-op. >> Arguments against: >> - Opens a Pandora's box of syntax: what other keywords can go there? >> 'identity'? >> 'primitive'? 'static'? 'record'? >> - Because there's no named type, there are significantly fewer opportunities >> for >> optimization—you're probably going to end up with a heap object anyway. >> - Value classes are primarily focused on simple data-carrying use cases, but >> any >> data being carried by an anonymous class is usually incidental. A new >> language >> feature would draw a lot of attention to this out-of-the-mainstream use case. >> - In the simplest cases, you can use a lambda instead, and there the API >> implementation has freedom to implement lambdas with value classes if it >> turns >> out to be useful. >> - The workaround—declare a local class instead—is reasonably straightforward >> for >> the scenarios where there's a real performance bottleneck that 'value' can >> help >> with.