Derek Peirce
07/30/2023, 1:44 AMoverride fun compareTo(other: Item): Int {
return compareValuesBy(this, other, { foo }, { bar })
}
The version that takes one argument is `inline`:
@kotlin.internal.InlineOnly
public inline fun <T> compareValuesBy(a: T, b: T, selector: (T) -> Comparable<*>?): Int {
return compareValues(selector(a), selector(b))
}
but the one that takes a variable number is not:
public fun <T> compareValuesBy(a: T, b: T, vararg selectors: (T) -> Comparable<*>?): Int {
require(selectors.size > 0)
return compareValuesByImpl(a, b, selectors)
}
This makes it considerably less efficient. This could be solved by just adding different compareValuesBy
methods with different lambda counts to stdlib, but the more forward-thinking approach is to inline the lambdas and unroll the loop on the array.
I tried searching for the topic, and found only my own post from 2020, which included a performance test demonstrating that inlining is faster. I haven't found any other progress on it, is this something on the Kotlin roadmap?
https://discuss.kotlinlang.org/t/inlining-arrays-of-lambdas/18249Youssef Shoaib [MOD]
07/30/2023, 3:56 AMcompareValuesBy
as a motivating example!
https://youtrack.jetbrains.com/issue/KT-20445/Support-inline-varargs-of-functional-types-and-propertiesDerek Peirce
07/30/2023, 4:08 AM