Swift Struct Storage
We can see that it’s exactly as we expected: when passing the
struct
to aninout
parameter, thestruct
is laid out contiguously in memory and then a pointer to it is passed in.[…]
struct
storage in Swift is ultimately pretty straightforward, and much of what we’ve seen carries over from C’s much simplerstruct
s. Astruct
instance is largely treated as a loose collection of independent values, which can be manipulated collectively when required. Localstruct
variables might be stored on the stack or the individual pieces might be stored in registers, depending on the size of thestruct
, the register usage of the rest of the code, and the whims of the compiler. Smallstruct
s are passed and returned in registers, while largerstruct
s are passed and returned by reference.struct
s get copied whenever they’re passed and returned. Although you can usestruct
s to implement copy-on-write data types, the base language construct is copied eagerly and more or less blindly.