Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-11286

Extraneous bytecode generated after calls to void methods

Details

    Description

      I have the following code:

      package org.example
      
      import groovy.transform.CompileStatic
      
      @CompileStatic
      final class Main {
          static void main(String[] args) {
              exampleMethod()
          }
      
          static void exampleMethod() {}
      }
      
      

      When looking at the compiled output, null is pushed to the stack then immediately popped:

      // access flags 0x89
      public static varargs main([Ljava/lang/String;)V
       L0
        LINENUMBER 8 L0
        INVOKESTATIC org/example/Main.exampleMethod ()V
        ACONST_NULL
        POP
       L1
        LINENUMBER 9 L1
        RETURN
        LOCALVARIABLE args [Ljava/lang/String; L0 L1 0
        MAXSTACK = 1
        MAXLOCALS = 1
      

      Attachments

        Activity

          emilles Eric Milles added a comment -

          This specific case of void call in void method could be optimized. I’m general, the null is there for the expression value, which could be used in a number of circumstances, like a non-void return.

          emilles Eric Milles added a comment - This specific case of void call in void method could be optimized. I’m general, the null is there for the expression value, which could be used in a number of circumstances, like a non-void return.
          emilles Eric Milles added a comment - - edited https://github.com/apache/groovy/commit/1dfc6cd27133c11e0c5e302a9443ac127e17c39b https://github.com/apache/groovy/commit/7b070d826037130fa22973be696fae23b9bb43db https://github.com/apache/groovy/commit/338929cefcf6c3d95accdf1494c53eb6be40e63f https://github.com/apache/groovy/commit/2323b8b704bf3fd69bbce193bcc7cffb27f73616

          People

            emilles Eric Milles
            paint_ninja Oscar N
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: