8058243: Reduce size of bytecode for large switch statements
Reviewed-by: jjg, vromero
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Gen.java Fri Oct 10 14:41:50 2014 -0700
+++ b/langtools/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);