[swift-evolution] Does protocol support add to an object's size?

Charlie Monroe charlie at charliemonroe.net
Mon Apr 3 23:47:07 CDT 2017


> On Apr 4, 2017, at 1:48 AM, Daryle Walker via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 
>> On Apr 3, 2017, at 4:33 PM, Jordan Rose <jordan_rose at apple.com <mailto:jordan_rose at apple.com>> wrote:
>> 
>>> On Mar 31, 2017, at 09:39, Joe Groff via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>> 
>>> 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.
> 
> As I said in the original post:
> 
>> I want to maintain the stride of an array being the total count times the stride of the element, which would complicate nominal arrays if adding protocols to one breaks that. 
> 
> This is for built-in arrays, which we don’t have yet. I want to make sure that enforcing “no padding outside the inner non-array type” won’t affect doing “cool Swift things” with the type. This question is mainly aimed towards Swift compiler implementors.

I am not an implementor of the compiler, but the structure, once compiled will have the same layout and size during runtime, no matter what protocols it conforms to. Protocols cannot add stored members and the information about conformance is not stored within the structure instance.

Note that compilers of different Swift versions can slightly modify the layout and the size may be different on different devices (if you use pointer-sized members such as Int, UInt or object references). Once the ABI is stable, you can rely on the size more.

If you need to rely on the structure of the struct more, fall back to C as Jordan suggested. C allows you to create packed structs, etc. which Swift currently doesn't.

> 
>> Daryle Walker
> Mac, Internet, and Video Game Junkie
> darylew AT mac DOT com 
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170404/19058c8d/attachment.html>


More information about the swift-evolution mailing list