Unnecessary boxing conversions in void-returning lambdas

XMLWordPrintable

    • Type: Bug
    • Resolution: Fixed
    • Priority: P2
    • 24
    • Affects Version/s: None
    • Component/s: tools
    • b08
    • Verified

      After the recent changes to lowering of lambdas, javac is generating unnecessary boxing conversions for expression bodied lambdas that implement a void returning method.

      class T {
        int g() { return 0; }
        Runnable r = () -> g();
      }

      Before:

        private void lambda$new$0();
          descriptor: ()V
          flags: (0x1002) ACC_PRIVATE, ACC_SYNTHETIC
          Code:
            stack=1, locals=1, args_size=1
               0: aload_0
               1: invokevirtual #17 // Method g:()I
               4: pop
               5: return
            LineNumberTable:
              line 3: 0

      After:

        private void lambda$new$0();
          descriptor: ()V
          flags: (0x1002) ACC_PRIVATE, ACC_SYNTHETIC
          Code:
            stack=1, locals=1, args_size=1
               0: aload_0
               1: invokevirtual #17 // Method g:()I
               4: invokestatic #21 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
               7: pop
               8: return
            LineNumberTable:
              line 3: 0

      Note the new call to Integer.valueOf

      javac is not an optimizing compiler, but the new calls are unnecessary can result in additional allocations, and would be nice to omit.

            Assignee:
            Liam Miller-Cushon
            Reporter:
            Liam Miller-Cushon
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: