Static member lookup performance / alternatives

I have a hot loop in my code that needs to set a var to different values based on an argument (essentially using a switch statement as lookup). Because the value set is used elsewhere (and has a non-zero init time) I've created static lets for all of them. However, instruments is showing a non-trivial amount of time being spent in the member's .unsafeMutableAddressor.

How can I refactor my code to avoid this cost? It's my understanding that global variables will have the same overhead (without the scoping/namespace advantages).

pseudo code:

struct Foo {
    
    static let matrixA = float3x3(...)
    static let matrixB = float3x3(...)
    
    @inline(__always) static func doStuff(input: SIMD3<Float>, whichToUse: SomeEnumType = .useA) -> SIMD3<Float> {
        
        let matrix = switch whichToUse {
        case .useA:
            matrixA
        case .useB:
            matrixB
        }
        
        //...use the matrix here
    }
}
'''

Perhaps you can form the array [matrixA, matrixB] and pass it around, so whichToUse becomes an index into the array. But this will incur the overhead of a bounds check, and possibly reference counting the array.

yes that fixes it, but then I have the values hard coded in multiple places (they are matrixes, so they are a set of 9 floats... right now I generate them in a helper app and copy their definitions into the struct)

If the matrices are just constants, you can extract the switch whichToUse into a helper function, and just put the constant expressions inside each case of the switch.