Edoardo Luppi
06/06/2024, 9:52 AM1
element optimization in toList
public fun <T> Array<out T>.toList(): List<T> {
return when (size) {
0 -> emptyList()
1 -> listOf(this[0])
else -> this.toMutableList()
}
}
Looking at the listOf
implementations it's mostly redundant checks and basically spawning another ArrayList
, same as toMutableList
Oleg Yukhnevich
06/06/2024, 9:54 AMSam
06/06/2024, 9:54 AMpublic actual fun <T> listOf(element: T): List<T> = java.util.Collections.singletonList(element)
Edoardo Luppi
06/06/2024, 9:55 AMcommonMain
in multiplatform
public fun <T> listOf(vararg elements: T): List<T> = if (elements.size > 0) elements.asList() else emptyList()
asList
again is then actualized with ArraysUtilJVM.asList(this)
, which spawns an ArrayList
Edoardo Luppi
06/06/2024, 9:56 AMtoMutableList
, at least in KMPEdoardo Luppi
06/06/2024, 9:57 AMEdoardo Luppi
06/06/2024, 9:59 AM