Inefficient property reference compilation with inline functions

In the topic bellow, property.name is introduced as compile-time constant, but this does not work correctly with inline functions

Example (simplified, not makes sense much):

import kotlin.reflect.KProperty

class WrapperClass(private val data: Map<String, Any?>) {

    val value1: Int
        get() = data.getTyped(WrapperClass::value1)
        // not ok, creates extra class which extends PropertyReference1Impl for the property is generated

    val value2: Int
        get() = data.getTyped(WrapperClass::value2.name)
        // ok, same as:
        // get() = data.getTyped("value2")
}

inline fun <T> Map<String, Any?>.getTyped(name: String): T {
    return this[name] as T
}

inline fun <T> Map<String, Any?>.getTyped(prop: KProperty<*>): T {
    return getTyped(prop.name)
}

I expect, that this:
get() = data.getTyped(WrapperClass::value1)
to generate equally efficient code as this
get() = data.getTyped(“value1”)

  • reason why I use property instead of its name directly is that It holds its type, but property.name does not

No it’s not introduced as a compile-time constant. It’s suggested that it should be. The issue for this is still open. It’s linked there as well but here it is again: KT-16304

My guess is that optimizations like that will become a higher priority once the compiler backend rewrite is finished. As far as I know this is an ongoing project that will be partially done with 1.4 (new type inference and IR for mpp) but will not be finished before kotlin 1.5.

1 Like