Creating a simd.floatNxN from array is very slow

Why creating a simd.floatNxN from array is slow?
Sample: Swift simd matrix init bench Raw · GitHub

/cc @scanon

At a guess, I’d suspect it’s because you are *allocating* an array.

The array has a size of 3 elements, so used FixedArray, and allocations can not be. (i think)

+ (compiler must optimize this call?)

Even if the allocations get elided, you're still building and tearing down objects repeatedly, plus the floatNxN constructor from an array needs to access the elements individually (with some bounds checks that my or may not get elided by the compiler), whereas the other constructor can just copy each column wholesale. An instruments trace would confirm easily enough.

Ok, I profiled the code and the compiler does not optimize the array if I calls method from other module.

Sample optimize:


    var inverse = float3x3([...])


    var inverse = float3x3(fromArray: [...])


    public extension float3x3 {
        init(fromArray array: [float3]) {
            precondition(array.count == 3, "float3x3 requires an array with 3 elements.")
            self.init(columns: (array[0], array[1], array[2]))

Result: x10 up performance.