You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2022/02/09 19:27:24 UTC

[groovy] branch master updated: GROOVY-10477: SC: optimize `for (item in array) { }` -- dynamic variable

This is an automated email from the ASF dual-hosted git repository.

emilles pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new 999a81b  GROOVY-10477: SC: optimize `for (item in array) { }` -- dynamic variable
999a81b is described below

commit 999a81be1afeb8aff26b1cfe1c81dbd891c4b0e2
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Wed Feb 9 13:21:07 2022 -0600

    GROOVY-10477: SC: optimize `for (item in array) { }` -- dynamic variable
---
 .../asm/sc/StaticTypesStatementWriter.java         |  2 +-
 src/test/groovy/transform/stc/LoopsSTCTest.groovy  | 32 +++++++++++-----------
 .../classgen/asm/sc/LoopsStaticCompileTest.groovy  | 10 +++++--
 3 files changed, 25 insertions(+), 19 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesStatementWriter.java b/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesStatementWriter.java
index 176cb6c..fe28544 100644
--- a/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesStatementWriter.java
+++ b/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesStatementWriter.java
@@ -96,7 +96,7 @@ public class StaticTypesStatementWriter extends StatementWriter {
 
         int mark = operandStack.getStackLength();
         Parameter loopVariable = loop.getVariable();
-        if (collectionType.isArray() && loopVariable.getOriginType().equals(collectionType.getComponentType())) {
+        if (collectionType.isArray() && loopVariable.getType().equals(collectionType.getComponentType())) {
             writeOptimizedForEachLoop(loop, loopVariable, collectionExpression, collectionType);
         } else if (GeneralUtils.isOrImplements(collectionType, ENUMERATION_CLASSNODE)) {
             writeEnumerationBasedForEachLoop(loop, collectionExpression, collectionType);
diff --git a/src/test/groovy/transform/stc/LoopsSTCTest.groovy b/src/test/groovy/transform/stc/LoopsSTCTest.groovy
index 1d306f0..e91ec80 100644
--- a/src/test/groovy/transform/stc/LoopsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/LoopsSTCTest.groovy
@@ -18,8 +18,6 @@
  */
 package groovy.transform.stc
 
-import groovy.transform.CompileStatic
-
 /**
  * Unit tests for static type checking : loops.
  */
@@ -37,23 +35,25 @@ class LoopsSTCTest extends StaticTypeCheckingTestCase {
         }
     }
 
-    // GROOVY-8882
-    void testStringCollectionLoop() {
-        for (s in 'abc') assert s instanceof String
-        for (String s in 'abc') assert s instanceof String
-
-        for (char c in 'abc') assert c instanceof Character
-        for (Character c in 'abc') assert c instanceof Character
+    void testForInLoopOnArray() {
+        assertScript '''
+            String[] strings = ['a','b','c']
+            for (string in strings) {
+                string.toUpperCase()
+            }
+        '''
     }
 
     // GROOVY-8882
-    @CompileStatic
-    void testStringCollectionLoopCS() {
-        for (s in 'abc') assert s instanceof String
-        for (String s in 'abc') assert s instanceof String
-
-        for (char c in 'abc') assert c instanceof Character
-        for (Character c in 'abc') assert c instanceof Character
+    void testForInLoopOnString() {
+        assertScript '''
+            for (s in 'abc') assert s instanceof String
+            for (String s in 'abc') assert s instanceof String
+        '''
+        assertScript '''
+            for (char c in 'abc') assert c instanceof Character
+            for (Character c in 'abc') assert c instanceof Character
+        '''
     }
 
     void testMethodCallWithEachAndDefAndTwoFooMethods() {
diff --git a/src/test/org/codehaus/groovy/classgen/asm/sc/LoopsStaticCompileTest.groovy b/src/test/org/codehaus/groovy/classgen/asm/sc/LoopsStaticCompileTest.groovy
index d9669f7..614b833 100644
--- a/src/test/org/codehaus/groovy/classgen/asm/sc/LoopsStaticCompileTest.groovy
+++ b/src/test/org/codehaus/groovy/classgen/asm/sc/LoopsStaticCompileTest.groovy
@@ -23,6 +23,12 @@ import groovy.transform.stc.LoopsSTCTest
 /**
  * Unit tests for static type checking : miscellaneous tests.
  */
-class LoopsStaticCompileTest extends LoopsSTCTest implements StaticCompilationTestSupport {
-}
+final class LoopsStaticCompileTest extends LoopsSTCTest implements StaticCompilationTestSupport {
 
+    // GROOVY-10477
+    void testForInLoopOnArray() {
+        super.testForInLoopOnArray()
+        def bytecode = astTrees.values()[0][1]
+        assert !bytecode.contains('INVOKESTATIC org/codehaus/groovy/runtime/DefaultGroovyMethods.iterator')
+    }
+}