[ 
https://issues.apache.org/jira/browse/GROOVY-10314?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17429602#comment-17429602
 ] 

Daniel Sun commented on GROOVY-10314:
-------------------------------------

I compiled the script via groovyc and decompiled with javap, here is what 
groovyc generated for {{test}} method. As we could see, ASM does not optimize 
the bytecode for us, there are still many {{nop}} in the bytecode.

{code:java}
  public java.lang.Object test();
    descriptor: ()Ljava/lang/Object;
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=2, locals=3, args_size=1
         0: aload_0
         1: ldc           #47                 // String hello
         3: invokedynamic #53,  0             // InvokeDynamic 
#1:invoke:(LTestGroovy10314;Ljava/lang/String;)Ljava/lang/Object;
         8: astore_1
         9: nop
        10: aload_0
        11: ldc           #55                 // String  world
        13: invokedynamic #53,  0             // InvokeDynamic 
#1:invoke:(LTestGroovy10314;Ljava/lang/String;)Ljava/lang/Object;
        18: pop
        19: nop
        20: aload_1
        21: areturn
        22: nop
        23: nop
        24: athrow
        25: nop
        26: nop
        27: nop
        28: nop
        29: nop
        30: nop
        31: nop
        32: nop
        33: athrow
        34: nop
        35: nop
        36: athrow
        37: astore_2
        38: aload_0
        39: ldc           #55                 // String  world
        41: invokedynamic #53,  0             // InvokeDynamic 
#1:invoke:(LTestGroovy10314;Ljava/lang/String;)Ljava/lang/Object;
        46: pop
        47: aload_2
        48: athrow
        49: nop
        50: athrow
      Exception table:
         from    to  target type
             0    10    37   any
            20    22    37   any
      StackMapTable: number_of_entries = 5
        frame_type = 255 /* full_frame */
          offset_delta = 22
          locals = []
          stack = [ class java/lang/Throwable ]
        frame_type = 66 /* same_locals_1_stack_item */
          stack = [ class java/lang/Throwable ]
        frame_type = 72 /* same_locals_1_stack_item */
          stack = [ class java/lang/Throwable ]
        frame_type = 255 /* full_frame */
          offset_delta = 2
          locals = [ class TestGroovy10314 ]
          stack = [ class java/lang/Throwable ]
        frame_type = 255 /* full_frame */
          offset_delta = 11
          locals = []
          stack = [ class java/lang/Throwable ]
      LineNumberTable:
        line 3: 0
        line 5: 10
        line 7: 49
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      49     0  this   LTestGroovy10314;
{code}


 

 

> finally block produces long sequence of nop instructions
> --------------------------------------------------------
>
>                 Key: GROOVY-10314
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10314
>             Project: Groovy
>          Issue Type: Bug
>          Components: class generator, Compiler
>            Reporter: Eric Milles
>            Priority: Minor
>              Labels: bytecode
>
> Consider the following:
> {code:groovy}
> def test() {
>   try {
>     print 'hello'
>   } finally {
>     print ' world'
>   }
> }
> {code}
> For any non-void method where the {{try}}/{{finally}} is part of the return 
> value, a long sequence of {{nop}} bytecode instructions is produced. The 
> sample code from GROOVY-4721 produces 18 {{nop}} in a row.
> {code}
>   // Method descriptor #42 ()Ljava/lang/Object;
>   // Stack: 2, Locals: 3
>   public java.lang.Object test();
>      0  aload_0 [this]
>      1  ldc <String "hello"> [47]
>      3  invokedynamic 1 invoke(Scratch, java.lang.String) : java.lang.Object 
> [53]
>      8  astore_1
>      9  nop
>     10  aload_0 [this]
>     11  ldc <String " world"> [55]
>     13  invokedynamic 1 invoke(Scratch, java.lang.String) : java.lang.Object 
> [53]
>     18  pop
>     19  nop
>     20  aload_1
>     21  areturn
>     22  nop
>     23  nop
>     24  athrow
>     25  nop
>     26  nop
>     27  nop
>     28  nop
>     29  nop
>     30  nop
>     31  nop
>     32  nop
>     33  athrow
>     34  nop
>     35  nop
>     36  athrow
>     37  astore_2
>     38  aload_0 [this]
>     39  ldc <String " world"> [55]
>     41  invokedynamic 1 invoke(Scratch, java.lang.String) : java.lang.Object 
> [53]
>     46  pop
>     47  aload_2
>     48  athrow
>     49  nop
>     50  athrow
>       Exception Table:
>         [pc: 0, pc: 10] -> 37 when : any
>         [pc: 20, pc: 22] -> 37 when : any
>       Line numbers:
>         [pc: 0, line: 3]
>         [pc: 10, line: 5]
>         [pc: 49, line: 7]
>       Local variable table:
>         [pc: 0, pc: 49] local: this index: 0 type: Scratch
> {code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to