[swift-evolution] Does protocol support add to an object's size?
Jordan Rose
jordan_rose at apple.com
Mon Apr 3 15:33:38 CDT 2017
> On Mar 31, 2017, at 09:39, Joe Groff via swift-evolution <swift-evolution at swift.org> wrote:
>
>
>> On Mar 31, 2017, at 9:33 AM, Daryle Walker via swift-evolution <swift-evolution at swift.org> wrote:
>>
>>
>>> On Feb 15, 2017, at 3:36 PM, Slava Pestov <spestov at apple.com> wrote:
>>>
>>> Values of concrete type always have the same size regardless of what protocols the type conforms to.
>>
>> So, two struct types with the same instance-level stored properties, but one with no protocol conformance and the other with at least one, have the same stride-of? Does that mean that the latter type keeps no per-instance space for protocol support? Or that all struct types keep protocol accounting information, even when a struct’s protocol list is empty (like the former type)?
>
> Protocol conformances are a distinct runtime object from the type itself. There's never any direct record of the conformance in a type's own metadata or instances. This is what allows conformances to be added externally by extensions from other modules.
At the same time, you are not allowed to assume anything about the layout of structs defined in Swift today. If the compiler wanted to insert the name of the type between every member, it would be permitted to. Please do not assume two structs that look "the same" are laid out the same; if you need to care about in-memory layout, define the struct in C.
Jordan
More information about the swift-evolution
mailing list