https://kotlinlang.org logo
#reflect
Title
# reflect
c

christophsturm

03/26/2021, 11:36 AM
kotlin reflection on the jvm seems to be 100x slower than java reflection for some usecases. is that a known issue?
people say reflection is slow, but things that take <1ms in java take 300ms on kotlin reflection
KClassImpl has an inner class
Data
and this class takes ages to build. and it is built lazily before any other reflection call is possible
1ms:
Copy code
val instanceField = jClass.getDeclaredField("INSTANCE")
            val obj = instanceField.get(null)
            val contexts = jClass.getDeclaredMethod("getContext").invoke(obj)
300ms:
Copy code
return kClass.declaredMemberProperties.single { it.name == "context"
 }.call(kClass.objectInstance) as RootContext
u

udalov

03/26/2021, 12:46 PM
First access is slow, subsequent accesses shoudl be significantly faster. It’s a known issue https://youtrack.jetbrains.com/issue/KT-32198
c

christophsturm

03/27/2021, 7:41 PM
Ok great. Like the ticket suggests it would be great if some simple use cases would work without initializing the inner data class
226 Views