Switch vs if ?

Sergey Bylokhov Sergey.Bylokhov at oracle.com
Thu Jan 18 22:46:11 UTC 2018


Hi, Laurent.
It would be good to check graal as a jit as well.

On 17/01/2018 23:42, Laurent Bourgès wrote:
> Thanks for the concrete code samples.
> 
> I will gather my own stats and give you my real benchmark results once I 
> converted switch(3 cases) to if cascades.
> 
> Bye,
> Laurent
> 
> Le 17 janv. 2018 9:42 PM, "John Rose" <john.r.rose at oracle.com 
> <mailto:john.r.rose at oracle.com>> a écrit :
> 
>     On Jan 16, 2018, at 6:59 AM, Roland Westrelin <rwestrel at redhat.com
>     <mailto: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:
>     https://bugs.openjdk.java.net/browse/JDK-8143859
>     <https://bugs.openjdk.java.net/browse/JDK-8143859>
> 
>     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…
>     }
> 
>     But…
> 
>     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
> 


-- 
Best regards, Sergey.


More information about the hotspot-compiler-dev mailing list