8058243: Reduce size of bytecode for large switch statements

Reviewed-by: jjg, vromero
This commit is contained in:
Paul Govereau 2014-10-13 11:21:51 -04:00
parent a7b8ca6209
commit f3201874bc

View File

@ -1220,17 +1220,14 @@ public class Gen extends JCTree.Visitor {
}
// 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);