You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (Jira)" <ji...@apache.org> on 2024/01/23 11:11:00 UTC
[jira] (GROOVY-11295) Optimise generated next() and previous() in enums
[ https://issues.apache.org/jira/browse/GROOVY-11295 ]
Paul King deleted comment on GROOVY-11295:
------------------------------------
was (Author: paulk):
Groovy allows you to write your own next() and previous(). I hope no-one has ever done that and changed next() and previous() inside to have non-conforming behavior - fine if they added logging or something else benign. It would be a breaking change in that case. I like your suggestion and think we should do something along those lines - just pointing out that rare breaking change - which means we might not push back to earlier versions, perhaps just 4 and 5.
> 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: 2.5.23, 3.0.20, 4.0.18, 5.0.0-alpha-5
> Reporter: Oscar N
> Priority: Minor
>
> 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)