[ 
https://issues.apache.org/jira/browse/GROOVY-11631?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17953970#comment-17953970
 ] 

Paul King commented on GROOVY-11631:
------------------------------------

This would be applicable for SC or SC+POJO. For the TC case, the bytecode would 
still allow for runtime defined isCase.

> Use native lookup switch for enums in TC/CS/POJO modes
> ------------------------------------------------------
>
>                 Key: GROOVY-11631
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11631
>             Project: Groovy
>          Issue Type: Improvement
>          Components: performance
>    Affects Versions: 5.0.0-alpha-12
>            Reporter: Oscar N
>            Priority: Minor
>
> It would be nice if the Groovy compiler could generate a native lookup switch 
> on the ordinal() for enum types when there is no extension method registered 
> for the enum's isCase() method and the enum itself doesn't declare one.
> {code:groovy}
> @POJO
> @CompileStatic
> enum SomeEnum {
>     A, B
> }
> @POJO
> @CompileStatic
> final class NativeEnumSwitch {
>     static void main(String[] args) {
>         SomeEnum value = SomeEnum.A
>         switch (value) {
>             case SomeEnum.A:
>                 println "Value is A"
>                 break
>             case SomeEnum.B:
>                 println "Value is B"
>                 break
>         }
>     }
> }
> {code}
> Currently the compiler generates an else-if chain of 
> ScriptBytecodeAdapter.isCase() calls, even with the POJO annotation:
> {code:java}
> public static void main(String... args) {
>     SomeEnum value = SomeEnum.A;
>     if (ScriptBytecodeAdapter.isCase(value, SomeEnum.A)) {
>         DefaultGroovyMethods.println(NativeEnumSwitch.class, "Value is A");
>         Object var10000 = null;
>     } else if (ScriptBytecodeAdapter.isCase(value, SomeEnum.B)) {
>         DefaultGroovyMethods.println(NativeEnumSwitch.class, "Value is B");
>         Object var3 = null;
>     }
> }
> {code}
> {code:java}
>   // access flags 0x89
>   public static varargs main([Ljava/lang/String;)V
>    L0
>     LINENUMBER 14 L0
>     GETSTATIC SomeEnum.A : LSomeEnum;
>     ASTORE 1
>    L1
>     LINENUMBER 15 L1
>     ALOAD 1
>     ASTORE 2
>    L2
>     LINENUMBER 16 L2
>     ALOAD 2
>     GETSTATIC SomeEnum.A : LSomeEnum;
>     INVOKESTATIC org/codehaus/groovy/runtime/ScriptBytecodeAdapter.isCase 
> (Ljava/lang/Object;Ljava/lang/Object;)Z
>     IFEQ L3
>    L4
>     LINENUMBER 17 L4
>     LDC LNativeEnumSwitch;.class
>     LDC "Value is A"
>     INVOKESTATIC org/codehaus/groovy/runtime/DefaultGroovyMethods.println 
> (Ljava/lang/Object;Ljava/lang/Object;)V
>     ACONST_NULL
>     POP
>    L5
>     LINENUMBER 18 L5
>     GOTO L6
>     NOP
>     NOP
>     ATHROW
>    L3
>     LINENUMBER 19 L3
>     ALOAD 2
>     GETSTATIC SomeEnum.B : LSomeEnum;
>     INVOKESTATIC org/codehaus/groovy/runtime/ScriptBytecodeAdapter.isCase 
> (Ljava/lang/Object;Ljava/lang/Object;)Z
>     IFEQ L6
>    L7
>     LINENUMBER 20 L7
>     LDC LNativeEnumSwitch;.class
>     LDC "Value is B"
>     INVOKESTATIC org/codehaus/groovy/runtime/DefaultGroovyMethods.println 
> (Ljava/lang/Object;Ljava/lang/Object;)V
>     ACONST_NULL
>     POP
>    L8
>     LINENUMBER 21 L8
>     GOTO L6
>    L6
>     LINENUMBER 23 L6
>     RETURN
>     LOCALVARIABLE args [Ljava/lang/String; L0 L6 0
>     LOCALVARIABLE value LSomeEnum; L1 L6 1
>     MAXSTACK = 2
>     MAXLOCALS = 3
> {code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to