is this still currently accurate info?
# stdlib
j
is this still currently accurate info?
b
You can easily test this using Tools>Kotlin>Show Kotlin Bytecode and Decompile. My tests show that the spread builder is only created when you combine an array with additional values. If you just spread an array, the array gets copied and multiple values just get passed like they would in java. If I read the bytecode correctly this creates an array. So your image seems pretty acurate
Also noteworthy is that
foo(**list.toArray())*
is even worse on performance since the data will get copied twice.
toArray()
creates one copy and the spread operator will create another one. I think there is a youtrack issue about it but I can’t find it right now.
j
in my code i had many intuitive uses of spread operator and varargs where escape analysis seems to miss the boat completely. almost everything can be a function of Array(){} that i had leveredged va or spread previously.
Copy code
@JvmName("getVA")
inline operator fun <reified T> Array<T>.get(vararg index: Int) = get(index)
inline operator fun <reified T> Array<T>.get(  index: IntArray) = Array(index.size) { i: Int -> this[index[i]] }
this refactoring just reduced my heap allocations by 50%
also, foo[*newindexes] is illegal syntax 😞
on that note, what are thoughts on pairing up IntRanges and Int Vararg tuples with Index operators by default?am i the first to ask? do i have to carry a handy trick like this around in my bag of personal kotlin hacks?
Copy code
@JvmName("getVA")
inline operator fun <reified T> Array<T>.get(vararg index: Int) = get(index)
inline operator fun <reified T> Array<T>.get(  index: 
IntArray) = Array(index.size) { i: Int -> this[index[i]] }
foo[3,2,1]
is extremely handy to have for an array copy operator, likewise list im sure.