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/10 15:40:35 UTC

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

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

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


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

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

    GROOVY-10477: SC: optimize `for (item in array) { }`
---
 .../asm/sc/StaticTypesStatementWriter.java         |  2 +-
 src/test/groovy/transform/stc/LoopsSTCTest.groovy  | 30 ++++++++++++----------
 .../classgen/asm/sc/LoopsStaticCompileTest.groovy  | 10 ++++++--
 3 files changed, 25 insertions(+), 17 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 cac69f7..56a59b9 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 {
         ClassNode collectionType = typeChooser.resolveType(collectionExpression, controller.getClassNode());
         Parameter loopVariable = loop.getVariable();
         int size = operandStack.getStackLength();
-        if (collectionType.isArray() && loopVariable.getOriginType().equals(collectionType.getComponentType())) {
+        if (collectionType.isArray() && loopVariable.getType().equals(collectionType.getComponentType())) {
             writeOptimizedForEachLoop(compileStack, operandStack, mv, loop, collectionExpression, collectionType, loopVariable);
         } else if (ENUMERATION_CLASSNODE.equals(collectionType)) {
             writeEnumerationBasedForEachLoop(loop, collectionExpression, collectionType);
diff --git a/src/test/groovy/transform/stc/LoopsSTCTest.groovy b/src/test/groovy/transform/stc/LoopsSTCTest.groovy
index 2798cfb..b4dd718 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.
  */
@@ -45,21 +43,25 @@ class LoopsSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
-    // GROOVY-8882
-    void testStringCollectionLoop() {
-        for (char c in 'abc') assert c instanceof Character
-        for (Character c in 'abc') assert c instanceof Character
-        for (String s in 'abc') assert s instanceof String
-        for (s in 'abc') assert s instanceof String
+    void testForInLoopOnArray() {
+        assertScript '''
+            String[] strings = ['a','b','c']
+            for (string in strings) {
+                string.toUpperCase()
+            }
+        '''
     }
 
     // GROOVY-8882
-    @CompileStatic
-    void testStringCollectionLoopCS() {
-        for (char c in 'abc') assert c instanceof Character
-        for (Character c in 'abc') assert c instanceof Character
-        for (String s in 'abc') assert s instanceof String
-        for (s in 'abc') assert s instanceof String
+    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')
+    }
+}