Also no syntax is good until proven performant. Ko...
# pattern-matching
r
Also no syntax is good until proven performant. Kotlin currently optimizes many
when
into table switches indexed that are very fast
👍🏻 1
n
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
what happen if you use when over ints?
n
I am using when. I am looking at bytecode generated from a when
OHH i misread you 1sec
r
I mean in the case when the subject is an int or a primitive value
n
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
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
That's clever! but yes for the average case it's not an issue