Switch vs if ?

Laurent Bourgès bourges.laurent at gmail.com
Wed Jan 17 08:38:56 UTC 2018

Thanks Roland for your detailled explanations, that confirmed ifs have
better profiling & optimization.

In the Marlin renderer, switch are used on curve types: 4=lines, 6=quads,
8=cubics so only 3 cases with probabilities lines > cubics >> quads.

I will experiment using ordered if clauses as these methods are hotspots.
Ideally the switch lookup table (jmp) should be faster...


Le 16 janv. 2018 3:59 PM, "Roland Westrelin" <rwestrel at redhat.com> a écrit :

> 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/hotspot-compiler-dev/attachments/20180117/5f550c52/attachment.html>

More information about the hotspot-compiler-dev mailing list