Switch vs if ?
Roland Westrelin
rwestrel at redhat.com
Tue Jan 16 14:59:51 UTC 2018
Hi Laurent,
> Do you know if hotspot prefers if over switch on integer values (profiling
> hotspots) ?
>
> Is it worth to order integer cases with higher probability first or not ?
>
> How to optimize such if cascades ?
If the switch has a small number of cases, C2 compiles it as a tree of
ifs. If it has a large number of cases, it compiles it as a jump through
a dispatch table.
If you have a large number of cases that are more or less evenly hit
then the dispatch table should help performance and beat anything you
can do with ifs.
Unfortunately, C2 doesn't leverage profiling for
tableswitch/lookupswitch. So for a small numbers of cases that you can
order by probability, using ifs in the java code could do better than
letting c2 generate a switch. Or if you have large number of cases and
you know a few of them are much higher priority, then testing for them
with ifs and falling back to switch could be beneficial.
For Shenandoah, we found the lack of profiling for
tableswitch/lookupswitch sometimes hurts performance. I've been working
on some profiling support for tableswitch/lookupswitch and that should
make it to a future jdk release.
Roland.
More information about the hotspot-compiler-dev
mailing list