I understand that Java does optimizations across the board and can optimize switches depending on how many cases there are, whether the switch is sparse or dense, and if the code is even hot enough for the JIT to optimize said code. I'm just wondering if I need to further provide "assist" like using bitwise and on the variable being tested or bitwise shift the variable to a "nicer" number, assuming said code is indeed very hot. "nicer" being开发者_JAVA百科 0xXX rather than 0xFFXX. Are there hotspot JIT docs that specifically state what's required? Rather than "just keep it along the general programming guidelines of xxx."
The decision for either tableswitch
or lookupswitch
is already done by the javac
compiler. I have never heard that the JIT tries to transform a lookupswitch
into a tableswitch
.
So your first step should be a check into what bytecode javac
translated your switch
.
There is not going to be an easy answer to this.
I would take this question to hotspot-dev - they may be able to provide more information about how this happens.
The other people who may have interesting perspectives on this hang out on the jvm-l mailing list (Charlie Nutter, Remi Forax, John Rose, etc.)
Because Java itself runs over a virtual machine, unless I'm missing something here, I don't think there's an easy way to do this.
So with Java, you get a black box of sorts, and to optimize more is risky, IMHO.
For the Hotspot JIT whitepaper, see here
精彩评论