Just discovered empirically that `is SomeInterface...
# random
y
Just discovered empirically that
is SomeInterface
is slower than
is SomeClass
, maybe about 6x slower. That pushed me to switch a
sealed interface
to a
sealed class
👀 2
f
Interessting find!
2
f
Can you share some benchmarks? It seems Platform dependant and something that would be optimized by the JIT
e
as classes are only single inheritance, class instanceof can walk up the parent tree only
whereas interfaces are multiple inheritance, so interface instanceof is more complex
still I would have hoped that Java's PermittedSubclasses attribute would be able to optimize that
(which Kotlin sealed compiles to as long as the JVM target 17+ iirc)
y
Oh yeah my JVM target is like 11, so maybe that's why
in principle the JVM could optimize it better even without
PermittedSubclasses
(there's already CHA during classloading) but it's not really significant so it seems the current implementation is considered good enough
e
@Youssef Shoaib [MOD] are you planning on filing an issue with your findings? 6x is pretty significant for a common pattern.
e
file an issue where? its performance is specific to different JVM implementations (eg hotspot will do differently than j9)
the mailing list link I sent above is talking about hotspot in particular
e
Ah I was assuming this was a Kotlin specific problem. Thanks for the link.
e
6x of 0 is 0 AND 6x of 2 is still less than 100 🙂 Point is, does using interfaces really reduce performance of your entire application? I would wager that for majority of apps, answer would be a no. The distinction btw sealed interface and sealed class has real world impact either in mental model of a domain or in its maintainability, I would say to be careful in making such a change in your own applications without doing some benchmarking
y
It was making a significant difference in a benchmark I was running. I'm writing a library, not an end-user app, so my goal is to reduce overhead as much as possible. For an app, I would never care about this. For my library, I was running millions of if these checks, so it genuinely made a difference to the overall performance
👍🏾 1
💯 1