Generics are constrained by protocols that list the entire set of operations you can do on the type. In unspecialized code, vtables for each requirement are passed along with the metadata for the type itself that dynamically provides the physical layout of values
-
-
Replying to @jckarter
In C++, vtables are always built into a struct. Does Swift have the ability to "erase" a vtable if it statically knows the struct type?
1 reply 1 retweet 1 like -
Replying to @mcclure111
Yeah. If you call a generic function whose implementation is available to the compiler, the optimizer will generate a version where the type is substituted and the vtables become unnecessary, more like a C++ template. Sometimes we do this for non type arguments too like closures
2 replies 1 retweet 6 likes -
-
Replying to @mcclure111 @jckarter
to be clear: vtable pointers are never part of a struct, they are either in the ref-counting header of a box, or passed as an "argument" to polymorphic code. this must be, as e.g. Array<T> lays out struct values inline and has the same layout in polymorphic and specialized code
1 reply 0 retweets 1 like -
that sounds smart. to be clear, the "passed as an argument" is invisible to the user?
1 reply 0 retweets 1 like -
-
you can find a bunch of notes on how generic type metadata is represented here: https://github.com/apple/swift/blob/master/docs/ABI/TypeMetadata.rst … (probably slightly out of date, they made lots of changes in the run up to ABI stability)
1 reply 0 retweets 1 like -
aw bummer, joe did y'all really rip out all the cute vtable entries for "$primitive_op, but do it for an array"? stupid well-defined is_pod query and "caring about code size" and "we have to generate this vtables at runtime oh god" ruining all the fun.
1 reply 0 retweets 1 like -
Replying to @Gankra_ @mcclure111
My next wild idea is to represent the type layout as a bitstring showing where the refcounted are instead of a vtable of primitive operations. Chances are this could end up being faster for runtime-generated types than going through n layers of unspecialized goo
2 replies 0 retweets 0 likes
I tried this for Rust once! It didn’t go well, but maybe Swift will do better
-
-
What problems did you run into?
1 reply 0 retweets 0 likes -
I wrote an entire VM in C++ templates to interpret these layouts as quickly as possible to adjust reference counts and such. It was a large speed penalty (~30% of time profile of Rust code was spent in it or something) and didn’t even reduce code size that much.
2 replies 0 retweets 0 likes - 9 more replies
New conversation -
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.