[code-reflection] RFR: Lambda model uniqueness [v5]

Paul Sandoz psandoz at openjdk.org
Tue May 6 17:12:28 UTC 2025


On Tue, 6 May 2025 11:14:19 GMT, Mourad Abbay <mabbay at openjdk.org> wrote:

>> Lambdas that share the same proxy class, have the same implementation and therefore have the same code model generated by the compiler. In this PR, we share the code model across instances of the same proxy class. 
>> This also means we reduce execution time as we only need to invoke `opMethod` once for given proxy class.
>> With these changes we can compare lambda code model based on identity.
>
> Mourad Abbay has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Remove unused code

src/java.base/share/classes/java/lang/invoke/InnerClassLambdaMetafactory.java line 397:

> 395:     }
> 396: 
> 397:     private void generateStaticInitializer(ClassBuilder clb) {

[Class initialization method](https://docs.oracle.com/javase/specs/jvms/se24/html/jvms-2.html#jvms-2.9.2) is the correct term, recommend renaming the method to `generateClassInitializationMethod`.

src/java.base/share/classes/java/lang/invoke/InnerClassLambdaMetafactory.java line 398:

> 396: 
> 397:     private void generateStaticInitializer(ClassBuilder clb) {
> 398:         clb.withMethodBody(CLASS_INIT_NAME, MTD_void, ACC_STATIC, new Consumer<CodeBuilder>() {

This might always create a class initializer method, which might be empty. I think we should condition the creation.

src/java.base/share/classes/java/lang/invoke/InnerClassLambdaMetafactory.java line 582:

> 580:             @Override
> 581:             public void accept(CodeBuilder cob) {
> 582:                 // bytecode for double-checked locking (copied from the compiler)

I wonder if you can rewrite this using `BlockCodeBuilder` and `CatchBuilder` (see usages in `CodeBuilder` methods)? If so i think it would be easier to comprehend as the structure of the code becomes evident.

src/java.base/share/classes/java/lang/invoke/InnerClassLambdaMetafactory.java line 605:

> 603:                         .aload(1)
> 604:                         .ifnonnull(l2);
> 605:                 createQuotedObj(cob);

I find this method confusing because we are performing DCL over the quoted instance and not the code model. I can see why you did that.

Can we refine this in a subsequent PR to do so on the model and then we can create `Quoted` instance each time? Later we can turn `Quoted` into a value class, and for now we can mark it as value-based. Then i think that allows us to adjust later if we so wish to store the code model in a weak reference

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

PR Review Comment: https://git.openjdk.org/babylon/pull/420#discussion_r2075885528
PR Review Comment: https://git.openjdk.org/babylon/pull/420#discussion_r2075882688
PR Review Comment: https://git.openjdk.org/babylon/pull/420#discussion_r2075880526
PR Review Comment: https://git.openjdk.org/babylon/pull/420#discussion_r2075922108


More information about the babylon-dev mailing list