Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhanced Switch] Wasteful generation of switch ordinal mapping table for enum switches that are dispatched via enumSwitch indy #3447

Closed
srikanth-sankaran opened this issue Dec 13, 2024 · 1 comment · Fixed by #3448
Assignees
Milestone

Comments

@srikanth-sankaran
Copy link
Contributor

Found by code inspection and white box testing:

Consider the following program:

enum E {
	A,
	B,
}

public class X {
	public static void main(String[] args) {
		E e = E.A;
		switch (e) {
			case A :    System.out.println("A"); 
					    break;
			case B:     System.out.println("B");
			            break;
			case null : System.out.println("null");
						break;
		}		
	}
} 

Code produced by ECJ on master/HEAD contains the field $SWITCH_TABLE$E and the method $SWITCH_TABLE$E() which are not needed for an enhanced switch which will use indy enumSwitch bootstrap method to switch.

private static volatile int[] $SWITCH_TABLE$E;
    descriptor: [I
    flags: (0x104a) ACC_PRIVATE, ACC_STATIC, ACC_VOLATILE, ACC_SYNTHETIC
  static int[] $SWITCH_TABLE$E();
    descriptor: ()[I
    flags: (0x1008) ACC_STATIC, ACC_SYNTHETIC
    Code:
      stack=3, locals=1, args_size=0
         0: getstatic     #55                 // Field $SWITCH_TABLE$E:[I
         3: dup
         4: ifnull        8
         7: areturn
         8: pop
         9: invokestatic  #57                 // Method E.values:()[LE;
        12: arraylength
        13: newarray       int
        15: astore_0
        16: aload_0
        17: getstatic     #18                 // Field E.A:LE;
        20: invokevirtual #61                 // Method E.ordinal:()I
        23: iconst_1
        24: iastore
        25: goto          29
        28: pop
        29: aload_0
        30: getstatic     #65                 // Field E.B:LE;
        33: invokevirtual #61                 // Method E.ordinal:()I
        36: iconst_2
        37: iastore
        38: goto          42
        41: pop
        42: aload_0
        43: dup
        44: putstatic     #55                 // Field $SWITCH_TABLE$E:[I
        47: areturn
      Exception table:
         from    to  target type
            16    25    28   Class java/lang/NoSuchFieldError
            29    38    41   Class java/lang/NoSuchFieldError
      LineNumberTable:
        line 6: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
      StackMapTable: number_of_entries = 5
        frame_type = 72 /* same_locals_1_stack_item */
          stack = [ class "[I" ]
        frame_type = 255 /* full_frame */
          offset_delta = 19
          locals = [ class "[I" ]
          stack = [ class java/lang/NoSuchFieldError ]
        frame_type = 0 /* same */
        frame_type = 75 /* same_locals_1_stack_item */
          stack = [ class java/lang/NoSuchFieldError ]
        frame_type = 0 /* same */
}
@srikanth-sankaran
Copy link
Contributor Author

Javac from JDK23 does better by suppressing its version of the synthetic mapping table Field X$1.$SwitchMap$E

@srikanth-sankaran srikanth-sankaran self-assigned this Dec 13, 2024
@srikanth-sankaran srikanth-sankaran added this to the 4.35 M1 milestone Dec 13, 2024
srikanth-sankaran added a commit to srikanth-sankaran/eclipse.jdt.core that referenced this issue Dec 13, 2024
for enum switches that are dispatched via enumSwitch indy

* Fixes eclipse-jdt#3447
srikanth-sankaran added a commit that referenced this issue Dec 13, 2024
… for enum switches that are dispatched via enumSwitch indy(#3448)

* Fixes #3447
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant