Tuesday, September 8, 2020 [Tweets] [Favorites]

Swift Runtime Heap Objects and Type Layout

Jordan Rose:

The way Swift’s flavor of automatic reference counting works is that destruction of the object happens synchronously when the last reference goes away. So while swift_retain ends after updating the reference count, swift_release has to check to see if the object should be destroyed. If the old reference count representation was 0 (remember, it’s a biased representation), then this release is for the last reference, and it’s time to destroy the object. As mentioned above, this information is stored relative to the class metadata, whose address we get by loading the first field of the object.


The first field of a closure is a function pointer with a special calling convention: it takes the arguments of the closure as well as an additional argument for accessing any captured bindings (parameters, variables, or constants, or explicitly-specified bindings from a capture list). This additional argument is loaded from the second field, and you can think of it as a sort of “anonymous class” that stores the captures. Passing around a closure means retaining and releasing this “captures object”, and it’s destroyed when the reference count hits 0 like any other object.

Jordan Rose:

So what does Swift do? For now, it just does the simple thing: lay the fields out in order, rounding up to alignment boundaries. (This means you can actually save on memory by carefully ordering your fields, although this probably only matters if you have many many instances of your structs.)


The “Product” example I showed above was a struct that could be laid out completely at compile time. However, that’s not always possible. If the types of First and Second aren’t known at compile time, it’s up to the runtime to figure it out. […] So not only does the runtime need to do type layout, but now you know why it has to be consistent with how the compiler does it.



Stay up-to-date by subscribing to the Comments RSS Feed for this post.

Leave a Comment