deduplicating lambda methods

B. Blaser bsrbnd at gmail.com
Wed Mar 28 17:44:28 UTC 2018


On 28 March 2018 at 17:44, B. Blaser <bsrbnd at gmail.com> wrote:
> On 27 March 2018 at 20:22, B. Blaser <bsrbnd at gmail.com> wrote:
>> On 27 March 2018 at 19:33, Vicente Romero <vicente.romero at oracle.com> wrote:
>>>
>>>
>>> On 03/27/2018 12:58 PM, Brian Goetz wrote:
>>>>
>>>> It looks like there were no changes in the outcome, perhaps because there
>>>> were no within-file duplications in the JDK.  (Which I believe.)  A more
>>>> streams/Rx/CompletableFuture-heavy codebase would likely see an improvement.
>>>
>>>
>>> right, no difference :(, let's see what happens with Liam's numbers :)
>>
>> Did you include the improvement I suggested to compare local variables
>> *by name* instead of * by symbol* ?
>> If not, there's quite no chance to find duplicates excepted trivial ones ;-)
>
> Field access symbols don't seem to be '==' neither (is that right?).
> So, to begin with something simple, I would compare all identifiers by name:
>
> TreeDiffer.visitIdent:
>     result = tree.sym.name == that.sym.name;
>
> TreeDiffer.visitSelect:
>     result = scan(tree.selected, that.selected) && tree.sym.name ==
> that.sym.name;
>
> TreeHasher.visitIdent:
>     hash(sym.name);
>
> TreeHasher.visitSelect:
>     hash(tree.sym.name);

Here is the full suggested improvement.

Bernard

diff -r 9925be430918
src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TreeDiffer.java
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TreeDiffer.java
   Wed Mar 28 14:24:17 2018 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TreeDiffer.java
   Wed Mar 28 19:32:16 2018 +0200
@@ -178,13 +178,13 @@
                 return;
             }
         }
-        result = tree.sym == that.sym;
+        result = tree.sym.name == that.sym.name;
     }

     @Override
     public void visitSelect(JCFieldAccess tree) {
         JCFieldAccess that = (JCFieldAccess) parameter;
-        result = scan(tree.selected, that.selected) && tree.sym == that.sym;
+        result = scan(tree.selected, that.selected) && tree.sym.name
== that.sym.name;
     }

     @Override
diff -r 9925be430918
src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TreeHasher.java
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TreeHasher.java
   Wed Mar 28 14:24:17 2018 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TreeHasher.java
   Wed Mar 28 19:32:16 2018 +0200
@@ -91,12 +91,12 @@
                 return;
             }
         }
-        hash(sym);
+        hash(sym.name);
     }

     @Override
     public void visitSelect(JCFieldAccess tree) {
-        hash(tree.sym);
+        hash(tree.sym.name);
         super.visitSelect(tree);
     }
 }

> I'll try to write it a bit better, run some more tests and find a
> couple of examples like:
>
>     Runnable r1 = () -> {
>         Runnable r2 = () -> {};
>     };
>     r1 = () -> {
>         Runnable r2 = () -> {};
>     };
>
>     r1 = () -> {
>         Class<?> c = Integer.class;
>     };
>     r1 = () -> {
>         Class<?> c = Integer.class;
>     };
>
> What do you think?
>
> Thanks,
> Bernard
>
>
>> Bernard
>>
>>> Vicente


More information about the amber-dev mailing list