# Static member lookup performance / alternatives

**URL:** <https://forums.swift.org/t/static-member-lookup-performance-alternatives/78314>\
**Category:** Using Swift\
**Tags:** optimizations\
**Created:** [March 5, 2025, 8:55pm UTC](https://forums.swift.org/t/static-member-lookup-performance-alternatives/78314 "2025-03-05T20:55:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikewoodworth](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mikewoodworth/32/25151_2.png) [@mikewoodworth](https://forums.swift.org/u/mikewoodworth)\
**Post date:** [March 5, 2025, 8:55pm UTC](https://forums.swift.org/t/static-member-lookup-performance-alternatives/78314/1 "2025-03-05T20:55:37Z")

</div>

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:

```swift
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
    }
}
'''
```

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [March 5, 2025, 9:21pm UTC](https://forums.swift.org/t/static-member-lookup-performance-alternatives/78314/2 "2025-03-05T21:21:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mikewoodworth](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mikewoodworth/32/25151_2.png) [@mikewoodworth](https://forums.swift.org/u/mikewoodworth)\
**Post date:** [March 5, 2025, 9:28pm UTC](https://forums.swift.org/t/static-member-lookup-performance-alternatives/78314/3 "2025-03-05T21:28:04Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [March 5, 2025, 9:58pm UTC](https://forums.swift.org/t/static-member-lookup-performance-alternatives/78314/4 "2025-03-05T21:58:44Z")

</div>

> [@mikewoodworth](#):
>
> 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.
