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:57:23 UTC
[groovy] branch GROOVY_2_5_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_2_5_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_2_5_X by this push:
new 92d33f8 GROOVY-10477: SC: optimize `for (item in array) { }`
92d33f8 is described below
commit 92d33f8abb3db93b2b4f21b618b44003e5f35f83
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 f049eb8..e1295a4 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
@@ -99,7 +99,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(compileStack, operandStack, mv, loop, collectionExpression, collectionType, loopVariable);
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')
+ }
+}