Friday, October 7, 2016 [Tweets] [Favorites]

Real World Swift Performance

Danielle Tomlinson:

Let’s talk about generics. You’re going to say, but you just showed us that protocols could be really slow, why would we want to use generics? The answer comes from what generics allow us to do.

Say we have this stack struct that is generic of a T, which is constrained by some type, which would be a protocol. What the compiler will do is replace that T with the protocol or the concrete class that you’re passing to it. Do that all of the way down the function chain and it will create specialized versions of that code that operate directly on the type.

You no longer need to go through the value witness table, or the protocol witness table, and you eradicate the existential container, which could be a really nice way to still write really fast generic code and have the really nice polymorphism that Swift gives us. That’s called static polymorphism.

Comments

Stay up-to-date by subscribing to the Comments RSS Feed for this post.

Leave a Comment