Friday, November 6, 2015

How Swift Implements Generics

Chris Lattner (via Erica Sadun):

The semantic model of swift generics is that they use runtime dispatch through “witness tables” provided by the protocol conformances of the generic types. This model allows for fast -O0 compiles and separate compilation of generics.

The problem with this model is that actually relying on this for everything would produce code that runs very slowly. To solve this problem, the optimizer uses heuristic-driven generic specialization that does code duplication where it thinks that it is profitable and sensible.

The way to contrast C++ and Swift is: C++ eagerly duplicates code in the frontend (and hopefully the optimizer can eliminate some of the copies later, with LTO…). Swift does not generate any copies in the front-end, but does generate them in the optimizer.

Also:

We are still on track to open source Swift (including Linux support) “by the end of 2015” as promised, more details will come out when they can.

Comments RSS · Twitter

Leave a Comment