Switch vs if ?

John Rose john.r.rose at oracle.com
Wed Jan 17 20:42:15 UTC 2018

On Jan 16, 2018, at 6:59 AM, Roland Westrelin <rwestrel at redhat.com> wrote:
> I've been working
> on some profiling support for tableswitch/lookupswitch and that should
> make it to a future jdk release.

I'm really glad about that!  Here's a relevant bug:

Until that stuff is fixed, you might choose to code like this:

if (…is 80% case…)
  …do 80% stuff…
else if (…is 10% case …)
  …do 10% stuff…
else switch (…some key…) {
…many other cases…


What we should be able to write is everything in one
switch, while the JVM should detect which cases are hot.
If you think you know what are the hot cases, you could
emphasize them however you want, e.g., by putting
them at the top of the switch.  So we want to write evenly
balanced code and get it optimized as if it were written
as above (unevenly).

— John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/hotspot-compiler-dev/attachments/20180117/7b3312f1/attachment-0001.html>

More information about the hotspot-compiler-dev mailing list