https://kotlinlang.org logo
Title
r

raulraja

05/11/2020, 4:26 PM
Also no syntax is good until proven performant. Kotlin currently optimizes many
when
into table switches indexed that are very fast
👍🏻 1
n

Nico

05/16/2020, 2:45 PM
This surprised me a lot because I did not know such a thing was possible in the JVM! I took a look at some bytecode generated from a
when
and all I see are chained
instanceof
calls. Did you mean this or am I missing something? I thought you meant a jump table that's constant time (much like haskell's pattern matching or a C
switch
statement)
As in a lookup table
r

raulraja

05/16/2020, 2:47 PM
what happen if you use when over ints?
n

Nico

05/16/2020, 2:47 PM
I am using when. I am looking at bytecode generated from a when
OHH i misread you 1sec
r

raulraja

05/16/2020, 2:48 PM
I mean in the case when the subject is an int or a primitive value
n

Nico

05/16/2020, 2:49 PM
Yeah no that's a proper jump table that's fine
Ok I just wanted to confirm it was impossible to do it with
instanceof
types instead of primitives with the instruction set we have in java 6+ currently
Cheers!
r

raulraja

05/16/2020, 3:46 PM
so the trick is that you can change those instance of for actual indexes of int
esentially is like an enumeration
every node of the tree is am int and then based on where you hit you cast it and decompose the structure in nested ifs that return null if there is no match
instanceof is fast too, this would be a micro optimization
n

Nico

05/16/2020, 3:59 PM
That's clever! but yes for the average case it's not an issue