[swift-users] [swift-dev] Guarantees of Tuples as Fixed Sized (stack allocated) Arrays
John McCall
rjmccall at apple.com
Tue May 2 15:53:31 CDT 2017
> On May 2, 2017, at 4:38 PM, Jordan Rose <jordan_rose at apple.com> wrote:
>> On Apr 28, 2017, at 16:28, John McCall via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
>>
>>> On Apr 28, 2017, at 7:03 AM, Johannes Weiss via swift-dev <swift-dev at swift.org <mailto:swift-dev at swift.org>> wrote:
>>> Hi swift-users,
>>>
>>> (sorry for the cross post to swift-dev, but wasn't sure where it belongs)
>>>
>>> I tried to find guarantees about the memory layout Swift tuples but couldn't find anything. The reason I ask is because I'd like to use them as fixed sized (stack allocated) arrays. I'm pretty sure they're actually not guaranteed to be stack allocated but highly likely I assume :).
>>
>> Tuples are guaranteed to use a standard C-style layout wherever that layout is ABI-observable, e.g. when you construct an UnsafePointer to one.
>
> Ah, is this true for non-homogeneous tuples? I thought we only guaranteed it for homogeneous ones.
It's true of all tuples; we have to have some agreement on tuple layout that works generically, and the simple C left-to-right layout is the obvious one. Of course, like I said, we totally reserve the right to not actually store tuples that way in specific classes, structs, enums, locals, argument, results, or whatever else; but in places where a universal ABI is necessary, including pointers, we do make that guarantee.
This notion of universal ABI generally also affects things like generic data structures, which can mean that e.g. a [(MyClass, Bool)] might be stored less efficiently than a [MyPair] where
struct MyPair { var object: MyClass; var flag: Bool }
But that's just the price of supporting dynamic generics.
John.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170502/02ee8e95/attachment.html>
More information about the swift-users
mailing list