We're encouraged to use
class in Swift.
This is because
- The compiler can do a lot of optimizations
- Instances are created on the stack which is a lot more performant than
The downside to
struct variables is that they are copied each time when returning from or assigned to a function. Obviously, this can become a bottleneck too.
E.g. imagine a 4x4 matrix. 16 Float values would have to be copied on every assign/return which would be 1'024 bits on a 64 bit system.
One way you can avoid this is using
inout when passing variables to functions, which is basically Swifts way of creating a pointer. But then we're also discouraged from using
So to my question:
How should I handle large, immutable data structures in Swift?
Do I have to worry creating a large
struct with many members?
If yes, when am I crossing the line?