[ 
https://issues.apache.org/jira/browse/GROOVY-8699?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Eric Milles updated GROOVY-8699:
--------------------------------
    Language: groovy

> Somehow reduce or optimize o.g.c.r calls in CompileStatic
> ---------------------------------------------------------
>
>                 Key: GROOVY-8699
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8699
>             Project: Groovy
>          Issue Type: Wish
>            Reporter: death lord
>            Assignee: Eric Milles
>            Priority: Major
>
> Given the following simple snippet:
>  
> {code:java}
> import groovy.transform.*
> @CompileStatic
> class IteratorTest {
>   static void main(args) {
>     final coords = [new Coord(1, 2), new Coord(0, 3), new Coord(1, 3), new 
> Coord(0, 2)]
>     int i = 0
>     for (coord in coords) {
>       if (i in coord.x..coord.y) println "coord $i is between coord $coord.x 
> and $coord.y"
>       ++i
>     }
>   }
>   
>   @TupleConstructor
>   static class Coord {
>     int x, y
>   }
> }{code}
> The bytecode output for the main method is polluted with needed class 
> constants (in 2.4.12)
> {code:java}
> public static void main(String... args) {
>     List coords = ScriptBytecodeAdapter.createList(new Object[]{new 
> IteratorTest.Coord(1, 2), new IteratorTest.Coord(0, 3), new 
> IteratorTest.Coord(1, 3), new IteratorTest.Coord(0, 2)});
>     // ScriptBytecodeAdapter.createList calls InvokerHelper.createList which 
> calls addAll on a ArrayList, meanwhile Arrays.asList (around since java 2) 
> would directly call an efficient private ArrayList constructor
>     int i = 0;
>     IteratorTest.Coord coord = null;
>     for(Iterator var4 = coords.iterator(); var4.hasNext(); ++i) {
>         coord = 
> (IteratorTest.Coord)ScriptBytecodeAdapter.castToType(var4.next(), 
> IteratorTest.Coord.class);
>         // this castToType call is fine with a normal cast
>         if (DefaultTypeTransformation.booleanUnbox(new IntRange(true, 
> coord.getX(), coord.getY()) == null ? false : DefaultGroovyMethods.isCase(new 
> IntRange(true, coord.getX(), coord.getY()), i))) {
>         // isCase returns a primitive boolean so no need for unbox
>         // if certain expressions like a range expression can prove to not be 
> null, then the == null check for for-in statements can be optimized out
>         // maybe an annotation to transform aliased method calls could make 
> this compile to (coord.x..coord.y).contains(i), or even better: (i >= coord.x 
> && i <= coord.y)
>             DefaultGroovyMethods.println(IteratorTest.class, new 
> GStringImpl(new Object[]{i, coord.getX(), coord.getY()}, new String[]{"coord 
> ", " is between coord ", " and ", ""}));
>             // GStrings could be optimized to StringBuilder if there's no 
> closure expressions
>             // or, this could alias transform into System.out.println, and 
> since it calls .toString on the GString all direct (GString 
> literal).toString() calls can be optimized to StringBuilder
>             Object var10000 = null;
>             // random aconst_null
>         }
>     }
> }
> {code}
>  
>  



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

Reply via email to