2017 LLVM Developers’ Meeting: “Implementing Swift Generics ”

Channel:LLVM · Length:44:37 · Published:2017-10-31 · Views:20879

Watch on YouTube

Summary

Swift compiler engineers explain how Swift implements generics using dictionary-passing with reified type metadata. They compare it with Java-style uniform representation and C++ or Rust monomorphization.

Why HN recommends it

Recommended as the clearest explanation of how Swift generics compile and how that differs from other languages.

there's a very nice talk on how Swift's generics compile. — HN, 2017

Programming language design · Compilers, interpreters & program analysis

Subtitles (excerpt)

okay I'm Slava pest ah've and today we're going to talk about generics and Swift so with other languages that have generics there are two popular implementation strategies so one you see a lot is just two mono morphing you take your generic function and for every instantiation of that function with concrete types the compiler generates a new specialization another common strategy is to use the types to type-check the function but then use some kind of boxed representation or runtime erasing the generic types so what a swift do well Swift does something different and it will take most of this talk to describe that but first let's start by giving an overview of the language so in Swift generic parameters are bounded this means that the type signature of a generic function places constraints on those parameters which describes what you can do with values of that type in this respect it's similar to most other languages that have generics but different than C++ templates for example and bounded generics is what enables us to do separate compilation so if we can type check and generate the code for a generic function separately from any specific instantiation or that function with concrete types and we can call it from another

On Hacker News

Swift's generics have some unique properties at module boundaries to allow for a stable ABI while enabling library evolution. To get additional background, you might be interested in this[0] conference talk (the paper's author is one of the co-presenters). Also, this post[1] by a Rust and Swift compiler engineer talks about what Swift does differently than other languages, and the tradeoffs that … — favorited, 2022-10
If you're interested, there's a very nice talk on how Swift's generics compile. It's different to Rust in some key ways. [VIDEO] — Veedrac, 2017-12
> It doesn't make sense to me that an embedded VM/interpreter could ever outperform direct code. You're adding a layer of abstraction and indirection, so how is it possible that a more indirect solution can have better performance? It is funny, but (like I’ve already mentioned[1] a few months ago) for serialization(-adjacent) formats in particular the preferential position of bytecode interpreter… — mananaysiempre, 2026-04
The Swift story around ABI stability and library evolution is pretty unique. Relatedly, its approach to generics doesn't fall into either of the 2 traditional paths (either box everything like Java and C#, or template instantiation like C++ or Rust). [LINK] [VIDEO] — favorited, 2022-11

Related videos