tableswitch does not use an array of offsets

Yann Le Tallec ylt at
Wed Mar 27 01:56:06 PDT 2013

The code below is compiled as a tableswitch bytecode instruction as one
would expect because the case values are contiguous. However, the JIT
(Hotspot using JDK/JRE 1.7u17 64 bit on x86/Windows in either -server or
-client mode) compiles it into a succession of cmp/je/jg.

When adding one more case statement (case 17) to reach a total number of 18
case statements, the JIT does compile the switch using an array of offsets
to calculate the jump.

A micro-benchmark (controlled for compilation, inlining (off), gc) shows
that adding an additional case statement to the method below (case 17) to
reach a total of 18 improves performance materially (up to 25% depending on
how the method is exercised).

Is the decision to *not* use an array of offsets for <18 cases derived from
profiling? Is that the expected behaviour and what is the underlying reason?

Many thanks,

    static double multiplyByPowerOfTen(final double d, final int exponent) {
        switch (exponent) {
            case 0:
                return d;
            case 1:
                return d * 10;
            case 2:
                return d * 100;
            case 3:
                return d * 1000;
            case 4:
                return d * 10000;
            case 5:
                return d * 100000;
            case 6:
                return d * 1000000;
            case 7:
                return d * 10000000;
            case 8:
                return d * 100000000;
            case 9:
                return d * 1000000000;
            case 10:
                return d * 10000000000L;
            case 11:
                return d * 100000000000L;
            case 12:
                return d * 1000000000000L;
            case 13:
                return d * 10000000000000L;
            case 14:
                return d * 100000000000000L;
            case 15:
                return d * 1000000000000000L;
            case 16:
                return d * 10000000000000000L;
                throw new RuntimeException("Unhandled power of ten " +
-------------- next part --------------
An HTML attachment was scrubbed...

More information about the hotspot-compiler-dev mailing list