changeset 2663:a65be7dd80f8

8058243: Reduce size of bytecode for large switch statements Reviewed-by: jjg, vromero
author pgovereau
date Mon, 13 Oct 2014 11:21:51 -0400
parents 338eb81007d7
children b5d44ecef54d
files src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Gen.java
diffstat 1 files changed, 8 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Gen.java	Fri Oct 10 14:41:50 2014 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Gen.java	Mon Oct 13 11:21:51 2014 -0400
@@ -1220,17 +1220,14 @@
             }
 
             // Determine whether to issue a tableswitch or a lookupswitch
-            // instruction.
-            long table_space_cost = 4 + ((long) hi - lo + 1); // words
-            long table_time_cost = 3; // comparisons
-            long lookup_space_cost = 3 + 2 * (long) nlabels;
-            long lookup_time_cost = nlabels;
-            int opcode =
-                nlabels > 0 &&
-                table_space_cost + 3 * table_time_cost <=
-                lookup_space_cost + 3 * lookup_time_cost
-                ?
-                tableswitch : lookupswitch;
+            // instruction. The difference in computation cost is
+            // proportional to log(#cases), which is negligable, so we only
+            // consider the size of the bytecode.
+            // A discussion of the metric can be found here:
+            // http://mail.openjdk.java.net/pipermail/compiler-dev/2014-September/008987.html
+            int table_cost = 4 + (hi - lo + 1); // words
+            int lookup_cost = 3 + 2 * nlabels;
+            int opcode = table_cost <= lookup_cost ? tableswitch : lookupswitch;
 
             int startpc = code.curCP();    // the position of the selector operation
             code.emitop0(opcode);