christophsturm

    christophsturm

    1 year ago
    is there a way to speed up this code?
    object S {
        val string = "String"
    }
    fun main() {
        println(measureTimeMillis {
            StringProvider(S::class).getContext()
        })
    }
    class StringProvider(val kClass: KClass<*>) {
        fun getContext(): String {
            return kClass.declaredMemberProperties.single { it.name == "string" }.call(kClass.objectInstance) as String
        }
    }
    it takes 420ms on my mac
    this takes just 16ms:
    fun getContext(): RootContext {
            val jClass = kClass.java
            val obj = jClass.getDeclaredField("INSTANCE").get(null)
            return jClass.getDeclaredMethod("getContext").invoke(obj) as RootContext
        }