You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2024/01/18 20:17:00 UTC

[jira] [Commented] (GROOVY-11288) Unnecessary bytecode generated after variable assignment

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

Eric Milles commented on GROOVY-11288:
--------------------------------------

This is a consequence of sequences like "a += 1" or "b = c = 2" or "\{ -> int d = 3 } // 3 is returned".  To make the result of the sub-expression available to another expression or a return statement, it is pushed onto the stack.  If the value is not used, it is popped.  To make this flow simple and general in the code, we end up with a bit of extra bytecode in the case where you don't use the value after store.

> Unnecessary bytecode generated after variable assignment
> --------------------------------------------------------
>
>                 Key: GROOVY-11288
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11288
>             Project: Groovy
>          Issue Type: Bug
>          Components: bytecode, Compiler
>    Affects Versions: 2.5.23, 3.0.20, 5.0.0-alpha-4, 4.0.17
>            Reporter: Oscar N
>            Priority: Minor
>
> I have the following code:
> {code:groovy}
> package org.example
> import groovy.transform.CompileStatic
> @CompileStatic
> final class Main {
>     static void main(String[] args) {
>         int three = 3
>         String str = 'foo'
>     }
> }
> {code}
> When looking at the compiled output, the variable is loaded and immediately popped after storing:
> {code:java}
> // access flags 0x89
> public static varargs main([Ljava/lang/String;)V
>  L0
>   LINENUMBER 8 L0
>   ICONST_3
>   ISTORE 1
>  L1
>   ILOAD 1
>   POP
>  L2
>   LINENUMBER 9 L2
>   LDC "foo"
>   ASTORE 2
>  L3
>   ALOAD 2
>   POP
>  L4
>   LINENUMBER 10 L4
>   RETURN
>   LOCALVARIABLE args [Ljava/lang/String; L0 L4 0
>   LOCALVARIABLE three I L1 L4 1
>   LOCALVARIABLE str Ljava/lang/String; L3 L4 2
>   MAXSTACK = 1
>   MAXLOCALS = 3
> {code}
> This issue also occurs in dynamic compilation mode.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)