Why does Swift need witness tables?

I'm trying to read up on implementation details of Swift, and one thing I can't nail down are its "witness tables". It looks like they're a separate vtable pointer used for structs.


But why would you need that? Structs are copied by value, so you already know at compile-time what type they are. So wouldn't you just hard-code which method to call and be done with it? Why perform virtual dispatch on these methods?


http://programmers.stackexchange.com/q/331971

The Apple engineers talk about it at the 30:00 mark in the video. ( This is a really good presentation imo )


https://developer.apple.com/videos/play/wwdc2016/416/


Ya, seems like there is a game of when stuff is put on the heap versus stack and other strategies to avoid arc activity. So, lots of pressure to use Structs. Since the heap is shared across threads, all access to it has to go through locking.


One thing I wonder about is -- Supposedly Structs can avoid the heap but only if the data they contain is less than 2 words with no references. Otherwise, it looks like its allocated on the heap. Also, if they contain references, then arc retains/releases references in a Struct. So, besides localization and protocol/extension style design how is a struct a benefit? Apparently, iterating through an array of struct is more efficient because it does not retain the element like a class reference type. Anyhow, most of my data structures have more than 2 words in them ... which makes me confused.

I think larger structs (larger than 2 words) are allocated on the stack if they don't escape the defining scope — if they're not return values and not captured by an escaping closure. Analogously, instance properties of struct type can be allocated within the memory block of the enclosing type, which may be stack or heap depending on the requirements of the enclosing type.


However, it's slightly more complicated than that, because some structs (value types) may be represented as reference types internally, regardless of size. For example, String values likely have an allocated block for their character storage, though String variables have value semantics. (Except that some short strings are represented by tagged pointers, and so use no data storage, and maybe some other short immutable strings are stored as structs really.)


Also, I believe I read or saw somewhere that Swift may at some point implement the allocation of class (reference type) object memory on the stack instead of the heap, if the value doesn't escape the defining scope.


AFAIK, the witness tables aren't about virtual dispatch of struct methods (there is no virtualization, because no inheritance), but about resolving the correct method to use when struct values are referenced via a protocol. It's similar to virtual dispatch in that the choice of the method has to be done at run time, but its main purpose (I would say) is to allow the reference (40 bits including pointer to witness table) to be small and fixed size, while the information need to dispatch the method may be large and vary in size between different methods.

Why does Swift need witness tables?
 
 
Q