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

Jochen Theodorou commented on GROOVY-11632:
-------------------------------------------

[~paint_ninja] I think leftShift is a pretty straight forward. I think it does 
not do the cast there as well. Be free to proof me wrong. But that also means 
there is no dynamic method invocation just two layers of methods calls or so 
and that's it. I don't think that this will be the kind of overhead we would 
want to compensate for.

"l+=l" is basically "l'=l+x;l'", which is quite a different beast in several 
aspects.
1) to keep semantics you cannot simply shortcut to list.add. If you take 
l'=l+x;l' you actually have get l, get x, execute add, set l. While short 
cutting to add would mean not to call the set.. well, you still could, but it 
is a semantic aspect to consider.
2) the logic of "cloneSimilarCollection", which may result in dynamically 
calling clone, is a bit problematic in terms of overhead. for this I see 
potential. But point 1 has to be considered of course if l is not a local 
variable.

> Optimise bytecode for list addition
> -----------------------------------
>
>                 Key: GROOVY-11632
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11632
>             Project: Groovy
>          Issue Type: Improvement
>    Affects Versions: 5.0.0-alpha-12
>            Reporter: Oscar N
>            Priority: Minor
>
> Using the += or << operators to add to a collection involves calls to DGM 
> methods and an indy call to cast. Having these operators directly call 
> Collection#add() when no extension methods are registered would avoid some 
> overhead and further improve @POJO support.
> {code:groovy}
> @CompileStatic
> final class ListDeclaration {
>     static void main(String[] args) {
>         var list = []
>         list += "Hello"
>         list << "World"
>         println list
>     }
> }
> {code}
> {code:java}
>   // access flags 0x89
>   public static varargs main([Ljava/lang/String;)V
>    L0
>     LINENUMBER 6 L0
>     NEW java/util/ArrayList
>     DUP
>     INVOKESPECIAL java/util/ArrayList.<init> ()V
>     ASTORE 1
>    L1
>     LINENUMBER 7 L1
>     ALOAD 1
>     LDC "Hello"
>     INVOKESTATIC org/codehaus/groovy/runtime/DefaultGroovyMethods.plus 
> (Ljava/util/List;Ljava/lang/Object;)Ljava/util/List;
>     INVOKEDYNAMIC cast(Ljava/util/List;)Ljava/util/ArrayList; [
>       // handle kind 0x6 : INVOKESTATIC
>       
> org/codehaus/groovy/vmplugin/v8/IndyInterface.bootstrap(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;I)Ljava/lang/invoke/CallSite;
>       // arguments:
>       "()", 
>       0
>     ]
>     ASTORE 1
>    L2
>     LINENUMBER 8 L2
>     ALOAD 1
>     LDC "World"
>     INVOKESTATIC org/codehaus/groovy/runtime/DefaultGroovyMethods.leftShift 
> (Ljava/util/List;Ljava/lang/Object;)Ljava/util/List;
>     POP
>    L3
>     LINENUMBER 9 L3
>     LDC LListDeclaration;.class
>     ALOAD 1
>     INVOKESTATIC org/codehaus/groovy/runtime/DefaultGroovyMethods.println 
> (Ljava/lang/Object;Ljava/lang/Object;)V
>     ACONST_NULL
>     POP
>    L4
>     LINENUMBER 10 L4
>     RETURN
>     LOCALVARIABLE args [Ljava/lang/String; L0 L4 0
>     LOCALVARIABLE list Ljava/util/ArrayList; L1 L4 1
>     MAXSTACK = 2
>     MAXLOCALS = 2
> {code}



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

Reply via email to