Oscar N created GROOVY-11295: -------------------------------- Summary: Optimise generated next() and previous() in enums Key: GROOVY-11295 URL: https://issues.apache.org/jira/browse/GROOVY-11295 Project: Groovy Issue Type: Improvement Components: Compiler, performance Affects Versions: 5.0.0-alpha-5, 4.0.18, 3.0.20, 2.5.23 Reporter: Oscar N
I have the following code: {code:groovy} @CompileStatic enum Foo { BAR, BAZ Foo optimisedNext() { int ordinal = ordinal() + 1 if (ordinal >= $VALUES.length) ordinal = 0 return $VALUES[ordinal] } } {code} When looking at the compiled output, the next() method generated by the compiler could be more efficient: {code:java} public Foo optimisedNext() { int ordinal = this.ordinal() + 1; if (ordinal >= $VALUES.length) { ordinal = 0; } return ((Class)BytecodeInterface8.objectArrayGet($VALUES, ordinal)).cast<invokedynamic>(BytecodeInterface8.objectArrayGet($VALUES, ordinal)); } @Generated public Foo next() { Number ordinal = DefaultGroovyMethods.next(this.ordinal()); if (ScriptBytecodeAdapter.compareGreaterThanEqual(ordinal, ArrayGroovyMethods.size($VALUES))) { ordinal = 0; } Foo[] var10001 = $VALUES; return ((Class)ScriptBytecodeAdapter.invokeMethodN(Foo.class, var10001, (String)"getAt", new Object[]{ordinal})).cast<invokedynamic>(ScriptBytecodeAdapter.invokeMethodN(Foo.class, var10001, (String)"getAt", new Object[]{ordinal})); } {code} -- This message was sent by Atlassian Jira (v8.20.10#820010)