Home > Software design >  How do Swift arrays connect indices to addresses?
How do Swift arrays connect indices to addresses?

Time:07-09

How does swift handle the “Any” type when using arrays. I figure swift handles arrays with a specific type the way c would, where it gets the address of the object at index “n” by moving (n*sizeOfObject) spaces from the initial address. How does this work with “Any” types where the objects could have differing sizes?

CodePudding user response:

When using any protocol existential like Any, a constant-sized box is used called an existential container. This box either holds a value inline (if it’s small enough), or it contains a pointer to reference counted, heap-allocated memory.

For more details, I’d suggest the Understanding Swift Performance talk from WWDC 2016. The relevant part starts around 24:30.

  • Related