JVM IR: Optimize equality on class literals
In common code in a multiplatform project the KotlinGenerateEqualsAndHashcodeAction generates equals methods containing a check of the form this::class == other::class. This is slower than code using javaClass on Kotlin/JVM since it contains additional calls to kotlin.jvm.internal.Reflection.getOrCreateKotlinClass.
This PR optimizes this check in the JVM IR backend, by replacing a::class == b::class with a.javaClass === b.javaClass.
KClass.equals uses javaObjectType to compare underlying classes, so behavior in this case will change:
fun f(a: Any): Boolean =
a::class == Int::class
fun main() {
println(f(42)) // Should be true
}
I completely missed that. I've changed the code to always compare boxed class types and added a test for boxed against unboxed primitive class comparisons.
In effect, this is implementing javaObjectType as an intrinsic in the Equals implementation. I didn't turn a::class.javaObjectType itself into an intrinsic since this pattern doesn't seem to be very common.
Merged in a90c4d5dd54a289a96b7e167c5c75d67656f1d49 with minor corrections. Thanks!