From 298742ae69870b72e782a1af31bbf0deeb282c59 Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Wed, 9 Apr 2014 15:36:41 +0400 Subject: [PATCH] Optimizations of When expression with int constants: heuristics from openJDK for decision about using tableswitch/lookupswitch --- .../jet/codegen/ExpressionCodegen.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java index 07b22e44e29..dca00df0514 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java @@ -3820,12 +3820,22 @@ The "returned" value of try expression with no finally is either the last expres i++; } - int hi = keys[keys.length - 1]; + int nlabels = keys.length; + int hi = keys[nlabels - 1]; int lo = keys[0]; - long emptyCells = ((long) hi - (long) lo + 1) - keys.length; - boolean useTableSwitch = keys.length > 0 && - 10L * emptyCells <= (long) keys.length; // less then 10% of empty cells + /* + * Heuristic estimation if it's better to use tableswitch or lookupswitch. + * From OpenJDK sources + */ + 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; + + boolean useTableSwitch = nlabels > 0 && + table_space_cost + 3 * table_time_cost <= + lookup_space_cost + 3 * lookup_time_cost; if (!useTableSwitch) { v.lookupswitch(defaultLabel, keys, labels);