Nested InlineArray generates unnecessary copies

Hi,

I was playing around with InlineArray and found out that this function

func col(i: Int, v: InlineArray<8, InlineArray<10, Int>>) -> InlineArray<10, Int> {
    [v[i][0], v[i][1], v[i][2], v[i][3], v[i][4], 
     v[i][5], v[i][6], v[i][7], v[i][8], v[i][9]]
}

puts 10 copies of v on the stack. godbolt. Am I missing something?

2 Likes

Is the input array an array of 8 rows or 8 columns?

It looks like it is an array of 8 columns, judging by the return value of the function. Then, the return value should be just the i th element of the input array. No? :slight_smile:

Yes, in this simplified example, you could do this

    func row(i: Int) -> InlineArray<10, Double> {
        return vec[i]
    }

And that function will still create one unneccessary copy of vec.

Similar problem. Local (or struct property, or global) array results in a lot of moves; output is fine when the array is a static property.