deduplicating lambda methods

B. Blaser bsrbnd at gmail.com
Sat Mar 31 15:15:24 UTC 2018


On 30 March 2018 at 18:29, B. Blaser <bsrbnd at gmail.com> wrote:
> On 29 March 2018 at 22:02, Maurizio Cimadamore
> <maurizio.cimadamore at oracle.com> wrote:
>> I wonder if there could be a way to reuse the logic in Pool::DynamicMethod -
>> that code is essentially doing the same thing!
>>
>> Maurizio
>
> Yes (as next), great!
> All lambda tests are passing successfully but we'll have to re-run the
> others which seem OK with the previous key.
>
> Bernard

I've also updated the de-duplication test with meta-factory calls
inside lambdas, as here under.

Bernard

diff -r 9925be430918
test/langtools/tools/javac/lambda/deduplication/Deduplication.java
--- a/test/langtools/tools/javac/lambda/deduplication/Deduplication.java
   Wed Mar 28 14:24:17 2018 +0100
+++ b/test/langtools/tools/javac/lambda/deduplication/Deduplication.java
   Sat Mar 31 16:49:48 2018 +0200
@@ -32,6 +32,12 @@
     void group(Object... xs) {}

     void test() {
+
+        group(
+                (Runnable) () -> { ( (Runnable) () -> {} ).run(); },
+                (Runnable) () -> { ( (Runnable) () -> {} ).run(); }
+        );
+
         group((Function<String, Integer>) x -> x.hashCode());
         group((Function<Object, Integer>) x -> x.hashCode());


> diff -r 9925be430918
> src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java
> --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java
>    Wed Mar 28 14:24:17 2018 +0100
> +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java
>    Fri Mar 30 18:11:49 2018 +0200
> @@ -67,6 +67,7 @@
>  import static com.sun.tools.javac.code.Kinds.Kind.*;
>  import static com.sun.tools.javac.code.TypeTag.*;
>  import static com.sun.tools.javac.tree.JCTree.Tag.*;
> +import static com.sun.tools.javac.jvm.Pool.DynamicMethod;
>
>  import javax.lang.model.element.ElementKind;
>  import javax.lang.model.type.TypeKind;
> @@ -226,6 +227,8 @@
>
>          private Map<DedupedLambda, DedupedLambda> dedupedLambdas;
>
> +        private Map<DynamicMethod, DynamicMethodSymbol> dynMethSyms =
> new HashMap<>();
> +
>          /**
>           * list of deserialization cases
>           */
> @@ -1218,9 +1221,10 @@
>                                              (MethodSymbol)bsm,
>                                              indyType,
>                                              staticArgs.toArray());
> -
>              JCFieldAccess qualifier =
> make.Select(make.QualIdent(site.tsym), bsmName);
> -            qualifier.sym = dynSym;
> +            DynamicMethodSymbol existing = kInfo.dynMethSyms.putIfAbsent(
> +                    new DynamicMethod(dynSym, types), dynSym);
> +            qualifier.sym = existing != null ? existing : dynSym;
>              qualifier.type = indyType.getReturnType();
>
>              JCMethodInvocation proxyCall = make.Apply(List.nil(),
> qualifier, indyArgs);
> diff -r 9925be430918
> src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Pool.java
> --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Pool.java
>    Wed Mar 28 14:24:17 2018 +0100
> +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Pool.java
>    Fri Mar 30 18:11:49 2018 +0200
> @@ -180,10 +180,10 @@
>          }
>      }
>
> -    static class DynamicMethod extends Method {
> +    public static class DynamicMethod extends Method {
>          public Object[] uniqueStaticArgs;
>
> -        DynamicMethod(DynamicMethodSymbol m, Types types) {
> +        public DynamicMethod(DynamicMethodSymbol m, Types types) {
>              super(m, types);
>              uniqueStaticArgs = getUniqueTypeArray(m.staticArgs, types);
>          }


More information about the amber-dev mailing list